action-store 0.3.2 → 0.3.3

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
- SHA1:
3
- metadata.gz: 6eba4261de49d8c71efd8375217b93c80d57ab45
4
- data.tar.gz: 20d7a28f9b8005110f4cd564d2a771b541b1beea
2
+ SHA256:
3
+ metadata.gz: d3f20d025cda1c0724c4b9b053288efb85d714069d90fff728c59affc3a47da0
4
+ data.tar.gz: c94553d106707178ffd85c2c2e2aef41feaaa040441faf4fca96356ec46a5fc2
5
5
  SHA512:
6
- metadata.gz: ed468a935c04a2714b19a63e9506c31faef01acd946e54ef22aeea3e9cec8973467293cc494f7f23470a9e6ea1d92b550e4d5c7b9f1c2676ad5241a3e81af368
7
- data.tar.gz: 9a4333d0d4e503aa20b643d4de570b494143581f564ee443fe6106f178f8889702eb19b5819e3591dfe6a9a7aca9f0f433f335e5bd24ea3b73140f8d1448e6cb
6
+ metadata.gz: cb20154fbc1dba325e3a40415d7940b37f7e52aa1dfc877da24ba6cdb5691f0f99788b7a737688f083ff37ee8659393496344febaf0d62b97e08db5f61b378fb
7
+ data.tar.gz: 3f925347dca40f57691af345af33e47fbd58d0b6b24deaabe0d89f3ebad3d24874d050783387d3dfc216fe500d3f31cf5d7682e99b5ac5935fc7760296ed7765
@@ -1,3 +1,8 @@
1
+ 0.3.3
2
+ -----
3
+
4
+ - Fix for supports Rails 5.2;
5
+
1
6
  0.3.2
2
7
  -----
3
8
 
data/README.md CHANGED
@@ -3,13 +3,13 @@ ActionStore
3
3
 
4
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)
5
5
 
6
- Store different kind of actions (Like, Follow, Star, Block ...) in one table via ActiveRecord Polymorphic Association.
6
+ Store different kinds of actions (Like, Follow, Star, Block, etc.) in a single table via ActiveRecord Polymorphic Associations.
7
7
 
8
- - Like Post/Comment/Reply ...
9
- - Watch/Subscribe Post
10
- - Follow User
11
- - Favorite Post
12
- - Read Notification/Message
8
+ - Like Posts/Comment/Reply ...
9
+ - Watch/Subscribe to Posts
10
+ - Follow Users
11
+ - Favorite Posts
12
+ - Read Notifications/Messages
13
13
 
14
14
  And more and more.
15
15
 
@@ -17,11 +17,11 @@ And more and more.
17
17
 
18
18
  ## Basic table struct
19
19
 
20
- | Field | Means |
20
+ | Column | Description |
21
21
  | ----- | ----- |
22
22
  | `action_type` | The type of action [like, watch, follow, star, favorite] |
23
- | `action_option` | Secondly option for store you custom status, or you can let it null if you don't needs it. |
24
- | `target_type`, `target_id` | Polymorphic Association for difference `Target` models [User, Post, Comment] |
23
+ | `action_option` | Secondary option for storing your custom status, or null if unneeded. |
24
+ | `target_type`, `target_id` | Polymorphic Association for different `Target` models [User, Post, Comment] |
25
25
 
26
26
  ## Usage
27
27
 
@@ -43,7 +43,7 @@ and run `rails db:migrate`.
43
43
 
44
44
  ### Define Actions
45
45
 
46
- You can use `action_store` to define actions:
46
+ Use `action_store` to define actions:
47
47
 
48
48
  app/models/user.rb
49
49
 
@@ -78,7 +78,7 @@ end
78
78
 
79
79
  ### Counter Cache
80
80
 
81
- And you need add counter_cache field to target, user table.
81
+ Add counter_cache field to target and user tables.
82
82
 
