Merge remote-tracking branch 'upstream/dev' into post-comments

Conflicts:
	db/schema.rb
This commit is contained in:
Brenda Wallace
2019-09-26 21:14:12 +12:00
14 changed files with 138 additions and 97 deletions

View File

@@ -85,7 +85,7 @@ GEM
bootstrap-kaminari-views (0.0.5)
kaminari (>= 0.13)
rails (>= 3.1)
bootstrap_form (4.2.0)
bootstrap_form (4.3.0)
actionpack (>= 5.0)
activemodel (>= 5.0)
builder (3.2.3)
@@ -182,10 +182,10 @@ GEM
erubis (2.7.0)
excon (0.64.0)
execjs (2.7.0)
factory_bot (5.0.2)
factory_bot (5.1.0)
activesupport (>= 4.2.0)
factory_bot_rails (5.0.2)
factory_bot (~> 5.0.2)
factory_bot_rails (5.1.0)
factory_bot (~> 5.1.0)
railties (>= 4.2.0)
faker (2.4.0)
i18n (~> 1.6.0)
@@ -195,7 +195,7 @@ GEM
figaro (1.1.1)
thor (~> 0.14)
flickraw (0.9.10)
font-awesome-sass (5.10.2)
font-awesome-sass (5.11.2)
sassc (>= 1.11)
friendly_id (5.2.5)
activerecord (>= 4.0.0)
@@ -323,7 +323,7 @@ GEM
mini_magick (4.9.4)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest (5.12.0)
moneta (1.0.0)
multi_json (1.11.3)
multi_xml (0.6.0)
@@ -369,7 +369,7 @@ GEM
popper_js (1.14.5)
procto (0.0.3)
public_suffix (4.0.1)
puma (4.1.1)
puma (4.2.0)
nio4r (~> 2.0)
rack (2.0.7)
rack-protection (2.0.7)

View File

