Compare commits

..

8 Commits

Author SHA1 Message Date
Brenda Wallace
25070f73d7 Merge pull request #1591 from Br3nda/feature/photos-helper
Use photos helpers to find photos
2018-03-26 18:14:32 +13:00
Brenda Wallace
2fe6012342 Merge branch 'dev' into feature/photos-helper 2018-03-23 15:03:01 +13:00
Brenda Wallace
511e89fa58 Merge branch 'dev' into feature/photos-helper 2018-03-19 13:39:02 +13:00
Brenda Wallace
f8e83cdb9c Merge branch 'dev' into feature/photos-helper 2018-03-16 14:54:53 +13:00
Brenda Wallace
ece7113c89 DRY photo helper spec 2018-03-15 22:56:32 +13:00
Brenda Wallace
8941c577e4 Specs for photos helper 2018-03-14 22:25:30 +13:00
Brenda Wallace
1bf1076076 Photos helper 2018-03-14 19:28:18 +13:00
Brenda Wallace
8906fd38ae For seeds, use planting photo if no seeds photo 2018-03-14 15:21:40 +13:00
13 changed files with 112 additions and 22 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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