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#add_reactions #18

Merged
merged 3 commits into from
Apr 30, 2022
Merged
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
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If bundler is not being used to manage dependencies, install the gem by executin

$ gem install acts_as_reactable

## Usage
## Preparations

### 1. create the Reaction model

Expand Down Expand Up @@ -54,13 +54,16 @@ class User < ApplicationRecord
end
```

### 3. creating/updating reactions
## Usage

### adding/updating reactions

```ruby
reaction = post.update_reaction_from(user, "😀")
post.add_reactions(user, "😀")
post.add_reactions(user, ["😞", "🙃"])
```

### 4. deleting reactions
### deleting reactions

```ruby
post.destroy_reaction_from(user) # returns value like #destroy in ActiveRecord
Expand All @@ -69,13 +72,13 @@ post.destroy_reaction_from(user) # returns value like #destroy in ActiveRecord
post.update_reaction_from(user)
```

### 5. private opinion from one reactor
### private opinion from one reactor

```ruby
reaction = ActsAsReactable::Reaction.find_by(reactable: self, reactor: user)&.emoji
```

### 6. group, count and sort to get a summary of public opinion
### group, count and sort to get a summary of public opinion

```ruby
ActsAsReactable::Reaction.where(reactable: reactor).group(:emoji).order('count_id DESC').count(:id)
Expand Down
22 changes: 22 additions & 0 deletions lib/acts_as_reactable/reactable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ def acts_as_reactable
reaction = reactions.where({reactor: reactor, emoji: emoji}).first_or_create
reaction
end

define_method :add_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

# TODO performance
# optimize by using a single query
emojis
.compact
.uniq
.each do |emoji|
reaction = reactions.find_or_create_by(reactor: reactor, emoji: emoji)
reaction.save unless reaction.persisted?
end

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

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

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

it "adds one reaction from one emoji character" do
expect {
post.add_reactions(user, "😀")
expect(post.reactions.map(&:emoji)).to eq(%w[😀])
}.to change(ActsAsReactable::Reaction, :count).by(1)
end

it "ignores invalid emoji" do
expect {
post.add_reactions(user, %w[🔴 🟡 "A"])
# NOTE using id not being nil to filter invalid reactions
expect(post.reactions.where.not(id: nil).map(&:emoji)).to eq(%w[🔴 🟡])
}.to change(ActsAsReactable::Reaction, :count).by(2)
end

it "ignores existing reactions" do
post.add_reactions(user, %w[🔴 🟡])
expect {
post.add_reactions(user, %w[🔴 🟢 🟣])
expect(post.reactions.map(&:emoji)).to eq(%w[🔴 🟡 🟢 🟣])
}.to change(ActsAsReactable::Reaction, :count).by(2)
end

it "ignores repeating reactions" do
expect {
post.add_reactions(user, %w[🔴 🔴 🟢 🟣])
expect(post.reactions.map(&:emoji)).to eq(%w[🔴 🟢 🟣])
}.to change(ActsAsReactable::Reaction, :count).by(3)
end

it "returns the self - reactable item" do
expect(post.add_reactions(user, %w[😀 😭])).to eq(post)
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
SimpleCov.start

RSpec.configure do |config|
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Schema.define(version: 1) do
Expand Down