Skip to content

Commit

Permalink
add like feature for comments (publiclab#2591)
Browse files Browse the repository at this point in the history
* add like feature for comments

* code climate fixes

* changes in tests

* changes in schema.rb.example

* changes in schema.rb.example
  • Loading branch information
ViditChitkara authored and jywarren committed May 29, 2018
1 parent 34762a3 commit f8ed117
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 2 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.positive?
if like.count.positive?
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)
likes.where(user_id: user_id).count > 0
end

def likers
User.where(id: 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
12 changes: 10 additions & 2 deletions db/schema.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180309180053) do
ActiveRecord::Schema.define(version: 20180406080905) do

create_table "answer_selections", force: true do |t|
t.integer "user_id"
Expand Down Expand Up @@ -199,6 +199,14 @@ ActiveRecord::Schema.define(version: 20180309180053) do
add_index "impressions", ["impressionable_type"], name: "index_impressions_on_impressionable_type", using: :btree
add_index "impressions", ["user_id"], name: "index_impressions_on_user_id", using: :btree

create_table "likes", force: true do |t|
t.integer "likeable_id"
t.integer "user_id"
t.string "likeable_type"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "node", primary_key: "nid", force: true do |t|
t.integer "vid", default: 0, null: false
t.string "type", limit: 32, default: "", null: false
Expand Down Expand Up @@ -442,4 +450,4 @@ ActiveRecord::Schema.define(version: 20180309180053) do
add_index "users", ["name"], name: "index_users_name", using: :btree
add_index "users", ["uid"], name: "index_users_uid", using: :btree

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

one:
likeable_id: 1
user_id: 1
likeable_type: "Comment"

two:
likeable_id: 1
user_id: 2
likeable_type: "Comment"
19 changes: 19 additions & 0 deletions test/functional/comment_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,25 @@ def teardown
assert_equal html.scan('<a href="/node/update/title?id=2&title=New Title"').length,0
end

test 'should increase likes if liked' do
UserSession.create(users(:bob))
comment = comments(:first)
like_count = Like.where(likeable_id: comment.id, likeable_type: "Comment").count
xhr :post, :like_comment, comment_id: comment.id, user_id: 7
updated_like_count = Like.where(likeable_id: comment.id, likeable_type: "Comment").count
assert_equal updated_like_count, like_count+1
end

test 'should decrease likes if unliked' do
UserSession.create(users(:bob))
comment = comments(:first)
Like.create(likeable_id: comment.id, user_id: 1, likeable_type: "Comment")
like_count = Like.where(likeable_id: comment.id, likeable_type: "Comment").count
xhr :post, :like_comment, comment_id: comment.id, user_id: 1
updated_like_count = Like.where(likeable_id: comment.id, likeable_type: "Comment").count
assert_equal updated_like_count, like_count-1
end

private

def current_user
Expand Down
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 f8ed117

Please sign in to comment.