diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 8932ebae8..f4004ad73 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -64,32 +64,32 @@ class PhotosController < ApplicationController @photo.owner_id = current_member.id @photo.set_flickr_metadata - # several models can have photos. we need to know what model and the id - # for the entry to attach the photo to - valid_models = ["planting", "harvest"] + # several models can have photos. we need to know what model and the id + # for the entry to attach the photo to + valid_models = ["planting", "harvest"] if params[:type] if valid_models.include?(params[:type]) - if params[:id] - item = params[:type].camelcase.constantize.find_by_id(params[:id]) - if item - if item.owner.id == current_member.id - # This syntax is weird, so just know that it means this: - # @photo.harvests << item unless @photo.harvests.include?(item) - # but with the correct many-to-many relationship automatically referenced - (@photo.send "#{params[:type]}s") << item unless (@photo.send "#{params[:type]}s").include?(item) - else - flash[:alert] = "You must own both the #{params[:type]} and the photo." - end - else - flash[:alert] = "Couldn't find #{params[:type]} to connect to photo." - end - else - flash[:alert] = "Missing id parameter" - end - else - flash[:alert] = "Cannot attach photos to #{params[:type]}" - end - else + if params[:id] + item = params[:type].camelcase.constantize.find_by_id(params[:id]) + if item + if item.owner.id == current_member.id + # This syntax is weird, so just know that it means this: + # @photo.harvests << item unless @photo.harvests.include?(item) + # but with the correct many-to-many relationship automatically referenced + (@photo.send "#{params[:type]}s") << item unless (@photo.send "#{params[:type]}s").include?(item) + else + flash[:alert] = "You must own both the #{params[:type]} and the photo." + end + else + flash[:alert] = "Couldn't find #{params[:type]} to connect to photo." + end + else + flash[:alert] = "Missing id parameter" + end + else + flash[:alert] = "Cannot attach photos to #{params[:type]}" + end + else flash[:alert] = "Missing type parameter" end diff --git a/app/models/photo.rb b/app/models/photo.rb index aa5a624d6..4f0a0160f 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -6,8 +6,8 @@ class Photo < ActiveRecord::Base has_and_belongs_to_many :plantings has_and_belongs_to_many :harvests before_destroy do |photo| - photo.plantings.clear - photo.harvests.clear + photo.plantings.clear + photo.harvests.clear end default_scope order("created_at desc") diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 92d55dfaa..eb51fab96 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -6,8 +6,7 @@ Devise.setup do |config| # note that it will be overwritten if you use your own mailer class with default "from" parameter. config.mailer_sender = "Growstuff " - #config.secret_key = ENV['RAILS_SECRET_TOKEN'] - config.secret_key = '6ccb49df7c02ce710c37ec3eadd81ec65e732708bd859ce6076f42593f0cff186b7c35be2fd195f46c9e923296ee07bf309eb33866fe1ea7d9ffeb88f367489c' + config.secret_key = ENV['RAILS_SECRET_TOKEN'] # Configure the class responsible to send e-mails. # config.mailer = "Devise::Mailer" diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index 4370e626f..1c3b47780 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -39,13 +39,13 @@ describe PhotosController do it "assigns a planting id" do get :new, { :type => "planting", :id => 5 } assigns(:id).should eq "5" - assigns(:type).should eq "planting" + assigns(:type).should eq "planting" end it "assigns a harvest id" do get :new, { :type => "harvest", :id => 5 } assigns(:id).should eq "5" - assigns(:type).should eq "harvest" + assigns(:type).should eq "harvest" end it "assigns the current set as @current_set" do @@ -76,7 +76,7 @@ describe PhotosController do planting = FactoryGirl.create(:planting, :garden => garden, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "planting", + :type => "planting", :id => planting.id } Photo.last.plantings.first.should eq planting end @@ -88,10 +88,10 @@ describe PhotosController do planting = FactoryGirl.create(:planting, :garden => garden, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "planting", + :type => "planting", :id => planting.id } post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "planting", + :type => "planting", :id => planting.id } Photo.last.plantings.count.should eq 1 end @@ -102,7 +102,7 @@ describe PhotosController do harvest = FactoryGirl.create(:harvest, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "harvest", + :type => "harvest", :id => harvest.id } Photo.last.harvests.first.should eq harvest end @@ -113,14 +113,14 @@ describe PhotosController do harvest = FactoryGirl.create(:harvest, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "harvest", + :type => "harvest", :id => harvest.id } post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "harvest", + :type => "harvest", :id => harvest.id } Photo.last.harvests.count.should eq 1 - end - end + end + end describe "for the second time" do it "does not add a photo twice" do @@ -141,7 +141,7 @@ describe PhotosController do planting = FactoryGirl.create(:planting, :garden => garden, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "planting", + :type => "planting", :id => planting.id } Photo.last.plantings.first.should eq planting end @@ -152,7 +152,7 @@ describe PhotosController do harvest = FactoryGirl.create(:harvest, :owner => member) photo = FactoryGirl.create(:photo, :owner => member) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "harvest", + :type => "harvest", :id => harvest.id } Photo.last.harvests.first.should eq harvest end @@ -164,7 +164,7 @@ describe PhotosController do planting = FactoryGirl.create(:planting) photo = FactoryGirl.create(:photo) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "planting", + :type => "planting", :id => planting.id } Photo.last.plantings.first.should_not eq planting end @@ -174,7 +174,7 @@ describe PhotosController do harvest = FactoryGirl.create(:harvest) photo = FactoryGirl.create(:photo) post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id }, - :type => "harvest", + :type => "harvest", :id => harvest.id } Photo.last.harvests.first.should_not eq harvest end