diff --git a/Gemfile b/Gemfile index c5c074891..55be65010 100644 --- a/Gemfile +++ b/Gemfile @@ -155,4 +155,5 @@ end group :travis do gem 'platform-api' end +gem 'loofah', '>= 2.2.1' gem 'rack-protection', '>= 2.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 48837e792..b15ea0120 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -300,7 +300,7 @@ GEM rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - loofah (2.1.1) + loofah (2.2.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) lumberjack (1.0.12) @@ -595,6 +595,7 @@ DEPENDENCIES kaminari leaflet-rails letter_opener + loofah (>= 2.2.1) memcachier newrelic_rpm omniauth (~> 1.3) diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb index 50064f328..b77f35bd9 100644 --- a/app/helpers/photos_helper.rb +++ b/app/helpers/photos_helper.rb @@ -3,7 +3,15 @@ module PhotosHelper if crop.default_photo.present? crop.default_photo.thumbnail_url else - default_image + placeholder_image + end + end + + def garden_image_path(garden) + if garden.default_photo.present? + garden.default_photo.thumbnail_url + else + placeholder_image end end @@ -11,29 +19,33 @@ module PhotosHelper if planting.photos.present? planting.photos.first.thumbnail_url else - default_image + placeholder_image end end def harvest_image_path(harvest) if harvest.photos.present? harvest.photos.first.thumbnail_url + elsif harvest.planting.present? && harvest.planting.photos.present? + harvest.planting.photos.first.thumbnail_url else - default_image + placeholder_image end end def seed_image_path(seed) - if seed.default_photo + if seed.default_photo.present? seed.default_photo.thumbnail_url + elsif seed.crop.default_photo.present? + seed.crop.default_photo.thumbnail_url else - default_image + placeholder_image end end private - def default_image + def placeholder_image 'placeholder_150.png' end end diff --git a/app/models/crop.rb b/app/models/crop.rb index 15787f6f1..682610436 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -120,12 +120,7 @@ class Crop < ActiveRecord::Base # later we can choose a default photo based on different criteria, # eg. popularity def default_photo - # most recent photo - return photos.order(created_at: :desc).first if photos.any? - - # Crop has no photos? Look for the most recent harvest with a photo. - harvest_with_photo = Harvest.where(crop_id: id).joins(:photos).order('harvests.id DESC').limit(1).first - harvest_with_photo.photos.first if harvest_with_photo + first_photo(:plantings) || first_photo(:harvests) || first_photo(:seeds) end # returns hash indicating whether this crop is grown in @@ -242,4 +237,8 @@ class Crop < ActiveRecord::Base return unless reason_for_rejection == "other" && rejection_notes.blank? errors.add(:rejection_notes, "must be added if the reason for rejection is \"other\"") end + + def first_photo(type) + Photo.joins(type).where("#{type}": { crop_id: id }).order("photos.created_at DESC").first + end end diff --git a/app/views/crops/_index_card.html.haml b/app/views/crops/_index_card.html.haml index 39b9e8da4..99b4a1a7e 100644 --- a/app/views/crops/_index_card.html.haml +++ b/app/views/crops/_index_card.html.haml @@ -1,7 +1,7 @@ .well .row .col-md-4 - = link_to image_tag((crop.default_photo ? crop.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(crop_image_path(crop), alt: '', class: 'img crop-image'), crop diff --git a/app/views/crops/_thumbnail.html.haml b/app/views/crops/_thumbnail.html.haml index c83a1a0c6..77793ef22 100644 --- a/app/views/crops/_thumbnail.html.haml +++ b/app/views/crops/_thumbnail.html.haml @@ -2,7 +2,7 @@ .thumbnail .crop-thumbnail - if crop - = link_to image_tag((crop.default_photo ? crop.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(crop_image_path(crop), alt: crop.name, class: 'img'), crop .cropinfo diff --git a/app/views/gardens/_photo.html.haml b/app/views/gardens/_photo.html.haml index caf5c04d1..b6d301d2e 100644 --- a/app/views/gardens/_photo.html.haml +++ b/app/views/gardens/_photo.html.haml @@ -1,3 +1,3 @@ -= link_to image_tag((garden.default_photo ? garden.default_photo.thumbnail_url : 'placeholder_150.png'), += link_to image_tag(garden_image_path(garden), alt: garden.name, class: 'img-responsive'), garden_path(garden) diff --git a/app/views/gardens/_thumbnail.html.haml b/app/views/gardens/_thumbnail.html.haml index 56c8c9614..a2e4ea7eb 100644 --- a/app/views/gardens/_thumbnail.html.haml +++ b/app/views/gardens/_thumbnail.html.haml @@ -8,7 +8,7 @@ .panel-body{ id: "gardens_panel_body" } .row .col-md-4 - = link_to image_tag((garden.default_photo ? garden.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(garden_image_path(garden), alt: garden.name, class: 'img'), garden_path(garden) .col-md-8 diff --git a/app/views/harvests/_card.html.haml b/app/views/harvests/_card.html.haml index 1a12128b0..52cfb3627 100644 --- a/app/views/harvests/_card.html.haml +++ b/app/views/harvests/_card.html.haml @@ -8,7 +8,7 @@ .panel-body .row .col-md-4 - = link_to image_tag((harvest.default_photo ? harvest.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(harvest_image_path(harvest), alt: harvest.crop.name, class: 'img'), harvest .col-md-8 diff --git a/app/views/harvests/_thumbnail.html.haml b/app/views/harvests/_thumbnail.html.haml index d7fec2a7a..4344aed7d 100644 --- a/app/views/harvests/_thumbnail.html.haml +++ b/app/views/harvests/_thumbnail.html.haml @@ -1,7 +1,7 @@ .thumbnail .harvest-thumbnail - if harvest - = link_to image_tag((harvest.default_photo ? harvest.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(harvest_image_path(harvest), alt: harvest.crop.name, class: 'img'), harvest .harvestinfo diff --git a/app/views/plantings/_card.html.haml b/app/views/plantings/_card.html.haml index bb6d77347..0f9518e6c 100644 --- a/app/views/plantings/_card.html.haml +++ b/app/views/plantings/_card.html.haml @@ -8,7 +8,7 @@ .panel-body .row .col-xs-12.col-md-5 - = link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(planting_image_path(planting), alt: planting.crop_id, class: 'img img-responsive'), planting .col-xs-12.col-md-7 diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 15ff2553e..e34c653da 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,7 +1,7 @@ .thumbnail .planting-thumbnail - if planting - = link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting .plantinginfo diff --git a/app/views/seeds/_card.html.haml b/app/views/seeds/_card.html.haml index 532db8d6c..8cb70f64f 100644 --- a/app/views/seeds/_card.html.haml +++ b/app/views/seeds/_card.html.haml @@ -8,7 +8,7 @@ .panel-body .row .col-md-4 - = link_to image_tag((seed.crop.default_photo ? seed.crop.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(seed_image_path(seed), alt: seed.crop.name, class: 'img'), seed.crop .col-md-8 diff --git a/app/views/seeds/_thumbnail.html.haml b/app/views/seeds/_thumbnail.html.haml index f985b4d16..38aa66d65 100644 --- a/app/views/seeds/_thumbnail.html.haml +++ b/app/views/seeds/_thumbnail.html.haml @@ -1,6 +1,6 @@ .thumbnail .seed-thumbnail - = link_to image_tag((seed.default_photo ? seed.default_photo.thumbnail_url : 'placeholder_150.png'), + = link_to image_tag(seed_image_path(seed), alt: seed.crop.name, class: 'img'), seed_path(seed) .seedinfo diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb new file mode 100644 index 000000000..589cf8104 --- /dev/null +++ b/spec/helpers/photos_helper_spec.rb @@ -0,0 +1,79 @@ +require 'rails_helper' + +describe PhotosHelper do + let(:crop) { FactoryBot.create :crop } + + let(:garden) { FactoryBot.create :garden } + let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg') } + let(:planting) { FactoryBot.create :planting, crop: crop } + let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg') } + let(:harvest) { FactoryBot.create :harvest, crop: crop } + let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg') } + let(:seed) { FactoryBot.create :seed, crop: crop } + let(:seed_photo) { FactoryBot.create(:photo, thumbnail_url: 'seed.jpg') } + + describe "crops" do + subject { crop_image_path(crop) } + + it { is_expected.to eq 'placeholder_150.png' } + + describe "with a planting" do + before { planting.photos << planting_photo } + it "uses planting photos" do + is_expected.to eq planting_photo.thumbnail_url + end + end + + describe "with a harvest photos" do + before { harvest.photos << harvest_photo } + it "uses harvest photos" do + is_expected.to eq harvest_photo.thumbnail_url + end + end + + describe "uses seed photo" do + before { seed.photos << seed_photo } + it "uses seed photos" do + is_expected.to eq seed_photo.thumbnail_url + end + end + end + + describe "gardens" do + subject { garden_image_path(garden) } + it { is_expected.to eq 'placeholder_150.png' } + + describe "uses garden's own photo" do + before { garden.photos << garden_photo } + it { is_expected.to eq garden_photo.thumbnail_url } + end + end + + describe 'plantings' do + subject { planting_image_path(planting) } + it { is_expected.to eq 'placeholder_150.png' } + describe "uses planting's own photo" do + before { planting.photos << planting_photo } + it { is_expected.to eq planting_photo.thumbnail_url } + end + end + + describe 'harvests' do + subject { harvest_image_path(harvest) } + it { is_expected.to eq 'placeholder_150.png' } + describe "uses harvest's own photo" do + before { harvest.photos << harvest_photo } + it { is_expected.to eq harvest_photo.thumbnail_url } + end + end + + describe 'seeds' do + subject { seed_image_path(seed) } + it { is_expected.to eq 'placeholder_150.png' } + + describe "uses seed's own photo" do + before { seed.photos << seed_photo } + it { is_expected.to eq seed_photo.thumbnail_url } + end + end +end