acts_as_reactable 0.2.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bfe9ba5656866d662b502fc6c04288c05e8b2ecde0a48a4812f5dc5c12607da
4
- data.tar.gz: 1f16f5de5bb6fd29c7e9f2afee9b22d8d692d88d8a3103e208e4764124a30118
3
+ metadata.gz: 17ef23a371d7abdef4f6134dec6841468f1fe8ac720a4087179c7760f57088d3
4
+ data.tar.gz: c7da600b50dde0fc8d1c432993f667dd0b96c7f2b8d081d20113549fda784577
5
5
  SHA512:
6
- metadata.gz: 372e7d0ab30345f4b5870338d4c7b8006041d50b84e25c93d853b0dd8ed961724fac9fb2b7005dbc2b0ad3de7c80469812bc16e14ac3520fdb3e4933bf14be58
7
- data.tar.gz: 168c5407c7934db6b4ddd4a3b3d86684af2362e1a6818e90bcdbdfcac84aa68c6163d212e3c43fb84b99597fbb3579541f4956b191a6f39010cb5f623cab234a
6
+ metadata.gz: e573dd63f8e9e87bc7a35d28b0e0462992a2860bc4d88a482e96e6a884aa14e56fd1e63013bfa33726fef3acd96c32138b81acdb94398e6395edf45771aea592
7
+ data.tar.gz: '09a8c3d23913f425d3a7f100dfe70f57b5bef53a337a45684b5417e9b195198ec543af5de098512644a8f8ed9b203513a9e1d8bb51e5d8509b75a72bf5bfd46d'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
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
+
17
+ ## [0.3.0](https://github.com/public-reactions/acts_as_reactable/compare/v0.2.8...v0.3.0) (2022-04-25)
18
+
19
+
20
+ ### Features
21
+
22
+ * enable multiple reactions to single subject for each user ([ec32854](https://github.com/public-reactions/acts_as_reactable/commit/ec32854897249a53d2a17b41509d7c7ab69bb680))
23
+
24
+ ### [0.2.8](https://github.com/public-reactions/acts_as_reactable/compare/v0.2.7...v0.2.8) (2022-04-18)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * remove needless validators ([2b6aef3](https://github.com/public-reactions/acts_as_reactable/commit/2b6aef3379da7d3aa9a969440720b04d005a78af))
30
+ * **spec:** test spec for #destroy_reaction_from ([ef79759](https://github.com/public-reactions/acts_as_reactable/commit/ef79759ce20109299e8bffa78ad4b7f0784510e0))
31
+
3
32
  ### [0.2.7](https://github.com/public-reactions/acts_as_reactable/compare/v0.2.6...v0.2.7) (2022-04-14)
4
33
 
5
34
 
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  ![build](https://github.com/public-reactions/acts_as_reactable/actions/workflows/main.yml/badge.svg)
4
4
  [![codecov](https://codecov.io/gh/public-reactions/acts_as_reactable/branch/main/graph/badge.svg?token=OVDCJIQAFN)](https://codecov.io/gh/public-reactions/acts_as_reactable)
5
5
  [![Gem Version](https://badge.fury.io/rb/acts_as_reactable.svg)](https://badge.fury.io/rb/acts_as_reactable)
6
+ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
6
7
 
7
8
  ## Installation
8
9
 
@@ -14,25 +15,83 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
15
 
15
16
  $ gem install acts_as_reactable
16
17
 
17
- ## Usage
18
+ ## Preparations
18
19
 
19
20
  ### 1. create the Reaction model
20
21
 
22
+ ```ruby
23
+ # rails g migration create_reactions
24
+
25
+ # with default id type
26
+ create_table :reactions do |t|
27
+ t.references :reactable, polymorphic: true, null: false
28
+ t.references :reactor, polymorphic: true, null: false
29
+ t.string :emoji, null: false, index: true
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'
33
+ end
34
+
35
+ # with uuid id
36
+ create_table :reactions, id: :uuid do |t|
37
+ t.references :reactable, polymorphic: true, type: :uuid, null: false
38
+ t.references :reactor, polymorphic: true, type: :uuid, null: false
39
+ ...
40
+ end
41
+ ```
42
+
21
43
  ### 2. annotate reactable and reactor models
22
44
 
23
- ### 3. creating/updating reactions
45
+ ```ruby
46
+ # reactable
47
+ class Post < ApplicationRecord
48
+ acts_as_reactable
49
+ end
50
+
51
+ # reactor
52
+ class User < ApplicationRecord
53
+ acts_as_reactor
54
+ end
55
+ ```
56
+
57
+ ## Usage
58
+
59
+ ### adding/updating reactions
60
+
61
+ ```ruby
62
+ post.add_reactions(user, "😀")
63
+ post.add_reactions(user, ["😞", "🙃"])
64
+ ```
65
+
66
+ ### removing reactions
67
+
68
+ ```ruby
69
+ post.remove_reactions(user, "😀")
70
+ post.remove_reactions(user, ["😞", "🙃"])
71
+ ```
72
+
73
+ ### private opinion on reactable from reactor
74
+
75
+ ```ruby
76
+ reactions = ActsAsReactable::Reaction.where(reactable: post, reactor: user)
77
+ ```
78
+
79
+ ### group, count and sort to get a summary of public opinion
24
80
 
25
- ### 4. deleting reactions
81
+ ```ruby
82
+ ActsAsReactable::Reaction.where(reactable: reactor).group(:emoji).order('count_id DESC').count(:id)
26
83
 
27
- ### 5. aggregate/summary of reactions
84
+ # { "😀": 10, "😢": 5, "😣": 1 }
85
+ ```
28
86
 
29
87
  ## FAQ
30
88
 
31
- ### Why saving the emoji character instead of "smily_face"
89
+ ### Why saving "😂" instead of "face_with_tears_of_joy"
32
90
 
33
- - Technically, there's no concrete name/key/id for emoji (and modifiers like skin tone). The [CLDR short names](https://unicode.org/emoji/format.html#col-name) "vary by language" and "may change".
34
- - It's easier to store since any modern database supports
35
- - It's easier to validate with libs/regex (e.g. [unicode-emoji](https://github.com/janlelis/unicode-emoji))
91
+ - Technically, there's no concrete name/key/id for emoji (and modifiers like skin tone). The [CLDR short names](https://unicode.org/emoji/format.html#col-name) "vary by language" and "may change", besides, are those names case sensitive? Should we use `-`, `_` or ` ` as divider? How to append tone variant? There are several error prone decisions to make.
92
+ - It's easier to store since all modern database supports encodings (e.g. UTF-8) for unicode characters.
93
+ - It's easy to validate with libs/regex (e.g. [unicode-emoji](https://github.com/janlelis/unicode-emoji)).
94
+ - It takes less size on disk to store (and presumably less time to index/sort/match) one unicode character 😂 (4 bytes) than `face with tears of joy` (22 bytes). [This is a great article to explain how utf-8 works](https://sethmlarson.dev/blog/utf-8)
36
95
 
37
96
  ## Development
38
97
 
@@ -42,7 +101,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
42
101
 
43
102
  ## Contributing
44
103
 
45
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/acts_as_reactable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/acts_as_reactable/blob/main/CODE_OF_CONDUCT.md).
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/public-reactions/acts_as_reactable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/acts_as_reactable/blob/main/CODE_OF_CONDUCT.md).
46
105
 
47
106
  ## License
48
107
 
@@ -25,12 +25,47 @@ module ActsAsReactable
25
25
  return destroy_reaction_from(reactor)
26
26
  end
27
27
 
28
- reaction = reactions.find_or_initialize_by(reactor: reactor)
29
- reaction.emoji = emoji
30
- reaction.save
31
-
28
+ reaction = reactions.where({reactor: reactor, emoji: emoji}).first_or_create
32
29
  reaction
33
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
34
69
  end
35
70
  end
36
71
  end
@@ -5,8 +5,6 @@ module ActsAsReactable
5
5
  belongs_to :reactable, polymorphic: true
6
6
  belongs_to :reactor, polymorphic: true
7
7
 
8
- validates_presence_of :reactable_id
9
- validates_presence_of :reactor_id
10
8
  validates_format_of :emoji, with: Unicode::Emoji::REGEX
11
9
  end
12
10
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsReactable
4
- VERSION = "0.2.7"
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.2.7
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-14 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