83
83
  ```rb
84
84
  add_column :users, :star_posts_count, :integer, default: 0
@@ -91,9 +91,9 @@ add_column :posts, :stars_count, :integer, default: 0
91
91
  add_column :comments, :likes_count, :integer, default: 0
92
92
  ```
93
93
 
94
- #### Now you can use like this:
94
+ #### Example usage:
95
95
 
96
- @user -> like @post
96
+ @user likes @post
97
97
 
98
98
  ```rb
99
99
  irb> User.create_action(:like, target: @post, user: @user)
@@ -104,7 +104,7 @@ irb> @post.reload.likes_count
104
104
  1
105
105
  ```
106
106
 
107
- @user1 -> unlike @user2
107
+ @user1 unlikes @user2
108
108
 
109
109
  ```rb
110
110
  irb> User.destroy_action(:follow, target: @post, user: @user)
@@ -115,7 +115,7 @@ irb> @post.reload.likes_count
115
115
  0
116
116
  ```
117
117
 
118
- Check @user1 is liked @post
118
+ Check if @user1 likes @post
119
119
 
120
120
  ```rb
121
121
  irb> action = User.find_action(:follow, target: @post, user: @user)
@@ -124,7 +124,7 @@ irb> action.present?
124
124
  true
125
125
  ```
126
126
 
127
- User follow cases:
127
+ Other following use cases:
128
128
 
129
129
  ```rb
130
130
  # @user1 -> follow @user2
@@ -141,11 +141,11 @@ User follow cases:
141
141
  @user1.destroy_action(:follow, target: @user3)
142
142
  ```
143
143
 
144
- ## Builtin relations and methods
144
+ ## Built-in relations and methods
145
145
 
146
- When you called `action_store`, ActionStore will define Many-to-Many relations for User and Target model.
146
+ When you call `action_store`, ActionStore will define many-to-many relations for User and Target models.
147
147
 
148
- for example:
148
+ For example:
149
149
 
150
150
  ```rb
151
151
  class User < ActiveRecord::Base
@@ -154,10 +154,10 @@ class User < ActiveRecord::Base
154
154
  end
155
155
  ```
156
156
 
157
- It will defines Many-to-Many relations:
157
+ Defines many-to-many relations:
158
158
 
159
- - For User model will defined: `<action>_<target>s` (like_posts)
160
- - For Target model will defined: `<action>_by_users` (like_by_users)
159
+ - For User model: `<action>_<target>s` (like_posts)
160
+ - For Target model: `<action>_by_users` (like_by_users)
161
161
 
162
162
  ```rb
163
163
  # for User model
@@ -175,7 +175,7 @@ has_many :like_by_user_actions
175
175
  has_many :like_by_users, through: :like_user_actions
176
176
  ```
177
177
 
178
- And `User` model will have methods:
178
+ And `User` model will now have methods:
179
179
 
180
180
  - @user.create_action(:like, target: @post)
181
181
  - @user.destroy_action(:like, target: @post)
data/Rakefile CHANGED
@@ -1,30 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  begin
2
- require 'bundler/setup'
3
+ require "bundler/setup"
3
4
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
6
  end
6
7
 
7
- require 'rdoc/task'
8
+ require "rdoc/task"
8
9
 
9
10
  RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'Actionstore'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
11
+ rdoc.rdoc_dir = "rdoc"
12
+ rdoc.title = "Actionstore"
13
+ rdoc.options << "--line-numbers"
14
+ rdoc.rdoc_files.include("README.md")
15
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
16
  end
16
17
 
17
18
  APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
- load 'rails/tasks/engine.rake'
19
- load 'rails/tasks/statistics.rake'
19
+ load "rails/tasks/engine.rake"
20
+ load "rails/tasks/statistics.rake"
20
21
 
21
- require 'bundler/gem_tasks'
22
- require 'rake/testtask'
22
+ require "bundler/gem_tasks"
23
+ require "rake/testtask"
23
24
 
24
25
  Rake::TestTask.new(:test) do |t|
