From bf2b1d65968737539eecf4e8db60872c88b57b15 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 21 Dec 2012 13:32:32 +1100 Subject: [PATCH] select garden/crop based on params - BROKEN TESTSwq --- app/views/plantings/_form.html.haml | 4 +- spec/views/plantings/new.html.haml_spec.rb | 48 ++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/views/plantings/_form.html.haml b/app/views/plantings/_form.html.haml index 9be9d192f..9a332b0f1 100644 --- a/app/views/plantings/_form.html.haml +++ b/app/views/plantings/_form.html.haml @@ -8,10 +8,10 @@ .control-group = f.label 'What did you plant?', :class => 'control-label' - .controls= collection_select(:planting, :crop_id, Crop.all(:order => :system_name), :id, :system_name) + .controls= collection_select(:planting, :crop_id, Crop.all(:order => :system_name), :id, :system_name, :selected => params[:crop_id]) .control-group = f.label 'Where did you plant it?', :class => 'control-label' - .controls= collection_select(:planting, :garden_id, Garden.where(:user_id => current_user), :id, :name) + .controls= collection_select(:planting, :garden_id, Garden.where(:user_id => current_user), :id, :name, :selected => params[:garden_id]) .control-group = f.label 'When?', :class => 'control-label' .controls= f.datetime_select :planted_at, options = { :include_blank => true }, html_options = { :class => "span1" } diff --git a/spec/views/plantings/new.html.haml_spec.rb b/spec/views/plantings/new.html.haml_spec.rb index 586d71f51..dac385d3d 100644 --- a/spec/views/plantings/new.html.haml_spec.rb +++ b/spec/views/plantings/new.html.haml_spec.rb @@ -23,13 +23,43 @@ describe "plantings/new" do :password => "irrelevant") @user.confirm! sign_in @user - render + + # create gardens and crops to populate dropdowns + @garden1 = Garden.create!( + :id => 1, + :user_id => @user.id, + :name => 'Garden1' + ) + @garden2 = Garden.create!( + :id => 2, + :user_id => @user.id, + :name => 'Garden2' + ) + + @crop1 = Crop.create!( + :id => 1, + :system_name => 'Tomato', + :en_wikipedia_url => 'http://blah' + ) + @crop2 = Crop.create!( + :id => 2, + :system_name => 'Corn', + :en_wikipedia_url => 'http://blah' + ) + @crop3 = Crop.create!( + :id => 3, + :system_name => 'Chard', + :en_wikipedia_url => 'http://blah' + ) + + # params to test default choice of crop and garden + # XXX this is very wrong! but we are still trying to figure out + # how to do it the right way. commiting as is, for now. + render :template => 'plantings/new', + :params => { :crop_id => 2, :garden_id => 2 } end it "renders new planting form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "form", :action => plantings_path, :method => "post" do assert_select "select#planting_garden_id", :name => "planting[garden_id]" assert_select "select#planting_crop_id", :name => "planting[crop_id]" @@ -37,5 +67,15 @@ describe "plantings/new" do assert_select "textarea#planting_description", :name => "planting[description]" end end + + it "selects a crop given in a param" do + assert_select "select#planting_crop_id", + :html => /option value="2" selected="selected"/ + end + + it "selects a garden given in a param" do + assert_select "select#planting_garden_id", + :html => /option value="2" selected="selected"/ + end end end