diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/likes.js similarity index 52% rename from app/assets/javascripts/posts.js rename to app/assets/javascripts/likes.js index 37269e78e..cb759bbf3 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/likes.js @@ -1,7 +1,9 @@ $(document).ready(function() { + $('.post-like').show(); $('.post-like').on('ajax:success', function(event, data) { + debugger; var likeControl = $('#post-' + data.id + ' .post-like'); $('#post-' + data.id + ' .like-count').text(data.description); @@ -15,4 +17,21 @@ $(document).ready(function() { likeControl.text('Like'); } }); + + $('.photo-like').show(); + + $('.photo-like').on('ajax:success', function(event, data) { + var likeControl = $('.photo-like'); + + $('.like-count').text(data.description); + if (data.liked_by_member) { + likeControl.data('method', 'delete'); + likeControl.attr('href', data.url); + likeControl.text('Unlike'); + } else { + likeControl.data('method', 'post'); + likeControl.attr('href', '/likes.json?photo_id=' + data.id); + likeControl.text('Like'); + } + }); }); diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index e942d5b1f..c99ae552f 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -19,7 +19,11 @@ class LikesController < ApplicationController private def find_likeable - Post.find(params[:post_id]) if params[:post_id] + if params[:post_id] + Post.find(params[:post_id]) + elsif params[:photo_id] + Photo.find(params[:photo_id]) + end end def render_json(like, liked_by_member: true) @@ -35,8 +39,9 @@ class LikesController < ApplicationController respond_to do |format| format.html { redirect_to like.likeable } format.json do - render(json: render_json(like, liked_by_member: liked_by_member), - status: status_code) + render(json: render_json(like, + liked_by_member: liked_by_member), + status: status_code) end end end diff --git a/app/models/photo.rb b/app/models/photo.rb index 2481fab2e..25ca565b6 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,4 +1,5 @@ class Photo < ApplicationRecord + include Likeable include Ownable PHOTO_CAPABLE = %w(Garden Planting Harvest Seed).freeze diff --git a/app/views/photos/_likes.html.haml b/app/views/photos/_likes.html.haml new file mode 100644 index 000000000..93acc4bf0 --- /dev/null +++ b/app/views/photos/_likes.html.haml @@ -0,0 +1,17 @@ + +- if member_signed_in? + - if !photo.members.include? current_member + - if can?(:new, Like) + = link_to 'Like', likes_path(photo_id: photo.id, format: :json), + method: :post, remote: true, class: 'photo-like btn' + - else + - like = photo.likes.find_by(member: current_member) + - if like && can?(:destroy, like) + = link_to 'Unlike', like_path(id: like.id, format: :json), + method: :delete, remote: true, class: 'photo-like btn' + +%span.badge.badge-info + .like-count + - unless photo.likes.empty? + = like_icon + = pluralize(photo.likes.count, "like") diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index bd8632f4d..53c6a14a7 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -11,17 +11,22 @@ %li.breadcrumb-item= link_to 'Photos', photos_path %li.breadcrumb-item.active= link_to @photo, @photo -.row +.row#photo .col-md-8 %h1.text-center.ellipsis=@photo.title %p.text-center = image_tag(@photo.fullsize_url, alt: @photo.title, class: 'rounded img-fluid shadow-sm') .col-md-4 - %p.text-center + %p.text-center{ id: "photo-#{@photo.id}" } = render 'photos/actions', photo: @photo = link_to "View on Flickr", @photo.link_url, class: 'btn' + + = render 'photos/likes', photo: @photo + - if @crops.size.positive? - %p= render @crops + .index-cards + - @crops.each do |crop| + = render 'crops/thumbnail', crop: crop %p = photo_icon %strong Photo by