From 8906fd38ae56e455318806175e44f849c691846e Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Wed, 14 Mar 2018 15:21:40 +1300 Subject: [PATCH 1/5] For seeds, use planting photo if no seeds photo --- app/helpers/photos_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb index 50064f328..0e8985dd7 100644 --- a/app/helpers/photos_helper.rb +++ b/app/helpers/photos_helper.rb @@ -24,8 +24,10 @@ module PhotosHelper 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 else default_image end From 1bf10760764fd61413abec44c72115e5cb907580 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Wed, 14 Mar 2018 15:28:00 +1300 Subject: [PATCH 2/5] Photos helper --- app/helpers/photos_helper.rb | 22 ++++++++++++++++------ app/views/crops/_index_card.html.haml | 2 +- app/views/crops/_thumbnail.html.haml | 2 +- app/views/gardens/_photo.html.haml | 2 +- app/views/gardens/_thumbnail.html.haml | 2 +- app/views/harvests/_card.html.haml | 2 +- app/views/harvests/_thumbnail.html.haml | 2 +- app/views/plantings/_card.html.haml | 2 +- app/views/plantings/_thumbnail.html.haml | 2 +- app/views/seeds/_card.html.haml | 2 +- app/views/seeds/_thumbnail.html.haml | 2 +- 11 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb index 0e8985dd7..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,15 +19,17 @@ 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 @@ -27,15 +37,15 @@ module PhotosHelper if seed.default_photo.present? seed.default_photo.thumbnail_url elsif seed.crop.default_photo.present? - seed.crop.default_photo + 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/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 From 8941c577e4474663fd64cf782e02b8e09d3efdc8 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Wed, 14 Mar 2018 22:25:30 +1300 Subject: [PATCH 3/5] Specs for photos helper --- app/models/crop.rb | 11 ++-- spec/helpers/photos_helper_spec.rb | 88 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 spec/helpers/photos_helper_spec.rb 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/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb new file mode 100644 index 000000000..60ba61fb4 --- /dev/null +++ b/spec/helpers/photos_helper_spec.rb @@ -0,0 +1,88 @@ +require 'rails_helper' + +describe PhotosHelper do + let(:crop) { FactoryBot.create :crop } + let(:crop_photo) { FactoryBot.create :photo, thumbnail_url: 'crop.jpg' } + let(:garden) { FactoryBot.create :garden } + let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg') } + let(:planting) { FactoryBot.create :planting } + let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg') } + let(:harvest) { FactoryBot.create :harvest } + let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg') } + let(:seed) { FactoryBot.create :seed } + 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 do + planting.photos << planting_photo + crop.plantings << planting + end + it "uses planting photos" do + is_expected.to eq planting_photo.thumbnail_url + end + end + + describe "with a harvest photos" do + before do + harvest.photos << harvest_photo + crop.harvests << harvest + end + it "uses harvest photos" do + is_expected.to eq harvest_photo.thumbnail_url + end + end + + describe "uses seed photo" do + before do + seed.photos << seed_photo + crop.seeds << seed + end + 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 From ece7113c89c6d6639f6312a619d4ea4ab4abf960 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 15 Mar 2018 22:56:32 +1300 Subject: [PATCH 4/5] DRY photo helper spec --- spec/helpers/photos_helper_spec.rb | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb index 60ba61fb4..589cf8104 100644 --- a/spec/helpers/photos_helper_spec.rb +++ b/spec/helpers/photos_helper_spec.rb @@ -2,14 +2,14 @@ require 'rails_helper' describe PhotosHelper do let(:crop) { FactoryBot.create :crop } - let(:crop_photo) { FactoryBot.create :photo, thumbnail_url: 'crop.jpg' } + let(:garden) { FactoryBot.create :garden } let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg') } - let(:planting) { FactoryBot.create :planting } + let(:planting) { FactoryBot.create :planting, crop: crop } let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg') } - let(:harvest) { FactoryBot.create :harvest } + let(:harvest) { FactoryBot.create :harvest, crop: crop } let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg') } - let(:seed) { FactoryBot.create :seed } + let(:seed) { FactoryBot.create :seed, crop: crop } let(:seed_photo) { FactoryBot.create(:photo, thumbnail_url: 'seed.jpg') } describe "crops" do @@ -18,30 +18,21 @@ describe PhotosHelper do it { is_expected.to eq 'placeholder_150.png' } describe "with a planting" do - before do - planting.photos << planting_photo - crop.plantings << planting - end + 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 do - harvest.photos << harvest_photo - crop.harvests << harvest - end + 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 do - seed.photos << seed_photo - crop.seeds << seed - end + before { seed.photos << seed_photo } it "uses seed photos" do is_expected.to eq seed_photo.thumbnail_url end From c80685bf0d9d201fa608b4e7a773bc5150016c99 Mon Sep 17 00:00:00 2001 From: deppbot Date: Thu, 22 Mar 2018 09:14:19 +0800 Subject: [PATCH 5/5] Security Update on 2018-03-20 (#1597) * Security Update on 2018-03-20 * Ordered gems, to please rubocop --- Gemfile | 1 + Gemfile.lock | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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)