@@ -1,3 +1,7 @@
.crop-icon {
height: 1em;
}
.planting {
.crop-card {
height: 100%;
@@ -30,3 +34,30 @@
.remove-altname-row {
display: none;
}
.crop-chip {
background-color: lighten($green, 20%);
border-radius: 25px;
display: inline-block;
font-size: 1em;
height: 30px;
line-height: 30px;
margin: .2em;
padding: 0 25px;
a {
color: $white;
}
img {
border-radius: 50%;
float: left;
height: 30px;
margin: 0 10px 0 -25px;
width: 30px;
}
}
.crop-hero-photo {
max-height: 300px;
}

View File

@@ -29,8 +29,8 @@ class PhotosController < ApplicationController
def new
@photo = Photo.new
@item = item_to_link_to
@type = item_type
@id = item_id
@type = params[:type]
@id = params[:id]
retrieve_from_flickr
respond_with @photo
end
@@ -63,31 +63,17 @@ class PhotosController < ApplicationController
private
#
# Params
def item_id
params[:id]
end
def item_type
params[:type]
end
def flickr_photo_id_param
params[:photo][:flickr_photo_id]
end
def photo_params
params.require(:photo).permit(:flickr_photo_id, :title, :license_name,
params.require(:photo).permit(:source_id, :source, :title, :license_name,
:license_url, :thumbnail_url, :fullsize_url, :link_url)
end
# Item with photos attached
def item_to_link_to
raise "No item id provided" if item_id.nil?
raise "No item type provided" if item_type.nil?
raise "No item id provided" if params[:id].nil?
raise "No item type provided" if params[:type].nil?
item_class = item_type.capitalize
item_class = params[:type].capitalize
raise "Photos not supported" unless Photo::PHOTO_CAPABLE.include? item_class
item_class.constantize.find(params[:id])
@@ -96,8 +82,11 @@ class PhotosController < ApplicationController
#
# Flickr retrieval
def find_or_create_photo_from_flickr_photo
photo = Photo.find_by(flickr_photo_id: flickr_photo_id_param)
photo ||= Photo.new(photo_params)
photo = Photo.find_or_initialize_by(
source_id: photo_params[:source_id],
source: 'flickr'
)
photo.update(photo_params)
photo.owner_id = current_member.id
photo.set_flickr_metadata!
photo

View File

@@ -24,7 +24,7 @@ class Photo < ApplicationRecord
# for easier stubbing and testing.
def flickr_metadata
flickr = owner.flickr
info = flickr.photos.getInfo(photo_id: flickr_photo_id)
info = flickr.photos.getInfo(photo_id: source_id)
licenses = flickr.photos.licenses.getInfo
license = licenses.find { |l| l.id == info.license }
{
@@ -63,4 +63,8 @@ class Photo < ApplicationRecord
def to_s
"#{title} by #{owner.login_name}"
end
def flickr_photo_id
source_id if source == 'flickr'
end
end

View File

@@ -0,0 +1,6 @@
- if crop.approved? || can?(:wrangle, Crop)
.crop-chip
= link_to crop do
= crop.name
- unless crop.approved?
%badge.badge-warning= crop.approval_status

View File

@@ -1,31 +1,32 @@
%h4 This photo depicts:
%p
%ul.associations
- @photo.posts.each do |post|
%li
= post_icon
= link_to post.subject, post
= render "association_delete_button", photo: @photo, type: 'post', thing: post
- @photo.plantings.each do |planting|
%li
= planting_icon
= link_to t('photos.show.planting', planting: planting.to_s, owner: planting.owner.to_s), planting_path(planting)
= render "association_delete_button", photo: @photo, type: 'planting', thing: planting
- if @photo.associations?
%h4 This photo depicts:
%p
%ul.associations
- @photo.posts.each do |post|
%li
= post_icon
= link_to post.subject, post
= render "association_delete_button", photo: @photo, type: 'post', thing: post
- @photo.plantings.each do |planting|
%li
= planting_icon
= link_to t('photos.show.planting', planting: planting.to_s, owner: planting.owner.to_s), planting_path(planting)
= render "association_delete_button", photo: @photo, type: 'planting', thing: planting
- @photo.harvests.each do |harvest|
%li
= harvest_icon
= link_to t('photos.show.harvest', crop: harvest.crop.name, owner: harvest.owner.to_s), harvest_path(harvest)
= render "association_delete_button", photo: @photo, type: 'harvest', thing: harvest
- @photo.harvests.each do |harvest|
%li
= harvest_icon
= link_to t('photos.show.harvest', crop: harvest.crop.name, owner: harvest.owner.to_s), harvest_path(harvest)
= render "association_delete_button", photo: @photo, type: 'harvest', thing: harvest
- @photo.gardens.each do |garden|
%li
= garden_icon
= link_to t('photos.show.garden', garden: garden.to_s, owner: garden.owner.to_s), garden_path(garden)
= render "association_delete_button", photo: @photo, type: 'garden', thing: garden
- @photo.gardens.each do |garden|
%li
= garden_icon
= link_to t('photos.show.garden', garden: garden.to_s, owner: garden.owner.to_s), garden_path(garden)
= render "association_delete_button", photo: @photo, type: 'garden', thing: garden
- @photo.seeds.each do |seed|
%li
= seed_icon
= link_to t('photos.show.seed', seed: seed.to_s, owner: seed.owner.to_s), seed_path(seed)
= render "association_delete_button", photo: @photo, type: 'seed', thing: seed
- @photo.seeds.each do |seed|
%li
= seed_icon
= link_to t('photos.show.seed', seed: seed.to_s, owner: seed.owner.to_s), seed_path(seed)
= render "association_delete_button", photo: @photo, type: 'seed', thing: seed

View File

@@ -1,5 +1,5 @@
.card.photo-card{id: "photo-#{photo.id}"}
= link_to image_tag(photo.fullsize_url, alt: photo.title, class: 'img img-card'), photo
= link_to image_tag(photo.source == 'flickr' ? photo.fullsize_url : photo.thumbnail_url, alt: photo.title, class: 'img img-card'), photo
.card-body
%h5.ellipsis= link_to photo.title, photo
%i by #{link_to photo.owner, photo.owner}

View File

@@ -30,12 +30,9 @@
- @photos.each do |photo|
.col-md-2.col-6
.card
= link_to image_tag(FlickRaw.url_z(photo), alt: '', class: 'img img-card',
width: 150, height: 150),
photos_path(photo: { flickr_photo_id: photo.id }, type: @type, id: @id),
method: :post
.card-body
%p.photo-title= photo.title
= link_to photos_path(photo: { source_id: photo.id, source: 'flickr' }, id: @id, type: @type ), method: :post do
= image_tag(FlickRaw.url_z(photo), alt: photo.title, class: 'img img-card')
.card-body= photo.title
.row.pagination
.col-md-12= will_paginate @photos

View File

@@ -12,27 +12,28 @@
%li.breadcrumb-item.active= link_to @photo, @photo
.row{id: "photo-#{@photo.id}"}
.col-md-8
%h1.text-center.ellipsis=@photo.title
.col-md-9
%h1.text-center.ellipsis
= photo_icon
= @photo.title
%p.text-center
= image_tag(@photo.fullsize_url, alt: @photo.title, class: 'rounded img-fluid shadow-sm')
.col-md-4
= image_tag(@photo.fullsize_url, alt: @photo.title, class: 'rounded img-responsive shadow-sm')
.col-md-3
%p
= render 'photos/actions', photo: @photo
= link_to "View on Flickr", @photo.link_url, class: 'btn'
= link_to @photo.link_url, class: 'btn btn-info' do
- if @photo.source == 'flickr'
= icon 'fab', 'flickr'
View on #{@photo.source.titleize}
%span.btn= render 'photos/likes', photo: @photo
- if @crops.size.positive?
.index-cards
- @crops.each do |crop|
= render 'crops/thumbnail', crop: crop
- @crops.each do |crop|
= render 'crops/tiny', crop: crop
%p
= photo_icon
%strong Photo by
= link_to @photo.owner, @photo.owner
Taken on #{@photo.date_taken}
= render 'members/tiny', member: @photo.owner
- if @photo.date_taken.present?
Taken on #{I18n.l @photo.date_taken.to_date}
%p
%strong License:
- if @photo.license_url
@@ -40,5 +41,4 @@
- else
= @photo.license_name
- if @photo.associations?
= render "associations", photo: @photo
= render "associations", photo: @photo

View File

@@ -0,0 +1,7 @@
class AddPhotoSource < ActiveRecord::Migration[5.2]
def change
add_column :photos, :source, :string
rename_column :photos, :flickr_photo_id, :source_id
Photo.update_all(source: 'flickr') # rubocop:disable Rails/SkipsModelValidations
end
end

View File

@@ -416,9 +416,10 @@ ActiveRecord::Schema.define(version: 2019_09_21_211652) do
t.string "license_name", null: false
t.string "license_url"
t.string "link_url", null: false
t.string "flickr_photo_id"
t.string "source_id"
t.datetime "date_taken"
t.integer "likes_count", default: 0
t.string "source"
end
create_table "photos_plantings", id: false, force: :cascade do |t|

View File

@@ -90,7 +90,7 @@ describe PhotosController do
it "attaches the photo to a planting" do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id },
photo: { source_id: photo.source_id, source: 'flickr' },
type: "planting", id: planting.id
}
expect(flash[:alert]).not_to be_present
@@ -100,10 +100,10 @@ describe PhotosController do
describe "doesn't attach a photo to a planting twice" do
before do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "planting", id: planting.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "planting", id: planting.id
}
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "planting", id: planting.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "planting", id: planting.id
}
end
@@ -112,17 +112,17 @@ describe PhotosController do
end
it "attaches the photo to a harvest" do
post :create, params: { photo: { flickr_photo_id: photo.flickr_photo_id }, type: "harvest", id: harvest.id }
post :create, params: { photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: harvest.id }
expect(flash[:alert]).not_to be_present
Photo.last.harvests.first.should eq harvest
end
it "doesn't attach a photo to a harvest twice" do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "harvest", id: harvest.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: harvest.id
}
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "harvest", id: harvest.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: harvest.id
}
expect(flash[:alert]).not_to be_present
Photo.last.harvests.size.should eq 1
@@ -132,7 +132,7 @@ describe PhotosController do
comment = FactoryBot.create(:comment)
expect do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "comment", id: comment.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "comment", id: comment.id
}
end.to raise_error
end
@@ -140,7 +140,7 @@ describe PhotosController do
describe "for the second time" do
let(:planting) { FactoryBot.create :planting, owner: member }
let(:valid_params) { { photo: { flickr_photo_id: 1 }, id: planting.id, type: 'planting' } }
let(:valid_params) { { photo: { source_id: 1 }, id: planting.id, type: 'planting' } }
it "does not add a photo twice" do
expect { post :create, params: valid_params }.to change(Photo, :count).by(1)
@@ -154,14 +154,14 @@ describe PhotosController do
it "creates the planting/photo link" do
planting = FactoryBot.create(:planting, garden: garden, owner: member)
photo = FactoryBot.create(:photo, owner: member)
post :create, params: { photo: { flickr_photo_id: photo.flickr_photo_id }, type: "planting", id: planting.id }
post :create, params: { photo: { source_id: photo.source_id, source: 'flickr' }, type: "planting", id: planting.id }
expect(flash[:alert]).not_to be_present
expect(Photo.last.plantings.first).to eq planting
end
describe "creates the harvest/photo link" do
before do
post :create, params: { photo: { flickr_photo_id: photo.flickr_photo_id }, type: "harvest", id: harvest.id }
post :create, params: { photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: harvest.id }
end
it { expect(flash[:alert]).not_to be_present }
@@ -177,7 +177,8 @@ describe PhotosController do
another_planting = FactoryBot.create(:planting)
expect do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "planting", id: another_planting.id
photo: { source_id: photo.source_id, source: 'flickr' },
type: "planting", id: another_planting.id
}
end.to raise_error(ActiveRecord::RecordInvalid)
Photo.last.plantings.first.should_not eq another_planting
@@ -188,7 +189,7 @@ describe PhotosController do
another_harvest = FactoryBot.create(:harvest)
expect do
post :create, params: {
photo: { flickr_photo_id: photo.flickr_photo_id }, type: "harvest", id: another_harvest.id
photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: another_harvest.id
}
end.to raise_error(ActiveRecord::RecordInvalid)
Photo.last.harvests.first.should_not eq another_harvest

View File

@@ -3,7 +3,8 @@
FactoryBot.define do
factory :photo do
owner
flickr_photo_id { 1 }
source { 'flickr' }
source_id { 1 }
title { Faker::Movies::HarryPotter.quote }
license_name { "CC-BY" }
license_url { "http://example.com/license.html" }

View File

@@ -21,22 +21,25 @@ describe "photos/show" do
end
it "links to the owner's profile" do
assert_select "a", href: @photo.owner
expect(rendered).to have_link(href: member_path(@photo.owner))
end
it "shows a link to the original image" do
assert_select "a", href: @photo.link_url, text: "View on Flickr"
expect(rendered).to have_link 'View on Flickr', href: @photo.link_url
end
it "links to harvest" do
assert_select "a", href: harvest_path(harvest)
end
it "links to planting" do
assert_select "a", href: planting_path(planting)
end
it "links to garden" do
assert_select "a", href: garden_path(garden)
end
it "links to seeds" do
assert_select "a", href: seed_path(seed)
end