From 02daab4fc15a95bf7472d0b640fd72d2ed9631db Mon Sep 17 00:00:00 2001 From: Skud Date: Sun, 23 Dec 2012 10:01:19 +1100 Subject: [PATCH] added crop_id and garden_id params to controller --- app/controllers/plantings_controller.rb | 4 ++ spec/controllers/plantings_controller_spec.rb | 59 +++++++++++-------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index 430682c2a..72e3b95c3 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -27,6 +27,10 @@ class PlantingsController < ApplicationController def new @planting = Planting.new + # using find_by_id here because it returns nil, unlike find + @crop = Crop.find_by_id(params[:crop_id]) + @garden = Garden.find_by_id(params[:garden_id]) + respond_to do |format| format.html # new.html.erb format.json { render json: @planting } diff --git a/spec/controllers/plantings_controller_spec.rb b/spec/controllers/plantings_controller_spec.rb index c56a89c7d..b2080dc68 100644 --- a/spec/controllers/plantings_controller_spec.rb +++ b/spec/controllers/plantings_controller_spec.rb @@ -1,35 +1,11 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe PlantingsController do - # This should return the minimal set of attributes required to create a valid - # Planting. As you add validations to Planting, be sure to - # update the return value of this method accordingly. def valid_attributes { :garden_id => 1, :crop_id => 1 } end - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # PlantingsController. Be sure to keep this updated too. def valid_session {} end @@ -55,6 +31,41 @@ describe PlantingsController do get :new, {}, valid_session assigns(:planting).should be_a_new(Planting) end + + it "picks up crop from params" do + crop = Crop.create!( + :system_name => 'Maize', + :en_wikipedia_url => 'http://blah' + ) + get :new, {:crop_id => crop.id}, valid_session + assigns(:crop).should eq(crop) + end + + it "doesn't die if no crop specified" do + get :new, {}, valid_session + assigns(:crop).should be_nil + end + + it "picks up garden from params" do + user = User.create!( + :username => 'blah', + :password => 'blahblah', + :email => 'blah@example.com', + :tos_agreement => true + ) + garden = Garden.create!( + :name => 'blah', + :user_id => user.id + ) + get :new, {:garden_id => garden.id}, valid_session + assigns(:garden).should eq(garden) + end + + it "doesn't die if no garden specified" do + get :new, {}, valid_session + assigns(:garden).should be_nil + end + end describe "GET edit" do