diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d62f62787..811c7f1ad 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -41,3 +41,4 @@ submit the change with your pull request. - Mackenzie "Maco" King / [maco](https://github.com/maco) - Amelia Greenhall / [ameliagreenhall](https://github.com/ameliagreenhall) - Barb Natali / [barbnatali](https://github.com/barbnatali) +- Taylor Griffin / [tygriffin](https://github.com/tygriffin) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 6f8fa6917..ae38b2817 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -81,7 +81,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 } @@ -99,6 +101,7 @@ class CropsController < ApplicationController params[:crop][:creator_id] = current_member.id @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 211a56146..3efe8be57 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -1,9 +1,13 @@ 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, + :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..8556a45ff 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -27,5 +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. + %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? + %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/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 diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index f1ac82827..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 } @@ -22,4 +26,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", :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