25
- t.libs << 'lib'
26
- t.libs << 'test'
27
- t.pattern = 'test/**/*_test.rb'
26
+ t.libs << "lib"
27
+ t.libs << "test"
28
+ t.pattern = "test/**/*_test.rb"
28
29
  t.verbose = false
29
30
  end
30
31
 
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  # Auto generate with action-store gem.
2
3
  class Action < ActiveRecord::Base
3
4
  include ActionStore::Model
4
- end
5
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # ActionStore Config
2
3
  ActionStore.configure do
3
- end
4
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  class CreateActions < ActiveRecord::Migration[5.0]
2
3
  def change
3
4
  create_table :actions do |t|
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "action_store/version"
2
3
  require "action_store/configuration"
3
4
  require "action_store/engine"
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActionStore
2
3
  class Configuration
3
4
  end
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  module ActionStore
2
3
  class Engine < ::Rails::Engine
3
4
  isolate_namespace ActionStore
4
5
 
5
6
  initializer "action_store.init_action", after: :load_config_initializers do
6
7
  # Ensure eager_load Action model to init methods
7
- require 'action'
8
+ require "action"
8
9
  end
9
10
  end
10
- end
11
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActionStore
2
3
  module Mixin
3
4
  extend ActiveSupport::Concern
@@ -33,7 +34,6 @@ module ActionStore
33
34
 
34
35
  def action_store(action_type, name, opts = {})
35
36
  opts ||= {}
36
- target_type = name
37
37
  klass_name = opts[:class_name] || name.to_s.classify
38
38
  target_klass = klass_name.constantize
39
39
  action_type = action_type.to_s
@@ -110,7 +110,8 @@ module ActionStore
110
110
  if defined_action[:counter_cache] && action.target.present?
111
111
  target_count = Action.where(
112
112
  action_type: defined_action[:action_type],
113
- target: action.target
113
+ target_type: action.target_type,
114
+ target_id: action.target_id
114
115
  ).count
115
116
  action.target.update_attribute(defined_action[:counter_cache], target_count)
116
117
  end
@@ -118,7 +119,8 @@ module ActionStore
118
119
  user_count = Action.where(
119
120
  action_type: defined_action[:action_type],
120
121
  target_type: action.target_type,
121
- user: action.user
122
+ user_type: action.user_type,
123
+ user_id: action.user_id
122
124
  ).count
123
125
  action.user.update_attribute(defined_action[:user_counter_cache], user_count)
124
126
  end
@@ -126,103 +128,103 @@ module ActionStore
126
128
 
127
129
  private
128
130
 
