From 5697f0b375a8fd2200aecb9f6e9840a988ede75e Mon Sep 17 00:00:00 2001 From: Vidit Date: Thu, 21 Jun 2018 00:13:03 +0530 Subject: [PATCH] Like with emoji (#2869) * added emoji type to likes * like via emoji feature initial * code cleanup * minor tweaks * code climate fixes * remove previous version of like-comment --- app/controllers/comment_controller.rb | 6 +- app/helpers/application_helper.rb | 14 ++++ app/models/comment.rb | 4 + app/views/comment/like_comment.js.erb | 26 +++--- app/views/notes/_comment.html.erb | 80 ++++++++++++++----- app/views/notes/_comments.html.erb | 2 +- .../20180618184048_add_emoji_type_to_like.rb | 5 ++ db/schema.rb.example | 3 +- 8 files changed, 101 insertions(+), 39 deletions(-) create mode 100644 db/migrate/20180618184048_add_emoji_type_to_like.rb diff --git a/app/controllers/comment_controller.rb b/app/controllers/comment_controller.rb index 0a95f7b1ae9..6f612790e03 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comment_controller.rb @@ -181,15 +181,17 @@ def make_answer def like_comment @comment_id = params["comment_id"].to_i @user_id = params["user_id"].to_i + @emoji_type = params["emoji_type"] comment = Comment.where(cid: @comment_id).first - like = comment.likes.where(user_id: @user_id) + like = comment.likes.where(user_id: @user_id, emoji_type: @emoji_type) @is_liked = like.count.positive? if like.count.positive? like.first.destroy else - comment.likes.create(user_id: @user_id) + comment.likes.create(user_id: @user_id, emoji_type: @emoji_type) end + @likes = comment.likes.group(:emoji_type).count respond_with do |format| format.js { render template: 'comment/like_comment' diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3cbab154b5c..8d0a6d816f4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -39,6 +39,20 @@ def emoji_names_list { emojis: emojis, image_map: image_map } end + def emoji_info + emoji_names = ["thumbs-up", "thumbs-down", "laugh", + "hooray", "confused", "heart"] + emoji_image_map = { + "thumbs-up" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f44d.png", + "thumbs-down" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f44e.png", + "laugh" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f604.png", + "hooray" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f389.png", + "confused" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f615.png", + "heart" => "https://assets-cdn.github.com/images/icons/emoji/unicode/2764.png" + } + [emoji_names, emoji_image_map] + end + def feature(title) features = Node.where(type: 'feature', title: title) if !features.empty? diff --git a/app/models/comment.rb b/app/models/comment.rb index 5a369b0e1dc..167c7b6b7e6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -200,6 +200,10 @@ def likers User.where(id: likes.pluck(:user_id)) end + def emoji_likes + likes.group(:emoji_type).count + end + def self.receive_mail(message) node_id = message.subject[/#([\d]+)/, 1] #This took out the node ID from the subject line unless node_id.nil? diff --git a/app/views/comment/like_comment.js.erb b/app/views/comment/like_comment.js.erb index d6c61e99eb4..0ec5c77aff1 100644 --- a/app/views/comment/like_comment.js.erb +++ b/app/views/comment/like_comment.js.erb @@ -1,14 +1,12 @@ -<% 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 +<% str="##{@comment_id}-like-emojis button" %> +<% emoji_names = ["thumbs-up", "thumbs-down", "laugh", + "hooray", "confused", "heart"] %> +$("<%= str %>").css("display","none"); +<% emoji_names.each do |e| %> + <% str="##{@comment_id}-emoji-button-#{e}" %> + <% capitalized_emoji_name = e.split("-").map(&:capitalize).join %> + <% if @likes.has_key? capitalized_emoji_name %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").first().html("<%= @likes[capitalized_emoji_name] %>"); + <% end %> +<% end %> diff --git a/app/views/notes/_comment.html.erb b/app/views/notes/_comment.html.erb index e480ea57ec4..3e5b3c56da9 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -1,6 +1,6 @@ -
+
- @@ -87,7 +87,7 @@ }) -
+

<%= raw render_comment_body(comment) %>

<% if comment.body.include?('?') %>

Is this a question? Click here to post it to the Questions page. @@ -97,6 +97,20 @@

Have you attempted or completed this activity? Consider sharing how it went to help refine and improve it.
<% end %>
+ <% str = "#{comment.id}-like-emojis" %> +
class="navbar-text navbar-right pull-right" style="margin: 0;width: 100%;display: flex;border: 1px solid #e7e7e7;border-top: 0;"> + <% emoji_names, emoji_image_map = emoji_info %> + <% emoji_like_map = comment.emoji_likes %> + <% emoji_names.each do |e| %> + <% capitalized_emoji_name = e.split("-").map(&:capitalize).join %> + <% str = "#{comment.id}-emoji-button-#{e}" %> + <% display = (emoji_like_map.has_key? capitalized_emoji_name) ? "display: flex;" : "display: none;" %> + + <% end %> +
<% if current_user %> <%= render :partial => "comments/edit", :locals => { title: I18n.t('notes._comment.edit_comment'), comment: comment, placeholder: I18n.t('notes._comment.edit_placeholder') } %> @@ -104,3 +118,27 @@
+ + diff --git a/app/views/notes/_comments.html.erb b/app/views/notes/_comments.html.erb index 349846c2ea0..b40a9b1cd79 100644 --- a/app/views/notes/_comments.html.erb +++ b/app/views/notes/_comments.html.erb @@ -4,7 +4,7 @@

<%= @node.comments.length %> <%= t('notes._comments.comments') %>

-
+
<% @node.comments.order('timestamp ASC').each do |comment| %> <% if comment.cid == @node.comments.first.cid %><% end %> diff --git a/db/migrate/20180618184048_add_emoji_type_to_like.rb b/db/migrate/20180618184048_add_emoji_type_to_like.rb new file mode 100644 index 00000000000..9b67dc978c1 --- /dev/null +++ b/db/migrate/20180618184048_add_emoji_type_to_like.rb @@ -0,0 +1,5 @@ +class AddEmojiTypeToLike < ActiveRecord::Migration[5.0] + def change + add_column :likes, :emoji_type, :string + end +end diff --git a/db/schema.rb.example b/db/schema.rb.example index 12a2dd1ce91..cca85428089 100644 --- a/db/schema.rb.example +++ b/db/schema.rb.example @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180605010014) do +ActiveRecord::Schema.define(version: 20180618184048) do create_table "answer_selections", force: true do |t| t.integer "user_id" @@ -207,6 +207,7 @@ ActiveRecord::Schema.define(version: 20180605010014) do t.string "likeable_type" t.datetime "created_at" t.datetime "updated_at" + t.string "emoji_type" end create_table "node", primary_key: "nid", force: true do |t|