diff --git a/app/controllers/comment_controller.rb b/app/controllers/comment_controller.rb index d478034cd10..1464b4a758a 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comment_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index e9bf0a9473a..e8690fdd042 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 @@ -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 diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 00000000000..af2bdb5314a --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,3 @@ +class Like < ActiveRecord::Base + belongs_to :likeable, polymorphic: true +end diff --git a/app/models/user.rb b/app/models/user.rb index b7f79d9d3e9..eb98d25e25b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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/ diff --git a/app/views/comment/like_comment.js.erb b/app/views/comment/like_comment.js.erb new file mode 100644 index 00000000000..d6c61e99eb4 --- /dev/null +++ b/app/views/comment/like_comment.js.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/notes/_comment.html.erb b/app/views/notes/_comment.html.erb index 23742311582..00278e36474 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -23,6 +23,26 @@ <% end %> +
+