action_plugin 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6fc41d4690345bed33ece11dbc300ea84bb34217
4
+ data.tar.gz: ca7388bbe7b8bf3238d5405d97e42cec38c95e18
5
+ SHA512:
6
+ metadata.gz: baef0abb6945a343bea95446b62128729baab4abebee7181d241ac5808d1cc0a311bb6448716856b1ff42f156ea768064b9b9ad541f3f795427d9f16995d454c
7
+ data.tar.gz: 60a3ad99e940b77a5374d55e5a18d98d80119d90eecb4b1067ef03fda5cbab076aff64c868e80a226b80659e2d5f0367d08684fab2ee0b3b905dfc5852e6e3d7
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at baodongdong@tigerjoys.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 baodongdong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # ActionPlugin
2
+
3
+ Plugin different kind of actions Like, Follow, Star ...
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'action_plugin'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install action_plugin
20
+
21
+ ## Usage
22
+
23
+ Generate Migrations:
24
+
25
+ ```ruby
26
+ $ rails g action_store:install
27
+ ```
28
+
29
+ ```ruby
30
+ migration 20170208024704_create_actions.rb from action_plugin
31
+ ```
32
+
33
+ ## Define Actions
34
+
35
+ app/models/user.rb
36
+
37
+ ```ruby
38
+ class Subject < ActiveRecord::Base
39
+ action_plugin subject_type, action_type, target_type
40
+ end
41
+ ```
42
+
43
+
44
+ for example:
45
+
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ action_plugin :User, :like, :Post
49
+ action_plugin :User, :collection, :Topic
50
+ action_plugin :Member, :like, :Topic
51
+ end
52
+ ```
53
+
54
+ ## some instance methods:
55
+
56
+ ```ruby
57
+ action_target_actions
58
+
59
+ action_targets
60
+
61
+ action_subject_actions
62
+
63
+ action_subjects
64
+
65
+ action_target?
66
+
67
+ for example:
68
+
69
+ action_plugin :User, :like, :Post -> generate instance methods:
70
+
71
+ @user.like_post_actions
72
+
73
+ @user.like_posts
74
+
75
+ @post.like_user_actions
76
+
77
+ @post.like_users
78
+
79
+ @user.like_post @post
80
+
81
+ @user.unlike_post @post
82
+
83
+ @user.like_post? @post
84
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "action_plugin/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "action_plugin"
8
+ spec.version = ActionPlugin::VERSION
9
+ spec.authors = ["baodongdong"]
10
+ spec.email = ["baodongdong@tigerjoys.com"]
11
+
12
+ spec.summary = %q{Plugin different kind of actions Like, Follow, Star ...}
13
+ spec.description = %q{Plugin different kind of actions Like, Follow, Star ...}
14
+ spec.homepage = "https://github.com/baodongdongCK/action_plugin"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "rails", "~> 4.2.0"
34
+ end
@@ -0,0 +1,16 @@
1
+ class CreateActions < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :actions do |t|
4
+ t.integer :subject_id
5
+ t.string :subject_type
6
+ t.string :action_type
7
+ t.string :target_type
8
+ t.integer :target_id
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :actions, %w[subject_id subject_type action_type]
14
+ add_index :actions, %w[target_id target_type action_type]
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require "action_plugin/version"
2
+ require "action_plugin/mixin"
3
+ require "action_plugin/action"
4
+
5
+ module ActionPlugin
6
+ end
7
+
8
+ ActiveSupport.on_load(:active_record) do
9
+ include ActionPlugin::Mixin
10
+ end
@@ -0,0 +1,4 @@
1
+ class Action < ActiveRecord::Base
2
+ belongs_to :subject, polymorphic: true
3
+ belongs_to :target, polymorphic: true
4
+ end
@@ -0,0 +1,92 @@
1
+ module ActionPlugin
2
+ module Mixin
3
+ extend ActiveSupport::Concern
4
+
5
+ def validate_opts opts = {}
6
+ return false if opts[:subject].blank? || opts[:action_type].blank?
7
+ return false if opts[:target_id].blank? || opts[:target_type].blank?
8
+ opts[:subject_id] = opts[:subject].id
9
+ opts[:subject_type] = opts[:subject].class.name
10
+ opts
11
+ end
12
+
13
+ def format_opts opts
14
+ opts.extract!(:subject_id, :subject_type, :action_type, :target_type, :target_id)
15
+ end
16
+
17
+ module ClassMethods
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
22
+ @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 }
27
+ end
28
+
29
+ def action_plugin(subject_type, action_type, target_type)
30
+ initialize_params(subject_type, action_type, target_type)
31
+ define_relations
32
+ end
33
+
34
+ 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])
42
+ }
43
+ @subject_class.send(:has_many, action_target_actions, has_many_scope,
44
+ class_name: "Action", foreign_key: "subject_id")
45
+ @subject_class.send(:has_many, action_targets, through: action_target_actions,
46
+ source: :target, source_type: @target_type)
47
+ @target_class.send(:has_many, action_subject_actions, has_many_scope,
48
+ class_name: "Action", foreign_key: "target_id")
49
+ @target_class.send(:has_many, action_subjects, through: action_subject_actions,
50
+ source: :subject, source_type: @subject_type)
51
+ define_some_methods
52
+ end
53
+
54
+ def define_some_methods
55
+ options = @options
56
+ action_target = [@action_type, @target].join("_")
57
+ unaction_target = "un#{action_target}"
58
+ action_target_mark = "#{action_target}?"
59
+
60
+ @subject_class.send(:define_method, action_target) do |target_or_id|
61
+ target_id = target_or_id.is_a?(options[:target_class]) ? target_or_id.id : target_or_id
62
+ Action.find_or_create_by( subject_id: self.id, subject_type: self.class.name,
63
+ action_type: options[:action_type], target_id: target_id,
64
+ target_type: options[:target_type]
65
+ )
66
+ end
67
+
68
+ @subject_class.send(:define_method, action_target_mark) do |target_or_id|
69
+ 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
+ )
73
+ action.present?
74
+ end
75
+
76
+ @subject_class.send(:define_method, unaction_target) do |target_or_id|
77
+ 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])
80
+ action.destroy
81
+ end
82
+
83
+ @subject_class.send(:define_method, "find_action") do |opts = {}|
84
+ opts = validate_opts(opts)
85
+ action = Action.find_by(format_opts(opts))
86
+ action
87
+ end
88
+ end
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module ActionPlugin
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+
3
+ module ActionPlugin
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Create ActionPlugin's action migration file"
7
+ source_root File.expand_path('../../../../db/migrate', __FILE__)
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"
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - baodongdong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ description: Plugin different kind of actions Like, Follow, Star ...
28
+ email:
29
+ - baodongdong@tigerjoys.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - CODE_OF_CONDUCT.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - action_plugin.gemspec
41
+ - db/migrate/create_actions.rb
42
+ - lib/action_plugin.rb
43
+ - lib/action_plugin/action.rb
44
+ - lib/action_plugin/mixin.rb
45
+ - lib/action_plugin/version.rb
46
+ - lib/generators/action_plugin/install_generator.rb
47
+ homepage: https://github.com/baodongdongCK/action_plugin
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.5.1
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Plugin different kind of actions Like, Follow, Star ...
71
+ test_files: []