From d367b9ef217cc63e6f015a16f1a20bbb830b5edf Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 16 Apr 2017 21:37:45 +1200 Subject: [PATCH] UI for removing photos from plantings --- .../photo_associations_controller.rb | 13 ++++++++ app/views/photos/show.html.haml | 30 ++++++++++++------- config/routes.rb | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 app/controllers/photo_associations_controller.rb diff --git a/app/controllers/photo_associations_controller.rb b/app/controllers/photo_associations_controller.rb new file mode 100644 index 000000000..b2d0ed684 --- /dev/null +++ b/app/controllers/photo_associations_controller.rb @@ -0,0 +1,13 @@ +class PhotoAssociationsController < ApplicationController + before_action :authenticate_member! + respond_to :json, :html + + def destroy + @photo = Photo.find(params[:photo_id]) + if params[:type] == 'planting' + @planting = Planting.find(params[:planting_id]) + @photo.plantings.delete(@planting) if @planting.owner == current_user && @photo.owner == current_user + end + respond_with(@photo) + end +end diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index 8e038f3f9..aa702d699 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -12,6 +12,15 @@ %p= image_tag(@photo.fullsize_url, alt: @photo.title, class: 'img') .col-md-4 + %p + - if can? :destroy, @photo + = link_to @photo, method: :delete, + data: { confirm: 'Are you sure?' }, class: 'btn btn-default btn-xs' do + %span.glyphicon.glyphicon-trash{ title: "Delete" } + + - if can? :edit, @photo + = link_to edit_photo_path(@photo), class: 'btn btn-default btn-xs' do + %span.glyphicon.glyphicon-pencil{ title: "Edit" } %p %strong Posted by: = link_to @photo.owner, @photo.owner @@ -25,24 +34,23 @@ %p= link_to "View on Flickr", @photo.link_url - - if can? :destroy, @photo - = link_to 'delete', - @photo, method: :delete, - data: { confirm: 'Are you sure?' }, - class: 'btn btn-default btn-xs' - - if can? :edit, @photo - = link_to 'edit', edit_photo_path(@photo), class: 'btn btn-default btn-xs' - - - if @photo.associations? %h4 This photo depicts: %ul - @photo.plantings.each do |p| - %li= link_to t('.planting', planting: p.to_s, owner: p.owner.to_s), planting_path(p) + %li + = link_to t('.planting', planting: p.to_s, owner: p.owner.to_s), planting_path(p) + - if can? :edit, @photo + = link_to photo_associations_path(photo_id: @photo.id, + type: 'planting', planting_id: p.id), method: 'delete', + class: 'btn btn-default btn-xs' do + %span.glyphicon.glyphicon-remove{ title: "Remove link" } + - @photo.harvests.each do |h| %li= link_to t('.harvest', crop: h.crop.name, owner: h.owner.to_s), harvest_path(h) + - @photo.gardens.each do |g| %li= link_to t('.garden', garden: g.to_s, owner: g.owner.to_s), garden_path(g) + - @photo.seeds.each do |s| %li= link_to t('.seed', seed: s.to_s, owner: s.owner.to_s), seed_path(s) - diff --git a/config/routes.rb b/config/routes.rb index 5d9a40ffd..e999510e7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,7 @@ Growstuff::Application.routes.draw do resources :members resources :photos + delete 'photo_associations' => 'photo_associations#destroy' resources :authentications, only: [:create, :destroy]