likes for photos

This commit is contained in:
Brenda Wallace
2019-07-08 20:00:55 +12:00
parent 76c18014e2
commit cbed8cc259
5 changed files with 53 additions and 6 deletions

View File

@@ -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');
}
});
});

View File

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

View File

@@ -1,4 +1,5 @@
class Photo < ApplicationRecord
include Likeable
include Ownable
PHOTO_CAPABLE = %w(Garden Planting Harvest Seed).freeze

View File

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

View File

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