action-store 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +43 -23
- data/README.md +71 -18
- data/Rakefile +1 -2
- data/db/migrate/20170204035500_create_actions.rb +2 -2
- data/db/migrate/20181121052638_add_unique_index_to_actions.rb +4 -1
- data/lib/action-store.rb +1 -0
- data/lib/action_store/mixin.rb +115 -111
- data/lib/action_store/version.rb +1 -1
- data/lib/generators/action_store/install_generator.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d467de9d475a9a67d39dc4f7698be8b7a315d4a595e148c2a08f9ca3a5eff11
|
4
|
+
data.tar.gz: 9f82b594f99b6b7a2b27f4aa8f2abb9be2b3b46ad1f366d76a8f8a939b829828
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3756a70605f0d4e51a06442b92df8706b776115b614ed8771e85ac4ce861a2acff69d7fcbe5d2e92622d17506ae209bc5561ff46abb5418bc85d8bd0dcce7f12
|
7
|
+
data.tar.gz: cadcd1a2f92975616885f446eda4530c5883bf0eccea64a28c41f94ccc38fa009b8713e5ffd67d513872449be7491afcb514eb7accd5c7775e7f3bd42bba3a75
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,30 @@
|
|
1
|
-
1.1.0
|
2
|
-
|
1
|
+
## 1.1.0
|
2
|
+
|
3
|
+
- Allows to configure different Models / Tables for store actions. (#16)
|
4
|
+
|
5
|
+
```rb
|
6
|
+
# app/models/like.rb
|
7
|
+
class Like < Action
|
8
|
+
self.table_name = "likes"
|
9
|
+
end
|
10
|
+
```
|
11
|
+
|
12
|
+
```rb
|
13
|
+
# app/models/user.rb
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
action_store :like, :post, counter_cache: true, action_class_name: "Like"
|
16
|
+
action_store :like, :comment, counter_cache: true, action_class_name: "Like"
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
## 1.0.0
|
3
21
|
|
4
22
|
- Require Rails >= 5.2.
|
5
23
|
- Fix primary key for use `bigint` column type.
|
6
24
|
|
7
|
-
0.4.3
|
8
|
-
|
25
|
+
0.4.3
|
26
|
+
|
27
|
+
---
|
9
28
|
|
10
29
|
- Auto reload `target`, when action/unaction target.
|
11
30
|
|
@@ -20,8 +39,7 @@ irb> post.likes_count
|
|
20
39
|
=> 0
|
21
40
|
```
|
22
41
|
|
23
|
-
0.4.2
|
24
|
-
-----
|
42
|
+
## 0.4.2
|
25
43
|
|
26
44
|
- Add `UNIQUE INDEX` on `:action_type, :target_type, :target_id, :user_type, :user_id` for makesure action uniqueness.
|
27
45
|
> NOTE! If you already have actions in database, the new migration may have issue on `db:migrate`,
|
@@ -38,25 +56,27 @@ $ rails g action_store:install
|
|
38
56
|
Overwrite config/initializers/action_store.rb? (enter "h" for help) [Ynaqdhm] n
|
39
57
|
skip config/initializers/action_store.rb
|
40
58
|
Copied migration 20181121061544_add_unique_index_to_actions.action_store.rb from action_store
|
41
|
-
|
59
|
+
```
|
42
60
|
|
43
|
-
0.3.3
|
44
|
-
-----
|
61
|
+
## 0.3.3
|
45
62
|
|
46
63
|
- Fix for supports Rails 5.2;
|
47
64
|
|
48
|
-
0.3.2
|
49
|
-
|
65
|
+
0.3.2
|
66
|
+
|
67
|
+
---
|
50
68
|
|
51
69
|
- Gem dependency for Rails 5.x.
|
52
70
|
|
53
|
-
0.3.1
|
54
|
-
|
71
|
+
0.3.1
|
72
|
+
|
73
|
+
---
|
55
74
|
|
56
75
|
- Fix that `:user_counter_cache` was incorrect, it not count with target_type.
|
57
76
|
|
58
|
-
0.3.0
|
59
|
-
|
77
|
+
0.3.0
|
78
|
+
|
79
|
+
---
|
60
80
|
|
61
81
|
- Fix for action_store in Model that not named `User`.
|
62
82
|
- Fix has_many name when `User` model in a namespace.
|
@@ -78,8 +98,7 @@ end
|
|
78
98
|
@blog_user.like_post(@post), @post.like_by_blog_users, @post.like_by_blog_user_actions
|
79
99
|
```
|
80
100
|
|
81
|
-
0.2.2
|
82
|
-
-----
|
101
|
+
## 0.2.2
|
83
102
|
|
84
103
|
- Fix #2 support Target that under a namespace.
|
85
104
|
|
@@ -87,18 +106,19 @@ end
|
|
87
106
|
action_store :like, :blog_post, class_name: 'Blog::Post'
|
88
107
|
```
|
89
108
|
|
90
|
-
0.2.1
|
91
|
-
-----
|
109
|
+
## 0.2.1
|
92
110
|
|
93
111
|
- Use `ActiveSupport.on_load` to hook into Active Record.
|
94
112
|
|
95
|
-
0.2.0
|
96
|
-
|
113
|
+
0.2.0
|
114
|
+
|
115
|
+
---
|
97
116
|
|
98
117
|
- New API, define action in User model.
|
99
118
|
- Builtin Many-to-Many relations and methods.
|
100
119
|
|
101
|
-
0.1.0
|
102
|
-
|
120
|
+
0.1.0
|
121
|
+
|
122
|
+
---
|
103
123
|
|
104
124
|
- First release.
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
ActionStore
|
2
|
-
-----------
|
1
|
+
## ActionStore
|
3
2
|
|
4
|
-
[![Gem Version](https://badge.fury.io/rb/action-store.svg)](https://badge.fury.io/rb/action-store) [![
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/action-store.svg)](https://badge.fury.io/rb/action-store) [![build](https://github.com/rails-engine/action-store/workflows/build/badge.svg)](https://github.com/rails-engine/action-store/actions?query=workflow%3Abuild) [![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
4
|
|
6
5
|
Store different kinds of actions (Like, Follow, Star, Block, etc.) in a single table via ActiveRecord Polymorphic Associations.
|
7
6
|
|
@@ -17,10 +16,10 @@ And more and more.
|
|
17
16
|
|
18
17
|
## Basic table struct
|
19
18
|
|
20
|
-
| Column
|
21
|
-
|
|
22
|
-
| `action_type`
|
23
|
-
| `action_option`
|
19
|
+
| Column | Description |
|
20
|
+
| -------------------------- | --------------------------------------------------------------------------- |
|
21
|
+
| `action_type` | The type of action [like, watch, follow, star, favorite] |
|
22
|
+
| `action_option` | Secondary option for storing your custom status, or null if unneeded. |
|
24
23
|
| `target_type`, `target_id` | Polymorphic Association for different `Target` models [User, Post, Comment] |
|
25
24
|
|
26
25
|
### Uniqueness
|
@@ -51,9 +50,8 @@ and run `rails db:migrate`.
|
|
51
50
|
|
52
51
|
Use `action_store` to define actions:
|
53
52
|
|
54
|
-
app/models/user.rb
|
55
|
-
|
56
53
|
```rb
|
54
|
+
# app/models/user.rb
|
57
55
|
class User < ActiveRecord::Base
|
58
56
|
action_store <action_type>, <target>, opts
|
59
57
|
end
|
@@ -61,18 +59,18 @@ end
|
|
61
59
|
|
62
60
|
#### Convention Over Configuration:
|
63
61
|
|
64
|
-
| action, target
|
65
|
-
|
66
|
-
| `action_store :like, :post`
|
67
|
-
| `action_store :like, :post, counter_cache: true`
|
68
|
-
| `action_store :star, :project, class_name: 'Repository'`
|
69
|
-
| `action_store :follow, :user`
|
70
|
-
| `action_store :follow, :user, counter_cache: 'followers_count', user_counter_cache: 'following_count'` | `User`
|
62
|
+
| action, target | Target Model | Target `counter_cache_field` | User `counter_cache_field` | Target has_many | User has_many |
|
63
|
+
| ------------------------------------------------------------------------------------------------------ | ------------- | ---------------------------- | -------------------------- | --------------------------------------------------------------- | --------------------------------------------------------- |
|
64
|
+
| `action_store :like, :post` | `Post` | | | `has_many :like_by_user_actions`, `has_many :like_by_users` | `has_many :like_post_actions`, `has_many :like_posts` |
|
65
|
+
| `action_store :like, :post, counter_cache: true` | `Post` | `likes_count` | | `has_many :like_by_user_actions`, `has_many :like_by_users` | `has_many :like_post_actions`, `has_many :like_posts` |
|
66
|
+
| `action_store :star, :project, class_name: 'Repository'` | `Repository ` | `stars_count` | `star_projects_count` | `has_many :star_by_user_actions`, `has_many :star_by_users` |
|
67
|
+
| `action_store :follow, :user` | `User` | `follows_count` | `follow_users_count` | `has_many :follow_by_user_actions`, `has_many :follow_by_users` | `has_many :follow_user_actions`, `has_many :follow_users` |
|
68
|
+
| `action_store :follow, :user, counter_cache: 'followers_count', user_counter_cache: 'following_count'` | `User` | `followers_count ` | `following_count ` | `has_many :follow_by_user_actions`, `has_many :follow_by_users` | `has_many :follow_user_actions`, `has_many :follow_users` |
|
71
69
|
|
72
70
|
for example:
|
73
71
|
|
74
72
|
```rb
|
75
|
-
# app/models/
|
73
|
+
# app/models/user.rb
|
76
74
|
class User < ActiveRecord::Base
|
77
75
|
action_store :like, :post, counter_cache: true
|
78
76
|
action_store :star, :post, counter_cache: true, user_counter_cache: true
|
@@ -137,7 +135,7 @@ true
|
|
137
135
|
irb> @user1.create_action(:follow, target: @user2)
|
138
136
|
irb> @user1.reload.following_count
|
139
137
|
=> 1
|
140
|
-
irb> @user2.reload.
|
138
|
+
irb> @user2.reload.followers_count
|
141
139
|
=> 1
|
142
140
|
irb> @user1.follow_user?(@user2)
|
143
141
|
=> true
|
@@ -181,6 +179,61 @@ irb> @issue.subscribe_by_user_actions.where(action_option: nil).count
|
|
181
179
|
=> 0
|
182
180
|
```
|
183
181
|
|
182
|
+
## Use different tables for store actions.
|
183
|
+
|
184
|
+
> since 1.1.0
|
185
|
+
|
186
|
+
Sometimes, you may want to store actions into different table, for example, **Like** scenarios often have a lot of data, we wants store them into `likes` table.
|
187
|
+
|
188
|
+
Create a migration and model
|
189
|
+
|
190
|
+
```bash
|
191
|
+
$ rails g migration create_likes
|
192
|
+
```
|
193
|
+
|
194
|
+
And then modify this migration file to let this table have same struct like the `actions`
|
195
|
+
|
196
|
+
```rb
|
197
|
+
class CreateLikes < ActiveRecord::Migration[6.1]
|
198
|
+
def change
|
199
|
+
create_table :likes do |t|
|
200
|
+
t.string :action_type, null: false
|
201
|
+
t.string :action_option
|
202
|
+
t.string :target_type
|
203
|
+
t.bigint :target_id
|
204
|
+
t.string :user_type
|
205
|
+
t.bigint :user_id
|
206
|
+
|
207
|
+
t.timestamps
|
208
|
+
end
|
209
|
+
|
210
|
+
add_index :likes, %i[user_type user_id action_type]
|
211
|
+
add_index :likes, %i[target_type target_id action_type]
|
212
|
+
add_index :likes, %i[action_type target_type target_id user_type user_id], unique: true, name: :uk_likes_target_user
|
213
|
+
end
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
Create a `app/model/like.rb` model file:
|
218
|
+
|
219
|
+
```rb
|
220
|
+
class Like < Action
|
221
|
+
self.table_name = "likes"
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
And then change you `action_store` define to special some case to use Like model:
|
226
|
+
|
227
|
+
```rb
|
228
|
+
# app/models/user.rb
|
229
|
+
class User < ActiveRecord::Base
|
230
|
+
action_store :like, :post, counter_cache: true, action_class_name: "Like"
|
231
|
+
action_store :like, :comment, counter_cache: true, action_class_name: "Like"
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
Now, `user.like_post`, `user.like_comment` will store the actions into `likes` table.
|
236
|
+
|
184
237
|
## Built-in relations and methods
|
185
238
|
|
186
239
|
When you call `action_store`, ActionStore will define many-to-many relations for User and Target models.
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
16
16
|
rdoc.rdoc_files.include("lib/**/*.rb")
|
17
17
|
end
|
18
18
|
|
19
|
-
APP_RAKEFILE = File.expand_path("
|
19
|
+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
20
20
|
load "rails/tasks/engine.rake"
|
21
21
|
load "rails/tasks/statistics.rake"
|
22
22
|
|
@@ -30,5 +30,4 @@ Rake::TestTask.new(:test) do |t|
|
|
30
30
|
t.verbose = false
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
33
|
task default: :test
|
@@ -13,7 +13,7 @@ class CreateActions < ActiveRecord::Migration[5.2]
|
|
13
13
|
t.timestamps
|
14
14
|
end
|
15
15
|
|
16
|
-
add_index :actions, [
|
17
|
-
add_index :actions, [
|
16
|
+
add_index :actions, %i[user_type user_id action_type]
|
17
|
+
add_index :actions, %i[target_type target_id action_type]
|
18
18
|
end
|
19
19
|
end
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class AddUniqueIndexToActions < ActiveRecord::Migration[5.2]
|
2
4
|
def change
|
3
|
-
add_index :actions, [
|
5
|
+
add_index :actions, %i[action_type target_type target_id user_type user_id], unique: true,
|
6
|
+
name: :uk_action_target_user
|
4
7
|
end
|
5
8
|
end
|
data/lib/action-store.rb
CHANGED
data/lib/action_store/mixin.rb
CHANGED
@@ -33,32 +33,36 @@ module ActionStore
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def action_store(action_type, name,
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
def action_store(action_type, name, class_name: nil, action_class_name: "Action", counter_cache: nil, user_counter_cache: nil)
|
37
|
+
name = name.to_s
|
38
|
+
|
39
|
+
class_name ||= name.classify
|
40
|
+
target_klass = class_name.constantize
|
41
|
+
action_klass = action_class_name.constantize
|
40
42
|
action_type = action_type.to_s
|
41
|
-
if
|
43
|
+
if counter_cache == true
|
42
44
|
# @post.stars_count
|
43
|
-
|
45
|
+
counter_cache = "#{action_type.pluralize}_count"
|
44
46
|
end
|
45
|
-
if
|
47
|
+
if user_counter_cache == true
|
46
48
|
# @user.star_posts_count
|
47
|
-
|
49
|
+
user_counter_cache = "#{action_type}_#{name.pluralize}_count"
|
48
50
|
end
|
49
51
|
|
50
52
|
@defined_actions ||= []
|
51
53
|
action = {
|
52
|
-
action_name: name
|
54
|
+
action_name: name,
|
53
55
|
action_type: action_type,
|
56
|
+
action_klass: action_klass,
|
54
57
|
target_klass: target_klass,
|
55
58
|
target_type: target_klass.name,
|
56
|
-
counter_cache:
|
57
|
-
user_counter_cache:
|
59
|
+
counter_cache: counter_cache,
|
60
|
+
user_counter_cache: user_counter_cache
|
58
61
|
}
|
59
62
|
@defined_actions << action
|
60
63
|
|
61
|
-
define_relations(
|
64
|
+
define_relations(action_klass: action_klass, target_klass: target_klass,
|
65
|
+
action_type: action_type, action_name: name)
|
62
66
|
end
|
63
67
|
|
64
68
|
def find_action(action_type, opts)
|
@@ -70,7 +74,7 @@ module ActionStore
|
|
70
74
|
defined_action = find_defined_action(opts[:action_type], opts[:target_type])
|
71
75
|
return nil if defined_action.nil?
|
72
76
|
|
73
|
-
|
77
|
+
defined_action[:action_klass].find_by(where_opts(opts))
|
74
78
|
end
|
75
79
|
|
76
80
|
def create_action(action_type, opts)
|
@@ -84,11 +88,11 @@ module ActionStore
|
|
84
88
|
|
85
89
|
# create! for raise RecordNotUnique
|
86
90
|
begin
|
87
|
-
action =
|
91
|
+
action = defined_action[:action_klass].find_or_create_by!(where_opts(opts))
|
88
92
|
action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
|
89
93
|
rescue ActiveRecord::RecordNotUnique
|
90
94
|
# update action_option on exist
|
91
|
-
action =
|
95
|
+
action = defined_action[:action_klass].where(where_opts(opts)).take
|
92
96
|
action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
|
93
97
|
end
|
94
98
|
|
@@ -105,8 +109,9 @@ module ActionStore
|
|
105
109
|
defined_action = find_defined_action(opts[:action_type], opts[:target_type])
|
106
110
|
return false if defined_action.nil?
|
107
111
|
|
108
|
-
action =
|
109
|
-
return true
|
112
|
+
action = defined_action[:action_klass].where(where_opts(opts)).first
|
113
|
+
return true unless action
|
114
|
+
|
110
115
|
action.destroy
|
111
116
|
reset_counter_cache(action, defined_action)
|
112
117
|
true
|
@@ -114,16 +119,18 @@ module ActionStore
|
|
114
119
|
|
115
120
|
def reset_counter_cache(action, defined_action)
|
116
121
|
return false if action.blank?
|
122
|
+
|
117
123
|
if defined_action[:counter_cache] && action.target.present?
|
118
|
-
target_count =
|
124
|
+
target_count = defined_action[:action_klass].where(
|
119
125
|
action_type: defined_action[:action_type],
|
120
126
|
target_type: action.target_type,
|
121
127
|
target_id: action.target_id
|
122
128
|
).count
|
123
129
|
action.target.update_attribute(defined_action[:counter_cache], target_count)
|
124
130
|
end
|
131
|
+
|
125
132
|
if defined_action[:user_counter_cache] && action.user.present?
|
126
|
-
user_count =
|
133
|
+
user_count = defined_action[:action_klass].where(
|
127
134
|
action_type: defined_action[:action_type],
|
128
135
|
target_type: action.target_type,
|
129
136
|
user_type: action.user_type,
|
@@ -134,105 +141,102 @@ module ActionStore
|
|
134
141
|
end
|
135
142
|
|
136
143
|
private
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
user_klass.send(:define_method, full_action_name) do |target_or_id|
|
190
|
-
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
191
|
-
result = user_klass.create_action(action_type, target_type: target_klass.name,
|
192
|
-
target_id: target_id,
|
193
|
-
user: self)
|
194
|
-
target_or_id.reload if target_or_id.is_a?(target_klass)
|
195
|
-
self.reload
|
196
|
-
result
|
197
|
-
end
|
198
|
-
|
199
|
-
# @user.like_topic?
|
200
|
-
user_klass.send(:define_method, "#{full_action_name}?") do |target_or_id|
|
201
|
-
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
202
|
-
result = user_klass.find_action(action_type, target_type: target_klass.name,
|
144
|
+
|
145
|
+
def define_relations(target_klass:, action_klass:, action_type:, action_name:)
|
146
|
+
user_klass = self
|
147
|
+
|
148
|
+
# user, person
|
149
|
+
user_name = user_klass.table_name.underscore.singularize
|
150
|
+
|
151
|
+
# like_topic, follow_user
|
152
|
+
full_action_name = [action_type, action_name].join("_")
|
153
|
+
# like_user, follow_user
|
154
|
+
full_action_name_for_target = [action_type, "by", user_name].join("_")
|
155
|
+
# unlike_topic, unfollow_user
|
156
|
+
unaction_name = "un#{full_action_name}"
|
157
|
+
|
158
|
+
# @target.like_topic_actions, @target.follow_user_actions
|
159
|
+
has_many_name = [full_action_name, "actions"].join("_").to_sym
|
160
|
+
# @target.like_topics, @target.follow_users
|
161
|
+
has_many_through_name = full_action_name.pluralize.to_sym
|
162
|
+
|
163
|
+
# @user.like_by_user_actions, @user.follow_by_user_actions
|
164
|
+
has_many_name_for_target = [full_action_name_for_target, "actions"].join("_").to_sym
|
165
|
+
# @user.like_by_users, @user.follow_by_users
|
166
|
+
has_many_through_name_for_target = full_action_name_for_target.pluralize.to_sym
|
167
|
+
|
168
|
+
has_many_scope = lambda {
|
169
|
+
where(action_type: action_type, target_type: target_klass.name, user_type: user_klass.name)
|
170
|
+
}
|
171
|
+
|
172
|
+
# User has_many :like_topic_actions
|
173
|
+
user_klass.send :has_many, has_many_name, has_many_scope,
|
174
|
+
class_name: action_klass.name,
|
175
|
+
foreign_key: "user_id"
|
176
|
+
# User has_many :like_topics
|
177
|
+
user_klass.send :has_many, has_many_through_name,
|
178
|
+
through: has_many_name,
|
179
|
+
source: :target,
|
180
|
+
source_type: target_klass.name
|
181
|
+
|
182
|
+
# Topic has_many :like_user_actions
|
183
|
+
target_klass.send :has_many, has_many_name_for_target, has_many_scope,
|
184
|
+
foreign_key: :target_id,
|
185
|
+
class_name: action_klass.name
|
186
|
+
# Topic has_many :like_users
|
187
|
+
target_klass.send :has_many, has_many_through_name_for_target,
|
188
|
+
through: has_many_name_for_target,
|
189
|
+
source_type: user_klass.name,
|
190
|
+
source: :user
|
191
|
+
|
192
|
+
# @user.like_topic
|
193
|
+
user_klass.send(:define_method, full_action_name) do |target_or_id|
|
194
|
+
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
195
|
+
result = user_klass.create_action(action_type, target_type: target_klass.name,
|
203
196
|
target_id: target_id,
|
204
197
|
user: self)
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
# @user.unlike_topic
|
209
|
-
user_klass.send(:define_method, unaction_name) do |target_or_id|
|
210
|
-
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
211
|
-
result = user_klass.destroy_action(action_type, target_type: target_klass.name,
|
212
|
-
target_id: target_id,
|
213
|
-
user: self)
|
214
|
-
target_or_id.reload if target_or_id.is_a?(target_klass)
|
215
|
-
self.reload
|
216
|
-
result
|
217
|
-
end
|
198
|
+
target_or_id.reload if target_or_id.is_a?(target_klass)
|
199
|
+
reload
|
200
|
+
result
|
218
201
|
end
|
219
202
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
opts[:target_type] = opts[:target].class.name
|
228
|
-
opts[:target_id] = opts[:target].id
|
229
|
-
end
|
230
|
-
opts
|
203
|
+
# @user.like_topic?
|
204
|
+
user_klass.send(:define_method, "#{full_action_name}?") do |target_or_id|
|
205
|
+
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
206
|
+
result = user_klass.find_action(action_type, target_type: target_klass.name,
|
207
|
+
target_id: target_id,
|
208
|
+
user: self)
|
209
|
+
result.present?
|
231
210
|
end
|
232
211
|
|
233
|
-
|
234
|
-
|
212
|
+
# @user.unlike_topic
|
213
|
+
user_klass.send(:define_method, unaction_name) do |target_or_id|
|
214
|
+
target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
|
215
|
+
result = user_klass.destroy_action(action_type, target_type: target_klass.name,
|
216
|
+
target_id: target_id,
|
217
|
+
user: self)
|
218
|
+
target_or_id.reload if target_or_id.is_a?(target_klass)
|
219
|
+
reload
|
220
|
+
result
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def safe_action_opts(opts)
|
225
|
+
opts ||= {}
|
226
|
+
if opts[:user]
|
227
|
+
opts[:user_id] = opts[:user].id
|
228
|
+
opts[:user_type] = opts[:user].class.name
|
229
|
+
end
|
230
|
+
if opts[:target]
|
231
|
+
opts[:target_type] = opts[:target].class.name
|
232
|
+
opts[:target_id] = opts[:target].id
|
235
233
|
end
|
234
|
+
opts
|
235
|
+
end
|
236
|
+
|
237
|
+
def where_opts(opts)
|
238
|
+
opts.extract!(:action_type, :target_type, :target_id, :user_id, :user_type)
|
239
|
+
end
|
236
240
|
end
|
237
241
|
end
|
238
242
|
end
|
data/lib/action_store/version.rb
CHANGED
@@ -5,7 +5,7 @@ module ActionStore
|
|
5
5
|
module Generators
|
6
6
|
class InstallGenerator < Rails::Generators::Base
|
7
7
|
desc "Create ActionStore's base files"
|
8
|
-
source_root File.expand_path("
|
8
|
+
source_root File.expand_path("../../..", __dir__)
|
9
9
|
|
10
10
|
def add_initializer
|
11
11
|
template "config/initializers/action_store.rb", "config/initializers/action_store.rb"
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action-store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2017-02-04 00:00:00.000000000 Z
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '7'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: codecov
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
62
|
+
name: pg
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - ">="
|
@@ -73,7 +73,7 @@ dependencies:
|
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
76
|
+
name: simplecov
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - ">="
|
@@ -112,7 +112,7 @@ homepage: https://github.com/rails-engine/action-store
|
|
112
112
|
licenses:
|
113
113
|
- MIT
|
114
114
|
metadata: {}
|
115
|
-
post_install_message:
|
115
|
+
post_install_message:
|
116
116
|
rdoc_options: []
|
117
117
|
require_paths:
|
118
118
|
- lib
|
@@ -127,8 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
127
|
- !ruby/object:Gem::Version
|
128
128
|
version: '0'
|
129
129
|
requirements: []
|
130
|
-
rubygems_version: 3.
|
131
|
-
signing_key:
|
130
|
+
rubygems_version: 3.1.4
|
131
|
+
signing_key:
|
132
132
|
specification_version: 4
|
133
133
|
summary: Store difference kind of actions (Like, Follow, Star, Block ...) in one table.
|
134
134
|
test_files: []
|