129
- def define_relations(action)
130
- target_klass = action[:target_klass]
131
- action_type = action[:action_type]
132
- action_name = action[:action_name]
133
-
134
- user_klass = self
135
-
136
- # user, person
137
- user_name = user_klass.table_name.underscore.singularize
138
-
139
- # like_topic, follow_user
140
- full_action_name = [action_type, action_name].join('_')
141
- # like_user, follow_user
142
- full_action_name_for_target = [action_type, 'by', user_name].join('_')
143
- # unlike_topic, unfollow_user
144
- unaction_name = "un#{full_action_name}"
145
-
146
- # @target.like_topic_actions, @target.follow_user_actions
147
- has_many_name = [full_action_name, 'actions'].join('_').to_sym
148
- # @target.like_topics, @target.follow_users
149
- has_many_through_name = full_action_name.pluralize.to_sym
150
-
151
- # @user.like_by_user_actions, @user.follow_by_user_actions
152
- has_many_name_for_target = [full_action_name_for_target, 'actions'].join('_').to_sym
153
- # @user.like_by_users, @user.follow_by_users
154
- has_many_through_name_for_target = full_action_name_for_target.pluralize.to_sym
155
-
156
- has_many_scope = -> {
157
- where(action_type: action_type, target_type: target_klass.name, user_type: user_klass.name)
158
- }
159
-
160
- # User has_many :like_topic_actions
161
- user_klass.send :has_many, has_many_name, has_many_scope,
162
- class_name: 'Action',
163
- foreign_key: 'user_id'
164
- # User has_many :like_topics
165
- user_klass.send :has_many, has_many_through_name,
166
- through: has_many_name,
167
- source: :target,
168
- source_type: target_klass.name
169
-
170
- # Topic has_many :like_user_actions
171
- target_klass.send :has_many, has_many_name_for_target, has_many_scope,
172
- foreign_key: :target_id,
173
- class_name: 'Action'
174
- # Topic has_many :like_users
175
- target_klass.send :has_many, has_many_through_name_for_target,
176
- through: has_many_name_for_target,
177
- source_type: user_klass.name,
178
- source: :user
179
-
180
- # @user.like_topic
181
- user_klass.send(:define_method, full_action_name) do |target_or_id|
182
- target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
183
- result = user_klass.create_action(action_type, target_type: target_klass.name,
131
+ def define_relations(action)
132
+ target_klass = action[:target_klass]
133
+ action_type = action[:action_type]
134
+ action_name = action[:action_name]
135
+
136
+ user_klass = self
137
+
138
+ # user, person
139
+ user_name = user_klass.table_name.underscore.singularize
140
+
141
+ # like_topic, follow_user
142
+ full_action_name = [action_type, action_name].join("_")
143
+ # like_user, follow_user
144
+ full_action_name_for_target = [action_type, "by", user_name].join("_")
145
+ # unlike_topic, unfollow_user
146
+ unaction_name = "un#{full_action_name}"
147
+
148
+ # @target.like_topic_actions, @target.follow_user_actions
149
+ has_many_name = [full_action_name, "actions"].join("_").to_sym
150
+ # @target.like_topics, @target.follow_users
151
+ has_many_through_name = full_action_name.pluralize.to_sym
152
+
153
+ # @user.like_by_user_actions, @user.follow_by_user_actions
154
+ has_many_name_for_target = [full_action_name_for_target, "actions"].join("_").to_sym
155
+ # @user.like_by_users, @user.follow_by_users
156
+ has_many_through_name_for_target = full_action_name_for_target.pluralize.to_sym
157
+
158
+ has_many_scope = -> {
159
+ where(action_type: action_type, target_type: target_klass.name, user_type: user_klass.name)
160
+ }
161
+
162
+ # User has_many :like_topic_actions
163
+ user_klass.send :has_many, has_many_name, has_many_scope,
164
+ class_name: "Action",
165
+ foreign_key: "user_id"
166
+ # User has_many :like_topics
167
+ user_klass.send :has_many, has_many_through_name,
168
+ through: has_many_name,
169
+ source: :target,
170
+ source_type: target_klass.name
171
+
172
+ # Topic has_many :like_user_actions
173
+ target_klass.send :has_many, has_many_name_for_target, has_many_scope,
174
+ foreign_key: :target_id,
175
+ class_name: "Action"
176
+ # Topic has_many :like_users
177
+ target_klass.send :has_many, has_many_through_name_for_target,
178
+ through: has_many_name_for_target,
179
+ source_type: user_klass.name,
180
+ source: :user
181
+
182
+ # @user.like_topic
183
+ user_klass.send(:define_method, full_action_name) do |target_or_id|
184
+ target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
185
+ result = user_klass.create_action(action_type, target_type: target_klass.name,
186
+ target_id: target_id,
187
+ user: self)
188
+ self.reload
189
+ result
190
+ end
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.find_action(action_type, target_type: target_klass.name,
184
196
  target_id: target_id,
185
197
  user: self)
186
- self.reload
187
- result
198
+ result.present?
199
+ end
200
+
201
+ # @user.unlike_topic
202
+ user_klass.send(:define_method, unaction_name) do |target_or_id|
203
+ target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
204
+ result = user_klass.destroy_action(action_type, target_type: target_klass.name,
205
+ target_id: target_id,
206
+ user: self)
207
+ self.reload
208
+ result
209
+ end
188
210
  end
189
211
 
190
- # @user.like_topic?
191
- user_klass.send(:define_method, "#{full_action_name}?") do |target_or_id|
192
- target_id = target_or_id.is_a?(target_klass) ? target_or_id.id : target_or_id
193
- result = user_klass.find_action(action_type, target_type: target_klass.name,
194
- target_id: target_id,
195
- user: self)
196
- result.present?
212
+ def safe_action_opts(opts)
213
+ opts ||= {}
214
+ if opts[:user]
215
+ opts[:user_id] = opts[:user].id
216
+ opts[:user_type] = opts[:user].class.name
217
+ end
218
+ if opts[:target]
219
+ opts[:target_type] = opts[:target].class.name
220
+ opts[:target_id] = opts[:target].id
221
+ end
222
+ opts
197
223
  end
198
224
 
199
- # @user.unlike_topic
200
- user_klass.send(:define_method, unaction_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.destroy_action(action_type, target_type: target_klass.name,
203
- target_id: target_id,
204
- user: self)
205
- self.reload
206
- result
225
+ def where_opts(opts)
226
+ opts.extract!(:action_type, :target_type, :target_id, :user_id, :user_type)
207
227
  end
208
- end
209
-
210
- def safe_action_opts(opts)
211
- opts ||= {}
212
- if opts[:user]
213
- opts[:user_id] = opts[:user].id
214
- opts[:user_type] = opts[:user].class.name
215
- end
216
- if opts[:target]
217
- opts[:target_type] = opts[:target].class.name
218
- opts[:target_id] = opts[:target].id
219
- end
220
- opts
221
- end
222
-
223
- def where_opts(opts)
224
- opts.extract!(:action_type, :target_type, :target_id, :user_id, :user_type)
225
- end
226
228
  end
227
229
  end
228
230
  end
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  module ActionStore
2
3
  module Model
3
4
  extend ActiveSupport::Concern
4
5
 
5
6
  included do
6
7
  # puts "Initialize ActionStore::Model"
7
- belongs_to :target, polymorphic: true
8
- belongs_to :user, polymorphic: true
8
+ belongs_to :target, polymorphic: true, optional: true
9
+ belongs_to :user, polymorphic: true, optional: true
9
10
  end
10
11
  end
11
12
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ActionStore
2
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
3
4
  end
@@ -1,16 +1,17 @@
1
- require 'rails/generators'
1
+ # frozen_string_literal: true
2
+ require "rails/generators"
2
3
  module ActionStore
3
4
  module Generators
4
5
  class InstallGenerator < Rails::Generators::Base
5
6
  desc "Create ActionStore's base files"
6
- source_root File.expand_path('../../../../', __FILE__)
7
+ source_root File.expand_path("../../../../", __FILE__)
7
8
 
8
9
  def add_initializer
9
- template 'config/initializers/action_store.rb', 'config/initializers/action_store.rb'
10
+ template "config/initializers/action_store.rb", "config/initializers/action_store.rb"
10
11
  end
11
12
 
12
13
  def add_migrations
13
- exec('rake action_store:install:migrations')
14
+ exec("rake action_store:install:migrations")
14
15
  end
15
16
  end
16
17
  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.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
@@ -14,9 +14,6 @@ dependencies:
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 4.2.0
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
19
  version: '5'
@@ -24,12 +21,65 @@ dependencies:
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 4.2.0
30
24
  - - "~>"
31
25
  - !ruby/object:Gem::Version
32
26
  version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
33
83
  description: Store difference kind of actions (Like, Follow, Star, Block ...) in one
34
84
  table via ActiveRecord Polymorphic Association.
35
85
  email: huacnlee@gmail.com
@@ -71,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
121
  version: '0'
72
122
  requirements: []
73
123
  rubyforge_project:
74
- rubygems_version: 2.6.11
124
+ rubygems_version: 2.7.6
75
125
  signing_key:
76
126
  specification_version: 4
77
127
  summary: Store difference kind of actions (Like, Follow, Star, Block ...) in one table.