Skip to content

Commit

Permalink
add like feature for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ViditChitkara committed May 25, 2018
1 parent 8617ad2 commit 07ff585
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 78 deletions.
19 changes: 19 additions & 0 deletions app/controllers/comment_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,23 @@ def make_answer
end
end

def like_comment
@comment_id = params["comment_id"].to_i
@user_id = params["user_id"].to_i
comment = Comment.where(cid: @comment_id).first
like = comment.likes.where(user_id: @user_id)
@is_liked = like.count>0
if like.count>0
like.first.destroy
else
comment.likes.create(user_id: @user_id)
end

respond_with do |format|
format.js {
render template: 'comment/like_comment'
}
end
end

end
9 changes: 9 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Comment < ActiveRecord::Base
# dependent: :destroy, counter_cache: true
belongs_to :drupal_user, foreign_key: 'uid'
belongs_to :answer, foreign_key: 'aid'
has_many :likes, :as => :likeable

validates :comment, presence: true

Expand Down Expand Up @@ -194,4 +195,12 @@ def publish
self
end

def liked_by(user_id)
self.likes.where(user_id: user_id).count > 0
end

def likers
User.where(id: self.likes.pluck(:user_id))
end

end
3 changes: 3 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Like < ActiveRecord::Base
belongs_to :likeable, polymorphic: true
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class User < ActiveRecord::Base
has_many :passive_relationships, class_name: 'Relationship', foreign_key: 'followed_id', dependent: :destroy
has_many :following_users, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_many :likes

validates_with UniqueUsernameValidator, on: :create
validates_format_of :username, with: /\A[A-Za-z\d_\-]+\z/
Expand Down
14 changes: 14 additions & 0 deletions app/views/comment/like_comment.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% comment_star = "#comment-like-star-#{@comment_id}-#{@user_id}" %>
<% comment_count = "#comment-like-count-#{@comment_id}-#{@user_id}" %>

<% if @is_liked %>
$("<%= comment_star %>").removeClass("fa fa-star").addClass("fa fa-star-o");
oldValue = parseInt($("<%= comment_count %>").html());
newValue = oldValue-1;
$("<%= comment_count %>").html(newValue);
<% else %>
$("<%= comment_star %>").removeClass("fa fa-star-o").addClass("fa fa-star");
oldValue = parseInt($("<%= comment_count %>").html());
newValue = oldValue+1;
$("<%= comment_count %>").html(newValue);
<% end %>
20 changes: 20 additions & 0 deletions app/views/notes/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@
<% end %>
</div>

<div class="hidden-print btn-toolbar pull-right" style="margin-left:10px;">
<ul class="btn-group" style="margin-top: 16px; margin-right: 16px;">
<% if !current_user %>
<li rel="tooltip" title="Helpful? Like it and get updates!" class="btn btn-default btn-sm btn-like">
<%= link_to new_user_session_path( return_to: request.path ), style: "text-decoration: none; color: black;" do %>
<span class="fa fa-star-o"></span>
<span><%= comment.likers.count %></span>
<% end %>
<% else %>
<% str = "/comment/like?comment_id=#{comment.cid}&user_id=#{current_user.uid}" %>
<li rel="tooltip" title="Helpful? Like it and get updates!" class="btn btn-default btn-sm btn-like">
<%= link_to str, data: { method: "post", remote: true }, style: "text-decoration: none; color: black;" do %>
<span id="comment-like-star-<%= comment.cid %>-<%= current_user.uid %>" class="fa fa-star<% if !comment.liked_by(current_user.uid) %>-o<% end %>"></span>
<span id="comment-like-count-<%= comment.cid %>-<%= current_user.uid %>"><%= comment.likers.count %></span>
<% end %>
<% end %>
</li>
</ul>
</div>

<div class="navbar-text navbar-right pull-right" style="padding-right:10px;">
<% if comment.author && comment.author.user && comment.author.user.role %>
<!-- Role icon for admins & moderators
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@

get 'comment/answer_create/:aid' => 'comment#answer_create'
post 'comment/make_answer/:id' => 'comment#make_answer'
post '/comment/like' => 'comment#like_comment'
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20180406080905_create_likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.integer :likeable_id
t.integer :user_id
t.string :likeable_type

t.timestamps
end
end
end
161 changes: 83 additions & 78 deletions db/schema.rb.example

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions test/fixtures/likes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
likeable_id: 1
likeable_type: MyString

two:
likeable_id: 1
likeable_type: MyString
7 changes: 7 additions & 0 deletions test/models/like_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class LikeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 07ff585

Please sign in to comment.