Skip to content

Commit

Permalink
回帖内容里面包含 +1, :plus1:, 以及表情符号的手势等赞有关的字符时,赞帖子。#460
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Oct 13, 2015
1 parent cb682da commit c70e253
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
13 changes: 13 additions & 0 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Reply
include Mongoid::Mentionable
include Mongoid::Likeable

UPVOTES = %w(+1 :+1: :thumbsup: :plus1: 👍 👍🏻 👍🏼 👍🏽 👍🏾 👍🏿)

field :body
field :body_html

Expand Down Expand Up @@ -54,6 +56,13 @@ def update_parent_topic_updated_at
Reply.delay.notify_reply_created(id)
end

after_create :check_vote_chars_for_like_topic
def check_vote_chars_for_like_topic
return unless self.upvote?
user.like(topic)
end


def self.per_page
50
end
Expand Down Expand Up @@ -92,6 +101,10 @@ def popular?
likes_count >= 5
end

def upvote?
body.strip.start_with?(*UPVOTES)
end

def destroy
super
notifications.delete_all
Expand Down
30 changes: 17 additions & 13 deletions app/views/replies/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<% if @reply.errors.blank? %>
<% @page = 1 %>
if($("#replies").length == 0){
<% if @reply.upvote? %>
Turbolinks.visit(location.href);
}
else {
var current_floor = parseInt($("#replies").data("last-floor")) + 1;
$("#replies .items").append('<%= j(render("reply", reply: @reply, reply_counter: @topic.replies_count - 1, display_edit: true)) %>')
.find(".total b").text('<%= @topic.replies_count %>');
$("#replies .items .reply:last .reply-floor").text(current_floor + " 楼");
$("#replies").data("last-floor", current_floor);
$("#replies .reply a.edit:last").css("display","inline-block");
_topicView.replyCallback(1,'<%= j(@msg) %>');
}
<% else %>
<% @page = 1 %>
if($("#replies").length == 0){
Turbolinks.visit(location.href);
}
else {
var current_floor = parseInt($("#replies").data("last-floor")) + 1;
$("#replies .items").append('<%= j(render("reply", reply: @reply, reply_counter: @topic.replies_count - 1, display_edit: true)) %>')
.find(".total b").text('<%= @topic.replies_count %>');
$("#replies .items .reply:last .reply-floor").text(current_floor + " 楼");
$("#replies").data("last-floor", current_floor);
$("#replies .reply a.edit:last").css("display", "inline-block");
_topicView.replyCallback(1, '<%= j(@msg) %>');
}
<% end %>
<% else %>
_topicView.replyCallback(0,'<%= j(@msg) %>');
_topicView.replyCallback(0, '<%= j(@msg) %>');
<% end %>
47 changes: 47 additions & 0 deletions spec/models/reply_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,51 @@
r.destroy
end
end

describe 'Vote by content' do
let(:reply) { build :reply }

describe '.upvote?' do
let(:chars) { %w(+1 :+1: :thumbsup: :plus1: 👍 👍🏻 👍🏼 👍🏽 👍🏾 👍🏿) }
it 'should work' do
chars.each do |key|
reply.body = key
expect(reply.upvote?).to eq(true)
end

reply.body = 'Ok +1'
expect(reply.upvote?).to eq(false)
end
end

describe '.check_vote_chars_for_like_topic' do
let(:user) { create :user }
let(:topic) { create :topic }
let(:reply) { build :reply, user: user, topic: topic }

context 'UpVote' do
it 'should work' do
expect(user).to receive(:like).with(topic).once
allow(reply).to receive(:upvote?).and_return(true)
reply.check_vote_chars_for_like_topic
end
end

context 'None' do
it 'should work' do
expect(user).to receive(:like).with(topic).at_most(0).times
allow(reply).to receive(:upvote?).and_return(false)
reply.check_vote_chars_for_like_topic
end
end

context 'callback on created' do
it 'should work' do
expect(reply).to receive(:check_vote_chars_for_like_topic).once
reply.save
end
end
end
end

end

0 comments on commit c70e253

Please sign in to comment.