action-store 0.3.3 → 0.4.2

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: d3f20d025cda1c0724c4b9b053288efb85d714069d90fff728c59affc3a47da0
4
- data.tar.gz: c94553d106707178ffd85c2c2e2aef41feaaa040441faf4fca96356ec46a5fc2
3
+ metadata.gz: e856a3c707a03c3b16ad3e5ab629ce80741c8a2ba929e70cec2b2b84c75187c9
4
+ data.tar.gz: df3ccb912b970c2e3b8512720382d09551880abb9f7b0c8e48c57e1b0d421318
5
5
  SHA512:
6
- metadata.gz: cb20154fbc1dba325e3a40415d7940b37f7e52aa1dfc877da24ba6cdb5691f0f99788b7a737688f083ff37ee8659393496344febaf0d62b97e08db5f61b378fb
7
- data.tar.gz: 3f925347dca40f57691af345af33e47fbd58d0b6b24deaabe0d89f3ebad3d24874d050783387d3dfc216fe500d3f31cf5d7682e99b5ac5935fc7760296ed7765
6
+ metadata.gz: bc193bc4b1a8f8a56b7a8c5ea4eee112e0a794e4b300d9abff91d3409f58976bea1312b84ba988c4a74e8e69934c312c3e0f648101738ac754f9d7a76998e4e5
7
+ data.tar.gz: 76daff55736babee4d1d58a80eeb28d624a585832b7e52f84f05235a30cc42c78a3290c241559486731ffce82e066b267d00d03e1109f0f14a1a1555d81b73c6
@@ -1,3 +1,23 @@
1
+ 0.4.2
2
+ -----
3
+
4
+ - Add `UNIQUE INDEX` on `:action_type, :target_type, :target_id, :user_type, :user_id` for makesure action uniqueness.
5
+ > NOTE! If you already have actions in database, the new migration may have issue on `db:migrate`,
6
+ > so you need fix them by remove duplicate.
7
+ - Now `create_action` method use database to makesure unique.
8
+
9
+ **Upgrade from 0.3.x:**
10
+
11
+ You must use `rails g action_store:install` to generate the new migration file.
12
+
13
+ ```bash
14
+ $ rails g action_store:install
15
+ conflict config/initializers/action_store.rb
16
+ Overwrite config/initializers/action_store.rb? (enter "h" for help) [Ynaqdhm] n
17
+ skip config/initializers/action_store.rb
18
+ Copied migration 20181121061544_add_unique_index_to_actions.action_store.rb from action_store
19
+ ````
20
+
1
21
  0.3.3
2
22
  -----
3
23
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ActionStore
2
2
  -----------
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/action-store.svg)](https://badge.fury.io/rb/action-store) [![Build Status](https://travis-ci.org/rails-engine/action-store.svg)](https://travis-ci.org/rails-engine/action-store) [![Code Climate](https://codeclimate.com/github/rails-engine/action-store/badges/gpa.svg)](https://codeclimate.com/github/rails-engine/action-store) [![codecov.io](https://codecov.io/github/rails-engine/action-store/coverage.svg?branch=master)](https://codecov.io/github/rails-engine/action-store?branch=master)
4
+ [![Gem Version](https://badge.fury.io/rb/action-store.svg)](https://badge.fury.io/rb/action-store) [![Build Status](https://travis-ci.org/rails-engine/action-store.svg)](https://travis-ci.org/rails-engine/action-store) [![codecov.io](https://codecov.io/github/rails-engine/action-store/coverage.svg?branch=master)](https://codecov.io/github/rails-engine/action-store?branch=master)
5
5
 
6
6
  Store different kinds of actions (Like, Follow, Star, Block, etc.) in a single table via ActiveRecord Polymorphic Associations.
7
7
 
@@ -23,6 +23,12 @@ And more and more.
23
23
  | `action_option` | Secondary option for storing your custom status, or null if unneeded. |
24
24
  | `target_type`, `target_id` | Polymorphic Association for different `Target` models [User, Post, Comment] |
25
25
 
26
+ ### Uniqueness
27
+
28
+ > version: ">= 0.4.0"
29
+
30
+ The have database unique index on fields: `[action_type, target_type, target_id, user_type, user_id]` for keep uniqueness for same action from user to target.
31
+
26
32
  ## Usage
27
33
 
28
34
  ```rb
@@ -33,7 +39,7 @@ and run `bundle install`
33
39
 
34
40
  Generate Migrations:
35
41
 
36
- ```
42
+ ```bash
37
43
  $ rails g action_store:install
38
44
  create config/initializers/action_store.rb
39
45
  migration 20170208024704_create_actions.rb from action_store
@@ -47,7 +53,7 @@ Use `action_store` to define actions:
47
53
 
48
54
  app/models/user.rb
49
55
 
