diff --git a/app/models/photo.rb b/app/models/photo.rb index ac67273ab..e65617c87 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -2,7 +2,9 @@ class Photo < ActiveRecord::Base attr_accessible :flickr_photo_id, :owner_id, :title, :license_name, :license_url, :thumbnail_url, :fullsize_url, :link_url belongs_to :owner, :class_name => 'Member' + has_and_belongs_to_many :plantings + before_destroy {|photo| photo.plantings.clear} default_scope order("created_at desc") diff --git a/app/models/planting.rb b/app/models/planting.rb index 1535c798b..0bf5f6eb6 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -7,7 +7,9 @@ class Planting < ActiveRecord::Base belongs_to :garden belongs_to :crop + has_and_belongs_to_many :photos + before_destroy {|planting| planting.photos.clear} default_scope order("created_at desc") diff --git a/app/views/photos/index.html.haml b/app/views/photos/index.html.haml index 09a8e1bd7..94ff1539d 100644 --- a/app/views/photos/index.html.haml +++ b/app/views/photos/index.html.haml @@ -1,4 +1,6 @@ -- content_for :title, "Photos" +- content_for :title, "Photo Gallery" + +%p This page shows all your photos. %div.pagination = page_entries_info @photos, :model => "photos" diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index 47a2b7ecc..d9166cfa1 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -15,6 +15,9 @@ %p = link_to "View on Flickr", @photo.link_url + - if can? :destroy, @photo + %p= link_to 'Delete Photo', @photo, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' + .span6 - if @photo.plantings.count > 0 %p This photo depicts: diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 8a9a75e93..e0d04d569 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -86,13 +86,24 @@ describe Planting do @planting.errors[:sunniness].should include("not valid is not a valid sunniness value") end + # we decided that all the tests for the planting/photo association would + # be done on this side, not on the photos side context 'photos' do - it 'has a photo' do + before(:each) do @planting = FactoryGirl.create(:planting) @photo = FactoryGirl.create(:photo) @planting.photos << @photo + end + + it 'has a photo' do @planting.photos.first.should eq @photo end + + it 'deletes association with photos when photo is deleted' do + @photo.destroy + @planting.reload + @planting.photos.should be_empty + end end end diff --git a/spec/views/photos/show.html.haml_spec.rb b/spec/views/photos/show.html.haml_spec.rb index fa03e08d6..18a201590 100644 --- a/spec/views/photos/show.html.haml_spec.rb +++ b/spec/views/photos/show.html.haml_spec.rb @@ -1,9 +1,14 @@ require 'spec_helper' describe "photos/show" do + before(:each) do + @member = FactoryGirl.create(:member) + controller.stub(:current_user) { @member } + end + context "CC-licensed photo" do before(:each) do - @photo = assign(:photo, FactoryGirl.create(:photo)) + @photo = assign(:photo, FactoryGirl.create(:photo, :owner => @member)) render end @@ -23,6 +28,10 @@ describe "photos/show" do it "shows a link to the original image" do assert_select "a", :href => @photo.link_url, :text => "View on Flickr" end + + it "has a delete button" do + assert_select "a[href=#{photo_path(@photo)}]", 'Delete Photo' + end end context "unlicensed photo" do @@ -34,6 +43,7 @@ describe "photos/show" do it "contains the phrase 'All rights reserved'" do rendered.should contain "All rights reserved" end + end context "linked to a planting" do