From 7d624fd9ca2cf3fa52a2519e0b70b98c4dac916d Mon Sep 17 00:00:00 2001 From: Skud Date: Sun, 6 Jul 2014 21:21:51 +1000 Subject: [PATCH 1/5] Attempting to get scientific name editing on one page (doesn't work yet) --- app/models/crop.rb | 4 ++++ app/views/crops/_form.html.haml | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index b5340c71b..5f2682432 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -4,6 +4,10 @@ class Crop < ActiveRecord::Base attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id has_many :scientific_names + accepts_nested_attributes_for :scientific_names, + :allow_destroy => true, + :reject_if => :all_blank + has_many :plantings has_many :photos, :through => :plantings has_many :seeds diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 1ff75ce04..91cbe6c22 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -27,5 +27,13 @@ .controls = collection_select(:crop, :parent_id, Crop.all, :id, :name, {:include_blank => true}) %span.help-inline Optional. For setting up crop hierarchies for varieties etc. + .control-group + = fields_for :scientific_names do |sn| + = sn.label :scientific_name, "Scientific name", :class => 'control-label' + .controls + = sn.text_field :scientific_name + - if sn.object && sn.object.persisted? + = sn.check_box :_destroy + = sn.label :_destroy, "Delete" .form-actions = f.submit 'Save', :class => 'btn btn-primary' From 8b45a1759c9bd13d22058f41eff501d81e894bc8 Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Sun, 13 Jul 2014 17:57:55 +1000 Subject: [PATCH 2/5] got scientific names working on the crop page but view tests are failing --- CONTRIBUTORS.md | 1 + app/controllers/crops_controller.rb | 6 +++++- app/models/crop.rb | 2 +- app/views/crops/_form.html.haml | 12 ++++++++---- spec/views/crops/new.html.haml_spec.rb | 6 ++++++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 2014f5e11..561eb6968 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -38,3 +38,4 @@ submit the change with your pull request. - Yaw Boakye / [yawboakye](https://github.com/yawboakye) - Ryan Clark / [IAMRYO](https://github.com/IAMRYO) - Marty Hines / [martyhines](https://github.com/martyhines) +- Taylor Griffin / [tygriffin](https://github.com/tygriffin) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 3bb020dda..0c6f1ce71 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -74,7 +74,9 @@ class CropsController < ApplicationController # GET /crops/new.json def new @crop = Crop.new - + 3.times do + @crop.scientific_names.build + end respond_to do |format| format.html # new.html.haml format.json { render json: @crop } @@ -90,8 +92,10 @@ class CropsController < ApplicationController # POST /crops.json def create params[:crop][:creator_id] = current_member.id + puts params.to_yaml @crop = Crop.new(params[:crop]) + respond_to do |format| if @crop.save format.html { redirect_to @crop, notice: 'Crop was successfully created.' } diff --git a/app/models/crop.rb b/app/models/crop.rb index 5f2682432..05d0b1721 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -1,7 +1,7 @@ class Crop < ActiveRecord::Base extend FriendlyId friendly_id :name, use: :slugged - attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id + attr_accessible :en_wikipedia_url, :name, :parent_id, :creator_id, :scientific_names_attributes has_many :scientific_names accepts_nested_attributes_for :scientific_names, diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 91cbe6c22..8556a45ff 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -27,13 +27,17 @@ .controls = collection_select(:crop, :parent_id, Crop.all, :id, :name, {:include_blank => true}) %span.help-inline Optional. For setting up crop hierarchies for varieties etc. - .control-group - = fields_for :scientific_names do |sn| + %p + %span.help-block + You may enter up to 3 scientific names for a crop. Most crops will have only one. + = f.fields_for :scientific_names do |sn| + .control-group = sn.label :scientific_name, "Scientific name", :class => 'control-label' .controls = sn.text_field :scientific_name - if sn.object && sn.object.persisted? - = sn.check_box :_destroy - = sn.label :_destroy, "Delete" + %label.checkbox + = sn.check_box :_destroy + = sn.label :_destroy, "Delete" .form-actions = f.submit 'Save', :class => 'btn btn-primary' diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index f1ac82827..66de6013b 100644 --- a/spec/views/crops/new.html.haml_spec.rb +++ b/spec/views/crops/new.html.haml_spec.rb @@ -22,4 +22,10 @@ describe "crops/new" do assert_select "a[href^=http://wiki.growstuff.org]", "crop wrangling guide" end + it "shows three fields for scientific_name" do + assert_select "input#crop_scientific_names_attributes_0_scientific_name" + assert_select "input#crop_scientific_names_attributes_1_scientific_name" + assert_select "input#crop_scientific_names_attributes_2_scientific_name" + end + end From ded464ad4c96996119f0aece6658a89a5ed9f93d Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Sun, 13 Jul 2014 18:00:46 +1000 Subject: [PATCH 3/5] removed call to for debugging --- app/controllers/crops_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 0c6f1ce71..e5f49e0d4 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -92,7 +92,6 @@ class CropsController < ApplicationController # POST /crops.json def create params[:crop][:creator_id] = current_member.id - puts params.to_yaml @crop = Crop.new(params[:crop]) From e63b2fb4728963d710af59e75a251273df93a845 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:27:25 +1000 Subject: [PATCH 4/5] Set up sci names in spec file so fields are shown --- spec/views/crops/new.html.haml_spec.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index 66de6013b..0aa443f74 100644 --- a/spec/views/crops/new.html.haml_spec.rb +++ b/spec/views/crops/new.html.haml_spec.rb @@ -2,7 +2,11 @@ require 'spec_helper' describe "crops/new" do before(:each) do - assign(:crop, FactoryGirl.create(:maize)) + @crop = FactoryGirl.create(:maize) + 3.times do + @crop.scientific_names.build + end + assign(:crop, @crop) @member = FactoryGirl.create(:crop_wrangling_member) sign_in @member controller.stub(:current_user) { @member } @@ -23,9 +27,9 @@ describe "crops/new" do end it "shows three fields for scientific_name" do - assert_select "input#crop_scientific_names_attributes_0_scientific_name" - assert_select "input#crop_scientific_names_attributes_1_scientific_name" - assert_select "input#crop_scientific_names_attributes_2_scientific_name" + assert_select "input#crop_scientific_names_attributes_0_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_1_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_2_scientific_name", :count => 1 end end From 7184a0d5d5bd2b279b4154503958d688407e4352 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 18 Jul 2014 17:29:10 +1000 Subject: [PATCH 5/5] Added scientific name field tests for edit form --- spec/views/crops/edit.html.haml_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/views/crops/edit.html.haml_spec.rb b/spec/views/crops/edit.html.haml_spec.rb index 9cf5fd469..3db9c52b0 100644 --- a/spec/views/crops/edit.html.haml_spec.rb +++ b/spec/views/crops/edit.html.haml_spec.rb @@ -5,7 +5,11 @@ describe "crops/edit" do controller.stub(:current_user) { FactoryGirl.create(:crop_wrangling_member) } - @crop = assign(:crop, FactoryGirl.create(:maize)) + @crop = FactoryGirl.create(:maize) + 3.times do + @crop.scientific_names.build + end + assign(:crop, @crop) render end @@ -19,4 +23,10 @@ describe "crops/edit" do assert_select "input#crop_en_wikipedia_url", :name => "crop[en_wikipedia_url]" end end + + it "shows three fields for scientific_name" do + assert_select "input#crop_scientific_names_attributes_0_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_1_scientific_name", :count => 1 + assert_select "input#crop_scientific_names_attributes_2_scientific_name", :count => 1 + end end