acts_as_reactable 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef084116ccbe7e59160dfda29dd6fd2679a2f1d425c689b4ac4fbb764e309ff4
4
- data.tar.gz: 4612e402698fee627577269f661ba646c33c5624d92a8a228c4d9c1f453b7483
3
+ metadata.gz: 17ef23a371d7abdef4f6134dec6841468f1fe8ac720a4087179c7760f57088d3
4
+ data.tar.gz: c7da600b50dde0fc8d1c432993f667dd0b96c7f2b8d081d20113549fda784577
5
5
  SHA512:
6
- metadata.gz: e85624536581e31d3844f2b4ebc3a6d30443a998924bf3abf6999035e4f7cc1439de643e066ba161c34beb7e8c2bb62d156542a7f84686cfad1e54c906b57ba2
7
- data.tar.gz: a1c03a4130d202328df9d83132c917dff7a467d71b07822efd4f62b88f690932b546b86022b5d6c433e0ee8bce3d48e3d30d036a8501498f57b375e6688a5a44
6
+ metadata.gz: e573dd63f8e9e87bc7a35d28b0e0462992a2860bc4d88a482e96e6a884aa14e56fd1e63013bfa33726fef3acd96c32138b81acdb94398e6395edf45771aea592
7
+ data.tar.gz: '09a8c3d23913f425d3a7f100dfe70f57b5bef53a337a45684b5417e9b195198ec543af5de098512644a8f8ed9b203513a9e1d8bb51e5d8509b75a72bf5bfd46d'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0](https://github.com/public-reactions/acts_as_reactable/compare/v0.3.0...v0.4.0) (2022-04-30)
4
+
5
+
6
+ ### Features
7
+
8
+ * reactable#add_reactions ([#18](https://github.com/public-reactions/acts_as_reactable/issues/18)) ([7320703](https://github.com/public-reactions/acts_as_reactable/commit/73207036cfd4879f620d30ef3777448163e291e7))
9
+ * reactable#remove_reactions ([#20](https://github.com/public-reactions/acts_as_reactable/issues/20)) ([100e7bd](https://github.com/public-reactions/acts_as_reactable/commit/100e7bd8e82b0b33de5a9427f82f6e0de40a7d51))
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * enforce standard ([d7c252c](https://github.com/public-reactions/acts_as_reactable/commit/d7c252cd6b7fae40e4f652efb4a6d2149fae9fa9))
15
+ * should add unique index for reactable/reactor/emoji ([050dd6c](https://github.com/public-reactions/acts_as_reactable/commit/050dd6ca5865afed80084fccdd237155967d601c))
16
+
3
17
  ## [0.3.0](https://github.com/public-reactions/acts_as_reactable/compare/v0.2.8...v0.3.0) (2022-04-25)
4
18
 
5
19
 
data/README.md CHANGED
@@ -15,29 +15,28 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
15
 
16
16
  $ gem install acts_as_reactable
17
17
 
18
- ## Usage
18
+ ## Preparations
19
19
 
20
20
  ### 1. create the Reaction model
21
21
 
22
22
  ```ruby
23
- # with default bigint id
23
+ # rails g migration create_reactions
24
+
25
+ # with default id type
24
26
  create_table :reactions do |t|
25
27
  t.references :reactable, polymorphic: true, null: false
26
28
  t.references :reactor, polymorphic: true, null: false
27
-
28
29
  t.string :emoji, null: false, index: true
29
-
30
30
  t.timestamps
31
+
32
+ t.index [:reactable_type, :reactable_id, :reactor_type, :reactor_id, :emoji], unique: true, name: 'index_reactions_on_reactable_and_reactor_and_emoji'
31
33
  end
32
34
 
33
35
  # with uuid id
34
- create_table :reactions do |t|
36
+ create_table :reactions, id: :uuid do |t|
35
37
  t.references :reactable, polymorphic: true, type: :uuid, null: false
36
38
  t.references :reactor, polymorphic: true, type: :uuid, null: false
37
-
38
- t.string :emoji, null: false, index: true
39
-
40
- t.timestamps
39
+ ...
41
40
  end
42
41
  ```
43
42
 
@@ -55,28 +54,29 @@ class User < ApplicationRecord
55
54
  end
56
55
  ```
57
56
 
58
- ### 3. creating/updating reactions
57
+ ## Usage
58
+
59
+ ### adding/updating reactions
59
60
 
60
61
  ```ruby
61
- reaction = post.update_reaction_from(user, "😀")
62
+ post.add_reactions(user, "😀")
63
+ post.add_reactions(user, ["😞", "🙃"])
62
64
  ```
63
65
 
64
- ### 4. deleting reactions
66
+ ### removing reactions
65
67
 
66
68
  ```ruby
67
- post.destroy_reaction_from(user) # returns value like #destroy in ActiveRecord
68
-
69
- # #update_reaction_from with a nil reaction also delete the reaction
70
- post.update_reaction_from(user)
69
+ post.remove_reactions(user, "😀")
70
+ post.remove_reactions(user, ["😞", "🙃"])
71
71
  ```
72
72
 
73
- ### 5. private opinion from one reactor
73
+ ### private opinion on reactable from reactor
74
74
 
75
75
  ```ruby
76
- reaction = ActsAsReactable::Reaction.find_by(reactable: self, reactor: user)&.emoji
76
+ reactions = ActsAsReactable::Reaction.where(reactable: post, reactor: user)
77
77
  ```
78
78
 
79
- ### 6. group, count and sort to get a summary of public opinion
79
+ ### group, count and sort to get a summary of public opinion
80
80
 
81
81
  ```ruby
82
82
  ActsAsReactable::Reaction.where(reactable: reactor).group(:emoji).order('count_id DESC').count(:id)
@@ -28,6 +28,44 @@ module ActsAsReactable
28
28
  reaction = reactions.where({reactor: reactor, emoji: emoji}).first_or_create
29
29
  reaction
30
30
  end
31
+
32
+ define_method :add_reactions do |reactor, emoji_or_list = nil|
33
+ return unless emoji_or_list
34
+
35
+ emojis = if emoji_or_list.is_a?(Array)
36
+ emoji_or_list
37
+ else
38
+ [emoji_or_list]
39
+ end
40
+
41
+ # TODO performance
42
+ # optimize by using a single query
43
+ emojis
44
+ .compact
45
+ .uniq
46
+ .each do |emoji|
47
+ reaction = reactions.find_or_create_by(reactor: reactor, emoji: emoji)
48
+ reaction.save unless reaction.persisted?
49
+ end
50
+
51
+ self
52
+ end
53
+
54
+ define_method :remove_reactions do |reactor, emoji_or_list = nil|
55
+ return unless emoji_or_list
56
+
57
+ emojis = if emoji_or_list.is_a?(Array)
58
+ emoji_or_list
59
+ else
60
+ [emoji_or_list]
61
+ end
62
+
63
+ reactions
64
+ .where(reactor: reactor, emoji: emojis.compact.uniq)
65
+ .destroy_all
66
+
67
+ self
68
+ end
31
69
  end
32
70
  end
33
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsReactable
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_reactable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - contact@public-reactions.com
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-25 00:00:00.000000000 Z
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord