diff --git a/app/assets/images/placeholder_150.png b/app/assets/images/placeholder_150.png new file mode 100644 index 000000000..c3d7f500c Binary files /dev/null and b/app/assets/images/placeholder_150.png differ diff --git a/app/models/crop.rb b/app/models/crop.rb index 105afe0b5..acb04d6d5 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -5,6 +5,7 @@ class Crop < ActiveRecord::Base has_many :scientific_names has_many :plantings + has_many :photos, :through => :plantings belongs_to :parent, :class_name => 'Crop' has_many :varieties, :class_name => 'Crop', :foreign_key => 'parent_id' @@ -38,4 +39,8 @@ class Crop < ActiveRecord::Base return plantings.count end + def default_photo + return photos.first + end + end 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..08fe34d90 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") @@ -50,4 +52,8 @@ class Planting < ActiveRecord::Base def to_s self.crop_system_name + " in " + self.location end + + def default_photo + return photos.first + end end diff --git a/app/views/comments/index.rss.haml b/app/views/comments/index.rss.haml index 5e4b227c5..db2f7a54f 100644 --- a/app/views/comments/index.rss.haml +++ b/app/views/comments/index.rss.haml @@ -20,4 +20,4 @@ %pubDate= comment.created_at.to_s(:rfc822) %link= post_url(comment.post) - %guid= post_url(comment.post) + %guid= comment_url(comment) diff --git a/app/views/crops/_thumbnail.html.haml b/app/views/crops/_thumbnail.html.haml index 5e4c6cb63..1337b8f0d 100644 --- a/app/views/crops/_thumbnail.html.haml +++ b/app/views/crops/_thumbnail.html.haml @@ -1,6 +1,6 @@ .thumbnail(style='height: 220px') - if crop - = link_to image_tag('http://placehold.it/150x150', :alt => '', :class => 'img-rounded'), crop + = link_to image_tag((crop.default_photo ? crop.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => crop.system_name, :class => 'img-rounded'), crop %p = link_to crop.system_name, crop - if crop.scientific_names.count > 0 @@ -12,6 +12,3 @@ %small Planted = pluralize(crop.plantings_count, "time") - - else - = image_tag('http://placehold.it/150x150', :alt => '', :class => 'img-rounded') - Sample crop 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/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 2e6147f02..362cd03a5 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,32 +1,37 @@ .well - %h4 - - if defined?(title) && title == 'owner' - = link_to planting.owner, planting.owner - - else - = link_to planting.crop.system_name, planting + .row-fluid + .span3 + = link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => '', :class => 'img-rounded'), planting + + .span9 + %h4 + - if defined?(title) && title == 'owner' + = link_to planting.owner, planting.owner + - else + = link_to planting.crop.system_name, planting - %p - Planted - - if planting.planted_at - = planting.planted_at - in - = link_to planting.location, planting.garden + %p + Planted + - if planting.planted_at + = planting.planted_at + in + = link_to planting.location, planting.garden - %p - - if planting.quantity - Quantity: - = planting.quantity - - else -   + %p + - if planting.quantity + Quantity: + = planting.quantity + - else +   - - if planting.description && ! defined?(hide_description) - %div - :markdown - #{ planting.description } + - if planting.description && ! defined?(hide_description) + %div + :markdown + #{ planting.description } - - if can? :edit, planting or can? :destroy, planting - %p - - if can? :edit, planting - =link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-mini' - - if can? :destroy, planting - =link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' + - if can? :edit, planting or can? :destroy, planting + %p + - if can? :edit, planting + =link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-mini' + - if can? :destroy, planting + =link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 137ffaaa4..9d33ecf17 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -30,24 +30,27 @@ .span6 .well - %h3 - = link_to @planting.crop, @planting.crop - - if can? :edit, @planting - = link_to 'Plant another', new_planting_path, :class => 'btn btn-primary' - - elsif can? :create, Planting - = link_to 'Plant this', new_planting_path, :class => 'btn btn-primary' + .row-fluid + .span4 + = link_to image_tag((@planting.crop.default_photo ? @planting.crop.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => '', :class => 'img-rounded'), @planting.crop + .span8 + %h3 + = link_to @planting.crop, @planting.crop + - if can? :edit, @planting + = link_to 'Plant another', new_planting_path, :class => 'btn btn-primary' + - elsif can? :create, Planting + = link_to 'Plant this', new_planting_path, :class => 'btn btn-primary' - %p - %b Scientific name: - = @planting.crop_default_scientific_name - %p - %b - Planted - = pluralize(@planting.crop_plantings_count, "time") - by #{Growstuff::Application.config.site_name} members + %p + %b Scientific name: + = @planting.crop_default_scientific_name + %p + %b + Planted + = pluralize(@planting.crop_plantings_count, "time") + by #{Growstuff::Application.config.site_name} members -%h2 - Notes +%h2 Notes :markdown #{ @planting.description != "" ? @planting.description : "No description given." } diff --git a/app/views/posts/show.rss.haml b/app/views/posts/show.rss.haml index 4f260a038..d5867dfb4 100644 --- a/app/views/posts/show.rss.haml +++ b/app/views/posts/show.rss.haml @@ -20,4 +20,4 @@ %pubDate= comment.created_at.to_s(:rfc822) %link= post_url(@post) - %guid= post_url(@post) + %guid= comment_url(comment) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index bea69ed01..3184d236e 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -69,4 +69,14 @@ describe Crop do @tomato.varieties.should eq [@roma] end end + + context 'photos' do + it 'has a default photo' do + @crop = FactoryGirl.create(:tomato) + @planting = FactoryGirl.create(:planting, :crop => @crop) + @photo = FactoryGirl.create(:photo) + @planting.photos << @photo + @crop.default_photo.should be_an_instance_of Photo + end + end end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 8a9a75e93..e5c2f777e 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -86,13 +86,28 @@ 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 + + it 'has a default photo' do + @planting.default_photo.should eq @photo + end end end diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb index 8f926129a..d61d108be 100644 --- a/spec/views/crops/index.html.haml_spec.rb +++ b/spec/views/crops/index.html.haml_spec.rb @@ -6,11 +6,10 @@ describe "crops/index" do page = 1 per_page = 2 total_entries = 2 + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) crops = WillPaginate::Collection.create(page, per_page, total_entries) do |pager| - pager.replace([ - FactoryGirl.create(:tomato), - FactoryGirl.create(:maize) - ]) + pager.replace([ @tomato, @maize ]) end assign(:crops, crops) end @@ -21,6 +20,14 @@ describe "crops/index" do assert_select "a", :text => "Tomato" end + it "shows photos where available" do + @planting = FactoryGirl.create(:planting, :crop => @tomato) + @photo = FactoryGirl.create(:photo) + @planting.photos << @photo + render + assert_select "img", :src => @photo.thumbnail_url + end + it "linkifies crop images" do render assert_select "img", :src => :tomato diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 0b34be50e..b1bfe26da 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -44,6 +44,14 @@ describe "crops/show" do rendered.should contain "Springfield Community Garden" end + it "shows photos where available" do + @planting = FactoryGirl.create(:planting, :crop => @crop) + @photo = FactoryGirl.create(:photo) + @planting.photos << @photo + render + assert_select "img", :src => @photo.thumbnail_url + end + context 'varieties' do before(:each) do @popcorn = FactoryGirl.create(:popcorn, :parent_id => @crop.id) 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