From 9ed6c01e3943a4b902bac28fce95776adddfb2be Mon Sep 17 00:00:00 2001 From: ViditChitkara Date: Tue, 19 Jun 2018 00:17:51 +0530 Subject: [PATCH 1/6] added emoji type to likes --- db/migrate/20180618184048_add_emoji_type_to_like.rb | 5 +++++ db/schema.rb.example | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180618184048_add_emoji_type_to_like.rb 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 0000000000..9b67dc978c --- /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 12a2dd1ce9..cca8542808 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| From 92a3df60e07a259de4e0733428ac7594b3b9bff2 Mon Sep 17 00:00:00 2001 From: ViditChitkara Date: Wed, 20 Jun 2018 20:34:57 +0530 Subject: [PATCH 2/6] like via emoji feature initial --- app/controllers/comment_controller.rb | 6 +- app/models/comment.rb | 4 + app/views/comment/like_comment.js.erb | 46 +++++++--- app/views/notes/_comment.html.erb | 120 +++++++++++++++++++++++++- 4 files changed, 157 insertions(+), 19 deletions(-) diff --git a/app/controllers/comment_controller.rb b/app/controllers/comment_controller.rb index 0a95f7b1ae..6f612790e0 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/models/comment.rb b/app/models/comment.rb index 5a369b0e1d..2804e7a4e0 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 + self.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 d6c61e99eb..8c025c0a18 100644 --- a/app/views/comment/like_comment.js.erb +++ b/app/views/comment/like_comment.js.erb @@ -1,14 +1,32 @@ -<% 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" %> +$("<%= str %>").css("display","none"); +<% if @likes.has_key? "ThumbsUp" %> + <% str="##{@comment_id}-emoji-button-thumbs-up" %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").val("<%= @likes["ThumbsUp"] %>"); +<% end %> +<% if @likes.has_key? "ThumbsDown" %> + <% str="##{@comment_id}-emoji-button-thumbs-down" %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").val("<%= @likes["ThumbsDown"] %>"); +<% end %> +<% if @likes.has_key? "Laugh" %> + <% str="##{@comment_id}-emoji-button-laugh" %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").val("<%= @likes["Laugh"] %>"); +<% end %> +<% if @likes.has_key? "Hooray" %> + <% str="##{@comment_id}-emoji-button-hooray" %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").val("<%= @likes["Hooray"] %>"); +<% end %> +<% if @likes.has_key? "Confused" %> + <% str="##{@comment_id}-emoji-button-confused" %> + $("<%= str %>").css("display","flex"); + $("<%= str %> div").val("<%= @likes["Confused"] %>"); +<% end %> +<% if @likes.has_key? "Heart" %> + <% str="##{@comment_id}-emoji-button-heart" %> + $("<%= str %>").css("display","flex"); + $("<%= str %>").val("<%= @likes["Heart"] %>"); +<% end %> diff --git a/app/views/notes/_comment.html.erb b/app/views/notes/_comment.html.erb index e480ea57ec..b5304d170b 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -1,6 +1,6 @@ -
+
- @@ -87,7 +131,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 +141,52 @@

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_like_map = comment.emoji_likes %> + <% if emoji_like_map.has_key? "ThumbsUp" %> + <% str = "#{comment.id}-emoji-button-thumbs-up" %> + + <% end %> + <% if emoji_like_map.has_key? "ThumbsDown" %> + <% str = "#{comment.id}-emoji-button-thumbs-down" %> + + <% end %> + <% if emoji_like_map.has_key? "Laugh" %> + <% str = "#{comment.id}-emoji-button-laugh" %> + + <% end %> + <% if emoji_like_map.has_key? "Hooray" %> + <% str = "#{comment.id}-emoji-button-hooray" %> + + <% end %> + <% if emoji_like_map.has_key? "Confused" %> + <% str = "#{comment.id}-emoji-button-confused" %> + + <% end %> + <% if emoji_like_map.has_key? "Heart" %> + <% str = "#{comment.id}-emoji-button-heart" %> + + <% 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 +194,27 @@
+ + From 3c7503837fd8b26c2c2c7ed9420454545bdf6411 Mon Sep 17 00:00:00 2001 From: ViditChitkara Date: Wed, 20 Jun 2018 21:48:34 +0530 Subject: [PATCH 3/6] code cleanup --- app/views/notes/_comment.html.erb | 56 +++++++++--------------------- app/views/notes/_comments.html.erb | 2 +- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/app/views/notes/_comment.html.erb b/app/views/notes/_comment.html.erb index b5304d170b..47cec379b6 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -143,49 +143,25 @@
<% 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 = ["thumbs-up", "thumbs-down", "laugh", "hooray", "confused", "heart"] %> <% emoji_like_map = comment.emoji_likes %> - <% if emoji_like_map.has_key? "ThumbsUp" %> - <% str = "#{comment.id}-emoji-button-thumbs-up" %> - <% end %> - <% if emoji_like_map.has_key? "ThumbsDown" %> - <% str = "#{comment.id}-emoji-button-thumbs-down" %> - - <% end %> - <% if emoji_like_map.has_key? "Laugh" %> - <% str = "#{comment.id}-emoji-button-laugh" %> - - <% end %> - <% if emoji_like_map.has_key? "Hooray" %> - <% str = "#{comment.id}-emoji-button-hooray" %> - - <% end %> - <% if emoji_like_map.has_key? "Confused" %> - <% str = "#{comment.id}-emoji-button-confused" %> - - <% end %> - <% if emoji_like_map.has_key? "Heart" %> - <% str = "#{comment.id}-emoji-button-heart" %> - - <% end %>
<% if current_user %> diff --git a/app/views/notes/_comments.html.erb b/app/views/notes/_comments.html.erb index 349846c2ea..b40a9b1cd7 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 %> From add51908311050725fd76a69884fcf63eacca170 Mon Sep 17 00:00:00 2001 From: ViditChitkara Date: Wed, 20 Jun 2018 22:40:45 +0530 Subject: [PATCH 4/6] minor tweaks --- app/helpers/application_helper.rb | 14 ++++++ app/views/comment/like_comment.js.erb | 38 ++++------------ app/views/notes/_comment.html.erb | 62 ++++++--------------------- 3 files changed, 37 insertions(+), 77 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3cbab154b5..6c8962dad7 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" + } + return emoji_names, emoji_image_map + end + def feature(title) features = Node.where(type: 'feature', title: title) if !features.empty? diff --git a/app/views/comment/like_comment.js.erb b/app/views/comment/like_comment.js.erb index 8c025c0a18..0ec5c77aff 100644 --- a/app/views/comment/like_comment.js.erb +++ b/app/views/comment/like_comment.js.erb @@ -1,32 +1,12 @@ <% str="##{@comment_id}-like-emojis button" %> +<% emoji_names = ["thumbs-up", "thumbs-down", "laugh", + "hooray", "confused", "heart"] %> $("<%= str %>").css("display","none"); -<% if @likes.has_key? "ThumbsUp" %> - <% str="##{@comment_id}-emoji-button-thumbs-up" %> - $("<%= str %>").css("display","flex"); - $("<%= str %> div").val("<%= @likes["ThumbsUp"] %>"); -<% end %> -<% if @likes.has_key? "ThumbsDown" %> - <% str="##{@comment_id}-emoji-button-thumbs-down" %> - $("<%= str %>").css("display","flex"); - $("<%= str %> div").val("<%= @likes["ThumbsDown"] %>"); -<% end %> -<% if @likes.has_key? "Laugh" %> - <% str="##{@comment_id}-emoji-button-laugh" %> - $("<%= str %>").css("display","flex"); - $("<%= str %> div").val("<%= @likes["Laugh"] %>"); -<% end %> -<% if @likes.has_key? "Hooray" %> - <% str="##{@comment_id}-emoji-button-hooray" %> - $("<%= str %>").css("display","flex"); - $("<%= str %> div").val("<%= @likes["Hooray"] %>"); -<% end %> -<% if @likes.has_key? "Confused" %> - <% str="##{@comment_id}-emoji-button-confused" %> - $("<%= str %>").css("display","flex"); - $("<%= str %> div").val("<%= @likes["Confused"] %>"); -<% end %> -<% if @likes.has_key? "Heart" %> - <% str="##{@comment_id}-emoji-button-heart" %> - $("<%= str %>").css("display","flex"); - $("<%= str %>").val("<%= @likes["Heart"] %>"); +<% 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 47cec379b6..c83104a928 100644 --- a/app/views/notes/_comment.html.erb +++ b/app/views/notes/_comment.html.erb @@ -73,43 +73,17 @@ + -
@@ -143,16 +117,8 @@
<% 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 = ["thumbs-up", "thumbs-down", "laugh", "hooray", "confused", "heart"] %> + <% emoji_names, emoji_image_map = emoji_info %> <% emoji_like_map = comment.emoji_likes %> - <% 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.each do |e| %> <% capitalized_emoji_name = e.split("-").map(&:capitalize).join %> <% str = "#{comment.id}-emoji-button-#{e}" %> @@ -172,13 +138,13 @@