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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fc41d4690345bed33ece11dbc300ea84bb34217
4
- data.tar.gz: ca7388bbe7b8bf3238d5405d97e42cec38c95e18
3
+ metadata.gz: 71d13a846edc92f3fd37a6d23201148b74652876
4
+ data.tar.gz: 497eaea757e2a373ff994642238a56da153124de
5
5
  SHA512:
6
- metadata.gz: baef0abb6945a343bea95446b62128729baab4abebee7181d241ac5808d1cc0a311bb6448716856b1ff42f156ea768064b9b9ad541f3f795427d9f16995d454c
7
- data.tar.gz: 60a3ad99e940b77a5374d55e5a18d98d80119d90eecb4b1067ef03fda5cbab076aff64c868e80a226b80659e2d5f0367d08684fab2ee0b3b905dfc5852e6e3d7
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 action_store:install
26
+ $ rails g action_plugin:install
27
27
  ```
28
28
 
29
29
  ```ruby
30
- migration 20170208024704_create_actions.rb from action_plugin
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
- action_target_actions
61
+ for example one:
58
62
 
59
- action_targets
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
- action_subject_actions
68
+ If you have the definition above the following methods are generated
62
69
 
63
- action_subjects
70
+ # Returns the actions of all the topics that this user likes
71
+ @user.likeing_topic_actions
64
72
 
65
- action_target?
73
+ # Return to all the topics that this user likes
74
+ @user.likeing_topics
66
75
 
67
- for example:
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
- action_plugin :User, :like, :Post -> generate instance methods:
116
+ # Return current_user following all user ids
117
+ current_user.following_user_ids
70
118
 
71
- @user.like_post_actions
119
+ # Return All user relationships that the user is concerned with
120
+ user.followed_user_actions
72
121
 
73
- @user.like_posts
122
+ # Returns All users that the user is concerned about.
123
+ user.followed_users
74
124
 
75
- @post.like_user_actions
125
+ # Returns all user followed user ids
126
+ user.followed_user_ids
76
127
 
77
- @post.like_users
128
+ # Users follow to create an relation
129
+ current_user.follow_user user
78
130
 
79
- @user.like_post @post
131
+ #The user does not unfollow the user to delete an relation
132
+ current_user.unfollow_user user
80
133
 
81
- @user.unlike_post @post
134
+ #Do users follow the theme
135
+ current_user.follow_user? user
82
136
 
83
- @user.like_post? @post
84
137
  ```
@@ -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 = ["baodongdong"]
10
- spec.email = ["baodongdong@tigerjoys.com"]
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 ...}
@@ -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] = 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 = subject_type.to_s.classify
20
- @action_type = action_type.to_s
21
- @target_type = target_type.to_s.classify
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 = @target_type.constantize
24
- @subject = @subject_type.downcase
25
- @target = @target_type.downcase
26
- @options = { subject_type: @subject_type, action_type: @action_type, target_type: @target_type, target_class: @target_class }
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 = @options
36
- action_target_actions = [@action_type, @target, 'actions'].join('_').to_sym
37
- action_targets = [@action_type, @target].join('_').pluralize.to_sym
38
- action_subject_actions = [@action_type, @subject, 'actions'].join('_').to_sym
39
- action_subjects = [@action_type, @subject].join('_').pluralize.to_sym
40
- has_many_scope = -> {
41
- where(subject_type: options[:subject_type], action_type: options[:action_type], target_type: options[:target_type])
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
- @subject_class.send(:has_many, action_target_actions, has_many_scope,
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
- @subject_class.send(:has_many, action_targets, through: action_target_actions,
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
- @target_class.send(:has_many, action_subject_actions, has_many_scope,
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
- @target_class.send(:has_many, action_subjects, through: action_subject_actions,
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 = @options
56
- action_target = [@action_type, @target].join("_")
57
- unaction_target = "un#{action_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 = find_action(subject: self, action_type: options[:action_type],
71
- target_id: target_id, target_type: options[:target_type]
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 = find_action(subject: self, action_type: options[:action_type],
79
- target_id: target_id, target_type: options[:target_type])
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 = validate_opts(opts)
99
+ opts = validate_opts(opts)
85
100
  action = Action.find_by(format_opts(opts))
86
101
  action
87
102
  end
@@ -1,3 +1,3 @@
1
1
  module ActionPlugin
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  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::Base
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
- def add_migration
10
- current_time = Time.now.strftime("%Y%m%d%H%M%S")
11
- copy_file "create_actions.rb", "db/migrate/#{current_time}_create_actions.rb"
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
@@ -1,4 +1,4 @@
1
- class CreateActions < ActiveRecord::Migration[5.0]
1
+ class CreateActions < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :actions do |t|
4
4
  t.integer :subject_id
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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
- - baodongdong
7
+ - baodongdongCK
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-29 00:00:00.000000000 Z
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
- - baodongdong@tigerjoys.com
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.5.1
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 ...