Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reactable#remove_reactions #20

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ post.add_reactions(user, "😀")
post.add_reactions(user, ["😞", "🙃"])
```

### deleting reactions
### removing reactions

```ruby
post.destroy_reaction_from(user) # returns value like #destroy in ActiveRecord

# #update_reaction_from with a nil reaction also delete the reaction
post.update_reaction_from(user)
post.remove_reactions(user, "😀")
post.remove_reactions(user, ["😞", "🙃"])
```

### private opinion from one reactor
Expand Down
16 changes: 16 additions & 0 deletions lib/acts_as_reactable/reactable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ def acts_as_reactable

self
end

define_method :remove_reactions do |reactor, emoji_or_list = nil|
return unless emoji_or_list

emojis = if emoji_or_list.is_a?(Array)
emoji_or_list
else
[emoji_or_list]
end

reactions
.where(reactor: reactor, emoji: emojis.compact.uniq)
.destroy_all

self
end
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/lib/acts_as_reactable/reactable/remove_reactions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

RSpec.describe ActsAsReactable::Reactable do
describe "#remove_reactions" do
let(:user) { create(:user) }
let(:post) { create(:post, user: user) }

before do
post.add_reactions(user, %w[😀 😭 😎])
end

it "removes one reaction" do
expect {
post.remove_reactions(user, "😭")
expect(post.reactions.map(&:emoji)).to eq(%w[😀 😎])
}.to change(ActsAsReactable::Reaction, :count).by(-1)
end

it "removes multiple reactions from an array" do
expect {
post.remove_reactions(user, %w[😭 😎])
expect(post.reactions.map(&:emoji)).to eq(%w[😀])
}.to change(ActsAsReactable::Reaction, :count).by(-2)
end

it "ignores invalid emoji" do
expect {
post.remove_reactions(user, %w[😭 A])
expect(post.reactions.map(&:emoji)).to eq(%w[😀 😎])
}.to change(ActsAsReactable::Reaction, :count).by(-1)
end

it "ignores non-existing reactions" do
expect {
post.remove_reactions(user, %w[😭 😍])
expect(post.reactions.map(&:emoji)).to eq(%w[😀 😎])
}.to change(ActsAsReactable::Reaction, :count).by(-1)
end

it "returns the self - reactable item" do
expect(post.remove_reactions(user, %w[😀 😭])).to eq(post)
end
end
end