50
- ```
56
+ ```rb
51
57
  class User < ActiveRecord::Base
52
58
  action_store <action_type>, <target>, opts
53
59
  end
@@ -124,21 +130,55 @@ irb> action.present?
124
130
  true
125
131
  ```
126
132
 
127
- Other following use cases:
133
+ **Other following use cases:**
128
134
 
129
135
  ```rb
130
136
  # @user1 -> follow @user2
131
- @user1.create_action(:follow, target: @user2)
132
- @user1.reload.following_count => 1
133
- @user2.reload.followers_count_ => 1
134
- @user1.follow_user?(@user2) => true
137
+ irb> @user1.create_action(:follow, target: @user2)
138
+ irb> @user1.reload.following_count
139
+ => 1
140
+ irb> @user2.reload.followers_count_
141
+ => 1
142
+ irb> @user1.follow_user?(@user2)
143
+ => true
144
+
135
145
  # @user2 -> follow @user1
136
- @user2.create_action(:follow, target: @user1)
137
- @user2.follow_user?(@user1) => true
146
+ irb> @user2.create_action(:follow, target: @user1)
147
+ irb> @user2.follow_user?(@user1)
148
+ => true
149
+
138
150
  # @user1 -> follow @user3
139
- @user1.create_action(:follow, target: @user3)
151
+ irb> @user1.create_action(:follow, target: @user3)
152
+
140
153
  # @user1 -> unfollow @user3
141
- @user1.destroy_action(:follow, target: @user3)
154
+ irb> @user1.destroy_action(:follow, target: @user3)
155
+ ```
156
+
157
+ **Subscribe cases:**
158
+
159
+ Sometimes, you may need use `action_option` option.
160
+
161
+ For example, user to subscribe a issue (like GitHub Issue) on issue create, and they wants keep in subscribe list on unsubscribe for makesure next comment will not subscribe this issue again.
162
+
163
+ So, in this case, we should not use `@user.unsubscribe_issue` method to destroy action record, we need set a value on `action_option` to mark this subscribe is `ignore`.
164
+
165
+ ```rb
166
+ irb> User.create_action(:subscribe, target: @issue, user: @user)
167
+ irb> @user.subscribe_issue?(@issue)
168
+ => true
169
+
170
+ irb> User.create_action(:subscribe, target: @issue, user: @user, action_option: "ignore")
171
+ irb> @user.subscribe_issue?(@issue)
172
+ => true
173
+
174
+ irb> action = User.find_action(:subscribe, target: @issue, user: @user)
175
+ irb> action.action_option
176
+ => "ignore"
177
+
178
+ irb> @issue.subscribe_by_user_actions.count
179
+ => 1
180
+ irb> @issue.subscribe_by_user_actions.where(action_option: nil).count
181
+ => 0
142
182
  ```
143
183
 
144
184
  ## Built-in relations and methods
@@ -0,0 +1,5 @@
1
+ class AddUniqueIndexToActions < ActiveRecord::Migration[5.0]
2
+ def change
3
+ add_index :actions, [:action_type, :target_type, :target_id, :user_type, :user_id], unique: true, name: :uk_action_target_user
4
+ end
5
+ end
@@ -81,10 +81,16 @@ module ActionStore
81
81
  defined_action = find_defined_action(opts[:action_type], opts[:target_type])
82
82
  return false if defined_action.nil?
83
83
 
84
- action = Action.find_or_create_by(where_opts(opts))
85
- if opts[:action_option]
86
- action.update_attribute(:action_option, opts[:action_option])
84
+ # create! for raise RecordNotUnique
85
+ begin
86
+ action = Action.find_or_create_by!(where_opts(opts))
87
+ action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
88
+ rescue ActiveRecord::RecordNotUnique
89
+ # update action_option on exist
90
+ action = Action.where(where_opts(opts)).take
91
+ action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
87
92
  end
93
+
88
94
  reset_counter_cache(action, defined_action)
89
95
  true
90
96
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ActionStore
3
- VERSION = "0.3.3"
3
+ VERSION = "0.4.2"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action-store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: factory_girl
42
+ name: factory_bot
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -94,6 +94,7 @@ files:
94
94
  - app/models/action.rb
95
95
  - config/initializers/action_store.rb
96
96
  - db/migrate/20170204035500_create_actions.rb
97
+ - db/migrate/20181121052638_add_unique_index_to_actions.rb
97
98
  - lib/action-store.rb
98
99
  - lib/action_store/configuration.rb
99
100
  - lib/action_store/engine.rb
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  version: '0'
122
123
  requirements: []
123
124
  rubyforge_project:
124
- rubygems_version: 2.7.6
125
+ rubygems_version: 2.7.8
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: Store difference kind of actions (Like, Follow, Star, Block ...) in one table.