action_plugin 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -16
- data/action_plugin.gemspec +2 -2
- data/lib/action_plugin/mixin.rb +43 -28
- data/lib/action_plugin/version.rb +1 -1
- data/lib/generators/action_plugin/install_generator.rb +25 -5
- data/{db/migrate/create_actions.rb → lib/generators/action_plugin/templates/migration.rb} +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71d13a846edc92f3fd37a6d23201148b74652876
|
4
|
+
data.tar.gz: 497eaea757e2a373ff994642238a56da153124de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a0cde949a1ba30e95efc762b30708efab0594c31c69d88038b0f9498ea68621f1dcd6e8b191e5ebf67f4778e779cb979937b61f80445ab88eef18586443306a
|
7
|
+
data.tar.gz: 7285d2df12f0e0e4df85600d7498132d41c459807258b1146dce62aed019a5c5db480099ce78643143a729bf0a3c3c948ca6266c72e68c55031882d2c00fd57d
|
data/README.md
CHANGED
@@ -23,11 +23,15 @@ Or install it yourself as:
|
|
23
23
|
Generate Migrations:
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
$ rails g
|
26
|
+
$ rails g action_plugin:install
|
27
27
|
```
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
migration 20170208024704_create_actions.rb
|
30
|
+
create migration 20170208024704_create_actions.rb
|
31
|
+
```
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
rails db:migrate
|
31
35
|
```
|
32
36
|
|
33
37
|
## Define Actions
|
@@ -54,31 +58,80 @@ end
|
|
54
58
|
## some instance methods:
|
55
59
|
|
56
60
|
```ruby
|
57
|
-
|
61
|
+
for example one:
|
58
62
|
|
59
|
-
|
63
|
+
# Users like topic can be defined as follows.
|
64
|
+
class User < ActiveRecord::Base
|
65
|
+
action_plugin :User, :like, :Topic
|
66
|
+
end
|
60
67
|
|
61
|
-
|
68
|
+
If you have the definition above the following methods are generated
|
62
69
|
|
63
|
-
|
70
|
+
# Returns the actions of all the topics that this user likes
|
71
|
+
@user.likeing_topic_actions
|
64
72
|
|
65
|
-
|
73
|
+
# Return to all the topics that this user likes
|
74
|
+
@user.likeing_topics
|
66
75
|
|
67
|
-
|
76
|
+
# Return to all the topic id that this user likes
|
77
|
+
@user.likeing_topic_ids
|
78
|
+
|
79
|
+
# Return all user actions that like this topic
|
80
|
+
@topic.likeed_user_actions
|
81
|
+
|
82
|
+
# Returns all users who like the topic
|
83
|
+
@topic.likeed_users
|
84
|
+
|
85
|
+
# Returns all user id who like the topic
|
86
|
+
@topic.likeed_user_ids
|
87
|
+
|
88
|
+
# Users like to create an action record
|
89
|
+
@user.like_topic @topic
|
90
|
+
|
91
|
+
#The user does not like the topic to delete an action record
|
92
|
+
@user.unlike_topic @topic
|
93
|
+
|
94
|
+
#Do users like the theme
|
95
|
+
@user.like_topic? @topic
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
|
101
|
+
for example two:
|
102
|
+
|
103
|
+
# Users can follow the definition of other people.
|
104
|
+
class User < ActiveRecord::Base
|
105
|
+
action_plugin :User, :follow, :User
|
106
|
+
end
|
107
|
+
|
108
|
+
If you have the definition above the following methods are generated
|
109
|
+
|
110
|
+
# Returns current_user following other people relations
|
111
|
+
current_user.following_user_actions
|
112
|
+
|
113
|
+
# Return current_user following all users
|
114
|
+
current_user.following_users
|
68
115
|
|
69
|
-
|
116
|
+
# Return current_user following all user ids
|
117
|
+
current_user.following_user_ids
|
70
118
|
|
71
|
-
|
119
|
+
# Return All user relationships that the user is concerned with
|
120
|
+
user.followed_user_actions
|
72
121
|
|
73
|
-
|
122
|
+
# Returns All users that the user is concerned about.
|
123
|
+
user.followed_users
|
74
124
|
|
75
|
-
|
125
|
+
# Returns all user followed user ids
|
126
|
+
user.followed_user_ids
|
76
127
|
|
77
|
-
|
128
|
+
# Users follow to create an relation
|
129
|
+
current_user.follow_user user
|
78
130
|
|
79
|
-
|
131
|
+
#The user does not unfollow the user to delete an relation
|
132
|
+
current_user.unfollow_user user
|
80
133
|
|
81
|
-
|
134
|
+
#Do users follow the theme
|
135
|
+
current_user.follow_user? user
|
82
136
|
|
83
|
-
@user.like_post? @post
|
84
137
|
```
|
data/action_plugin.gemspec
CHANGED
@@ -6,8 +6,8 @@ require "action_plugin/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "action_plugin"
|
8
8
|
spec.version = ActionPlugin::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["
|
9
|
+
spec.authors = ["baodongdongCK"]
|
10
|
+
spec.email = ["bao1214063293@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Plugin different kind of actions Like, Follow, Star ...}
|
13
13
|
spec.description = %q{Plugin different kind of actions Like, Follow, Star ...}
|
data/lib/action_plugin/mixin.rb
CHANGED
@@ -5,7 +5,7 @@ module ActionPlugin
|
|
5
5
|
def validate_opts opts = {}
|
6
6
|
return false if opts[:subject].blank? || opts[:action_type].blank?
|
7
7
|
return false if opts[:target_id].blank? || opts[:target_type].blank?
|
8
|
-
opts[:subject_id]
|
8
|
+
opts[:subject_id] = opts[:subject].id
|
9
9
|
opts[:subject_type] = opts[:subject].class.name
|
10
10
|
opts
|
11
11
|
end
|
@@ -16,14 +16,18 @@ module ActionPlugin
|
|
16
16
|
|
17
17
|
module ClassMethods
|
18
18
|
def initialize_params(subject_type, action_type, target_type)
|
19
|
-
@subject_type
|
20
|
-
@action_type
|
21
|
-
@
|
19
|
+
@subject_type = subject_type.to_s.classify
|
20
|
+
@action_type = action_type.to_s
|
21
|
+
@actioning = "#{@action_type}ing"
|
22
|
+
@actioned = "#{@action_type}ed"
|
23
|
+
@target_type = target_type.to_s.classify
|
22
24
|
@subject_class = @subject_type.constantize
|
23
|
-
@target_class
|
24
|
-
@subject
|
25
|
-
@target
|
26
|
-
@options
|
25
|
+
@target_class = @target_type.constantize
|
26
|
+
@subject = @subject_type.downcase
|
27
|
+
@target = @target_type.downcase
|
28
|
+
@options = { subject_type: @subject_type, action_type: @action_type,
|
29
|
+
target_type: @target_type, target_class: @target_class
|
30
|
+
}
|
27
31
|
end
|
28
32
|
|
29
33
|
def action_plugin(subject_type, action_type, target_type)
|
@@ -32,31 +36,39 @@ module ActionPlugin
|
|
32
36
|
end
|
33
37
|
|
34
38
|
def define_relations
|
35
|
-
options
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
has_many_scope
|
41
|
-
|
39
|
+
options = @options
|
40
|
+
actioning_target_actions = [@actioning, @target, :actions].join('_').to_sym
|
41
|
+
actioning_targets = [@actioning, @target].join('_').pluralize.to_sym
|
42
|
+
actioned_subject_actions = [@actioned, @subject, :actions].join('_').to_sym
|
43
|
+
actioned_subjects = [@actioned, @subject].join('_').pluralize.to_sym
|
44
|
+
has_many_scope = -> { where(subject_type: options[:subject_type],
|
45
|
+
action_type: options[:action_type], target_type: options[:target_type])
|
42
46
|
}
|
43
|
-
|
47
|
+
|
48
|
+
# like following_user_actions
|
49
|
+
@subject_class.send(:has_many, actioning_target_actions, has_many_scope,
|
44
50
|
class_name: "Action", foreign_key: "subject_id")
|
45
|
-
|
51
|
+
# like following_users
|
52
|
+
@subject_class.send(:has_many, actioning_targets, through: actioning_target_actions,
|
46
53
|
source: :target, source_type: @target_type)
|
47
|
-
|
54
|
+
|
55
|
+
# like followed_user_actions
|
56
|
+
@target_class.send(:has_many, actioned_subject_actions, has_many_scope,
|
48
57
|
class_name: "Action", foreign_key: "target_id")
|
49
|
-
|
58
|
+
|
59
|
+
# like followed_users
|
60
|
+
@target_class.send(:has_many, actioned_subjects, through: actioned_subject_actions,
|
50
61
|
source: :subject, source_type: @subject_type)
|
51
62
|
define_some_methods
|
52
63
|
end
|
53
64
|
|
54
65
|
def define_some_methods
|
55
|
-
options
|
56
|
-
action_target
|
57
|
-
unaction_target
|
66
|
+
options = @options
|
67
|
+
action_target = [@action_type, @target].join("_")
|
68
|
+
unaction_target = "un#{action_target}"
|
58
69
|
action_target_mark = "#{action_target}?"
|
59
70
|
|
71
|
+
# define_method like follow_user
|
60
72
|
@subject_class.send(:define_method, action_target) do |target_or_id|
|
61
73
|
target_id = target_or_id.is_a?(options[:target_class]) ? target_or_id.id : target_or_id
|
62
74
|
Action.find_or_create_by( subject_id: self.id, subject_type: self.class.name,
|
@@ -65,23 +77,26 @@ module ActionPlugin
|
|
65
77
|
)
|
66
78
|
end
|
67
79
|
|
80
|
+
# define_method like follow_user?
|
68
81
|
@subject_class.send(:define_method, action_target_mark) do |target_or_id|
|
69
82
|
target_id = target_or_id.is_a?(options[:target_class]) ? target_or_id.id : target_or_id
|
70
|
-
action
|
71
|
-
|
72
|
-
|
83
|
+
action = find_action(subject: self, action_type: options[:action_type],
|
84
|
+
target_id: target_id, target_type: options[:target_type]
|
85
|
+
)
|
73
86
|
action.present?
|
74
87
|
end
|
75
88
|
|
89
|
+
# define_method like unfollow_user
|
76
90
|
@subject_class.send(:define_method, unaction_target) do |target_or_id|
|
77
91
|
target_id = target_or_id.is_a?(options[:target_class]) ? target_or_id.id : target_or_id
|
78
|
-
action
|
79
|
-
|
92
|
+
action = find_action(subject: self, action_type: options[:action_type],
|
93
|
+
target_id: target_id, target_type: options[:target_type]
|
94
|
+
)
|
80
95
|
action.destroy
|
81
96
|
end
|
82
97
|
|
83
98
|
@subject_class.send(:define_method, "find_action") do |opts = {}|
|
84
|
-
opts
|
99
|
+
opts = validate_opts(opts)
|
85
100
|
action = Action.find_by(format_opts(opts))
|
86
101
|
action
|
87
102
|
end
|
@@ -1,15 +1,35 @@
|
|
1
1
|
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
2
3
|
|
3
4
|
module ActionPlugin
|
4
5
|
module Generators
|
5
|
-
class InstallGenerator < Rails::Generators::
|
6
|
+
class InstallGenerator < Rails::Generators::NamedBase
|
6
7
|
desc "Create ActionPlugin's action migration file"
|
7
|
-
source_root File.expand_path('../../../../db/migrate', __FILE__)
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
argument :name, type: :string, default: 'actions'
|
10
|
+
include Rails::Generators::Migration
|
11
|
+
source_root File.expand_path('../templates', __FILE__)
|
12
|
+
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
if ActiveRecord::Base.timestamped_migrations
|
15
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d"].max
|
16
|
+
else
|
17
|
+
SchemaMigration.normalize_migration_number(dirname)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_action
|
22
|
+
migration_template 'migration.rb', 'db/migrate/create_actions.rb', migration_version: migration_version
|
23
|
+
end
|
24
|
+
|
25
|
+
def rails5?
|
26
|
+
Rails.version.start_with? '5'
|
27
|
+
end
|
28
|
+
|
29
|
+
def migration_version
|
30
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if rails5?
|
12
31
|
end
|
32
|
+
|
13
33
|
end
|
14
34
|
end
|
15
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- baodongdongCK
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
version: 4.2.0
|
27
27
|
description: Plugin different kind of actions Like, Follow, Star ...
|
28
28
|
email:
|
29
|
-
-
|
29
|
+
- bao1214063293@gmail.com
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
@@ -38,12 +38,12 @@ files:
|
|
38
38
|
- README.md
|
39
39
|
- Rakefile
|
40
40
|
- action_plugin.gemspec
|
41
|
-
- db/migrate/create_actions.rb
|
42
41
|
- lib/action_plugin.rb
|
43
42
|
- lib/action_plugin/action.rb
|
44
43
|
- lib/action_plugin/mixin.rb
|
45
44
|
- lib/action_plugin/version.rb
|
46
45
|
- lib/generators/action_plugin/install_generator.rb
|
46
|
+
- lib/generators/action_plugin/templates/migration.rb
|
47
47
|
homepage: https://github.com/baodongdongCK/action_plugin
|
48
48
|
licenses:
|
49
49
|
- MIT
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.6.14
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: Plugin different kind of actions Like, Follow, Star ...
|