rails_callback_ex 0.1.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
+ SHA256:
3
+ metadata.gz: 5269dd2ef360d14adf8c32467cb98dd8003b30bd3340e80bed3eb9d32578286b
4
+ data.tar.gz: bd3271c354a8a8e31cb66e64a3b86b18ae20f9ac39603aa0bc42fde0b86f701f
5
+ SHA512:
6
+ metadata.gz: 887cfdb740aff824619a0b892ef490bccaa7d0066063bb38a49d8fa7e3c3d3d1b7d7a7f53c6d4f457ce917b8c0d391a5e73080e357b2ee8afa2572475880ab35
7
+ data.tar.gz: '08dc9532cb2b7fbbb728a4197ec991356258be090078415dd07a5951c22eb94fa38b7790a65e0ab23015d71ac046cc39711b0df31cf72da88944b8e8de28deb3'
@@ -0,0 +1,32 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-20.04
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: ['3.0', 3.1, 3.2]
12
+ gemfile: ['rails61', 'rails70', 'rails71']
13
+
14
+ name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}, ${{ matrix.database }}
15
+
16
+ env:
17
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
18
+
19
+ steps:
20
+ - uses: actions/checkout@v3
21
+ - uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby }}
24
+ bundler-cache: true
25
+ - name: Prepare test
26
+ run: |
27
+ cd spec/dummy
28
+ BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate
29
+ cd ../..
30
+ - name: Run test
31
+ run: |
32
+ bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .bundle/
2
+ Gemfile.lock
3
+ coverage/
4
+ gemfiles/*.lock
5
+ pkg/
6
+ spec/dummy/db/schema*.rb
7
+ spec/dummy/db/*.sqlite3*
8
+ spec/dummy/log/*.log
9
+ spec/dummy/tmp/*
10
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ * First release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Yoshikazu Kaneta
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,116 @@
1
+ # RailsCallbackEx
2
+
3
+ Insert/delete callbacks for rails.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 3.0+
8
+ * rails 6.1+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rails_callback_ex'
16
+ ```
17
+
18
+ Then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ ### Controller
25
+
26
+ Example:
27
+
28
+ ```ruby
29
+ class SamplesController < ActionController::Base
30
+ before_action :action1
31
+ before_action :action2
32
+
33
+ insert_before_action :new_action1, before: :action1
34
+ insert_before_action :new_action2, after: :action1
35
+ end
36
+
37
+ SamplesController.__callbacks[:process_action].map(&:filter) #=> [:new_action1, :action1, :new_action2, :action2]
38
+
39
+ SamplesController.delete_before_action :new_action1
40
+
41
+ SamplesController.__callbacks[:process_action].map(&:filter) #=> [:action1, :new_action2, :action2]
42
+ ```
43
+
44
+ ### Model
45
+
46
+ Example:
47
+
48
+ ```ruby
49
+ class Sample < ActiveRecord::Base
50
+ before_save :action1
51
+ before_save :action2
52
+
53
+ insert_before_save :new_action1, before: :action1
54
+ insert_before_save :new_action2, after: :action1
55
+ end
56
+
57
+ Sample.__callbacks[:save].map(&:filter) #=> [:new_action1, :action1, :new_action2, :action2]
58
+
59
+ Sample.delete_before_save :new_action1
60
+
61
+ Sample.__callbacks[:save].map(&:filter) #=> [:action1, :new_action2, :action2]
62
+ ```
63
+
64
+ Supported callbacks are `validation`, `save`, `create`, `update` and `destroy`.
65
+
66
+ ### Job
67
+
68
+ Example:
69
+
70
+ ```ruby
71
+ class SampleJob < ActiveJob::Base
72
+ before_perform :action1
73
+ before_perform :action2
74
+
75
+ insert_before_perform :new_action1, before: :action1
76
+ insert_before_perform :new_action2, after: :action1
77
+ end
78
+
79
+ SampleJob.__callbacks[:perform].map(&:filter) #=> [:new_action1, :action1, :new_action2, :action2]
80
+
81
+ SampleJob.delete_before_perform :new_action1
82
+
83
+ SampleJob.__callbacks[:perform].map(&:filter) #=> [:action1, :new_action2, :action2]
84
+ ```
85
+
86
+ Supported callbacks are `enqueue` and `perform`.
87
+
88
+ ### Mailer
89
+
90
+ Example:
91
+
92
+ ```ruby
93
+ class SampleMailer < ActionMailer::Base
94
+ before_deliver :action1
95
+ before_deliver :action2
96
+
97
+ insert_before_deliver :new_action1, before: :action1
98
+ insert_before_deliver :new_action2, after: :action1
99
+ end
100
+
101
+ SampleMailer.__callbacks[:deliver].map(&:filter) #=> [:new_action1, :action1, :new_action2, :action2]
102
+
103
+ SampleMailer.delete_before_deliver :new_action1
104
+
105
+ SampleMailer.__callbacks[:deliver].map(&:filter) #=> [:action1, :new_action2, :action2]
106
+ ```
107
+
108
+ Supported callback is `deliver`. Note that `deliver` callbacks can be used on rails >= 7.1.
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome at https://github.com/kanety/rails_callback_ex.
113
+
114
+ ## License
115
+
116
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.1"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ [:before, :after, :around].each do |callback|
9
+ define_method "insert_#{callback}_action" do |*names, &blk|
10
+ _insert_callbacks(names, blk) do |name, options|
11
+ insert_callback(:process_action, callback, name, options)
12
+ end
13
+ end
14
+
15
+ define_method "delete_#{callback}_action" do |*names, &blk|
16
+ _insert_callbacks(names, blk) do |name, options|
17
+ delete_callback(:process_action, callback, name, options)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Job
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ [:perform, :enqueue].each do |kind|
9
+ [:before, :after, :around].each do |callback|
10
+ define_method "insert_#{callback}_#{kind}" do |*filters, **options, &blk|
11
+ insert_callback(kind, callback, *filters, **options, &blk)
12
+ end
13
+
14
+ define_method "delete_#{callback}_#{kind}" do |*filters, **options, &blk|
15
+ delete_callback(kind, callback, *filters, **options, &blk)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Mailer
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ [:deliver].each do |kind|
9
+ [:before, :after, :around].each do |callback|
10
+ define_method "insert_#{callback}_#{kind}" do |*filters, **options, &blk|
11
+ insert_callback(kind, callback, *filters, **options, &blk)
12
+ end
13
+
14
+ define_method "delete_#{callback}_#{kind}" do |*filters, **options, &blk|
15
+ delete_callback(kind, callback, *filters, **options, &blk)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Model
5
+ module Callbacks
6
+ extend ActiveSupport::Concern
7
+
8
+ def define_insert_model_callbacks(*callbacks)
9
+ options = callbacks.extract_options!
10
+ options = {
11
+ skip_after_callbacks_if_terminated: true,
12
+ scope: [:kind, :name],
13
+ only: [:before, :around, :after]
14
+ }.merge!(options)
15
+
16
+ types = Array(options.delete(:only))
17
+
18
+ callbacks.each do |callback|
19
+ types.each do |type|
20
+ define_singleton_method("insert_#{type}_#{callback}") do |*args, **options, &block|
21
+ options.assert_valid_keys(:if, :unless, :prepend, :before, :after)
22
+ options[:prepend] = true if type == :after
23
+ insert_callback(:"#{callback}", type, *args, options, &block)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def define_delete_model_callbacks(*callbacks)
30
+ options = callbacks.extract_options!
31
+ options = {
32
+ scope: [:kind, :name],
33
+ only: [:before, :around, :after]
34
+ }.merge!(options)
35
+
36
+ types = Array(options.delete(:only))
37
+
38
+ callbacks.each do |callback|
39
+ types.each do |type|
40
+ define_singleton_method("delete_#{type}_#{callback}") do |*args, **options, &block|
41
+ options.assert_valid_keys(:if, :unless, :prepend)
42
+ delete_callback(:"#{callback}", type, *args, options, &block)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Model
5
+ module Validations
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ [:before, :after].each do |kind|
10
+ define_method "insert_#{kind}_validation" do |*args, &block|
11
+ options = args.extract_options!
12
+ set_options_for_callback(options)
13
+ options[:prepend] = true if kind == :after
14
+ insert_callback(:validation, kind, *args, options, &block)
15
+ end
16
+
17
+ define_method "delete_#{kind}_validation" do |*args, &block|
18
+ options = args.extract_options!
19
+ set_options_for_callback(options)
20
+ delete_callback(:validation, kind, *args, options, &block)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ class Railtie < Rails::Railtie
5
+ ActiveSupport.on_load :action_controller do
6
+ require_relative 'controller'
7
+ ActionController::Base.include RailsCallbackEx::Controller
8
+ end
9
+
10
+ ActiveSupport.on_load :active_record do
11
+ require_relative 'model/callbacks'
12
+ require_relative 'model/validations'
13
+ ActiveModel::Callbacks.include RailsCallbackEx::Model::Callbacks
14
+ ActiveModel::Validations::Callbacks::ClassMethods.include RailsCallbackEx::Model::Validations
15
+ require_relative 'record'
16
+ ActiveRecord::Base.include RailsCallbackEx::Record
17
+ end
18
+
19
+ ActiveSupport.on_load :active_job do
20
+ require_relative 'job'
21
+ ActiveJob::Base.include RailsCallbackEx::Job
22
+ end
23
+
24
+ ActiveSupport.on_load :action_mailer do
25
+ require_relative 'controller'
26
+ require_relative 'mailer'
27
+ ActionMailer::Base.include RailsCallbackEx::Controller
28
+ ActionMailer::Base.include RailsCallbackEx::Mailer if Rails::VERSION::STRING.to_f >= 7.1
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ module Record
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ define_insert_model_callbacks :save, :create, :update, :destroy
9
+ define_delete_model_callbacks :save, :create, :update, :destroy
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module Callbacks
5
+ module ClassMethods
6
+ def insert_callback(name, *filter_list, &block)
7
+ type, filters, options = normalize_callback_params(filter_list, block)
8
+
9
+ self_chain = get_callbacks name
10
+ mapped = filters.map do |filter|
11
+ Callback.build(self_chain, filter, type, options)
12
+ end
13
+
14
+ __update_callbacks(name) do |target, chain|
15
+ RailsCallbackEx::Support.insert(chain, type, mapped, options)
16
+ target.set_callbacks name, chain
17
+ end
18
+ end
19
+
20
+ def delete_callback(name, *filter_list, &block)
21
+ type, filters, options = normalize_callback_params(filter_list, block)
22
+
23
+ __update_callbacks(name) do |target, chain|
24
+ RailsCallbackEx::Support.delete(chain, type, filters, options)
25
+ target.set_callbacks name, chain
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module RailsCallbackEx
33
+ module Support
34
+ class << self
35
+ def insert(chain, type, mapped, options)
36
+ unless (position = options[:before] || options[:after])
37
+ raise ArgumentError.new("Specify callback using :before or :after")
38
+ end
39
+
40
+ if (cb = chain.find { |c| c.matches?(type, position) }) && (index = chain.index(cb))
41
+ index += 1 if (options[:before] && options[:prepend]) || (options[:after] && !options[:prepend])
42
+ mapped.each { |c| chain.insert(index, c) }
43
+ else
44
+ raise ArgumentError.new("Couldn't find callback: #{position}")
45
+ end
46
+ end
47
+
48
+ def delete(chain, type, filters, options)
49
+ filters.each do |filter|
50
+ if (cb = chain.find { |c| c.matches?(type, filter) })
51
+ chain.delete(cb)
52
+ else
53
+ raise ArgumentError.new("Couldn't find callback: #{filter}")
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsCallbackEx
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/all'
4
+
5
+ require_relative 'rails_callback_ex/version'
6
+ require_relative 'rails_callback_ex/support'
7
+ require_relative 'rails_callback_ex/railtie' if defined?(Rails)
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_callback_ex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_callback_ex"
8
+ spec.version = RailsCallbackEx::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{Insert/delete callbacks for rails.}
12
+ spec.description = %q{Insert/delete callbacks for rails.}
13
+ spec.homepage = "https://github.com/kanety/rails_callback_ex"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rails", ">= 6.1"
21
+
22
+ spec.add_development_dependency "sqlite3"
23
+ spec.add_development_dependency "rspec-rails"
24
+ spec.add_development_dependency "simplecov"
25
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_callback_ex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-16 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: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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: rspec-rails
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
+ description: Insert/delete callbacks for rails.
70
+ email:
71
+ - kaneta@sitebridge.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/workflows/ci.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - gemfiles/rails61.gemfile
85
+ - gemfiles/rails70.gemfile
86
+ - gemfiles/rails71.gemfile
87
+ - lib/rails_callback_ex.rb
88
+ - lib/rails_callback_ex/controller.rb
89
+ - lib/rails_callback_ex/job.rb
90
+ - lib/rails_callback_ex/mailer.rb
91
+ - lib/rails_callback_ex/model/callbacks.rb
92
+ - lib/rails_callback_ex/model/validations.rb
93
+ - lib/rails_callback_ex/railtie.rb
94
+ - lib/rails_callback_ex/record.rb
95
+ - lib/rails_callback_ex/support.rb
96
+ - lib/rails_callback_ex/version.rb
97
+ - rails_callback_ex.gemspec
98
+ homepage: https://github.com/kanety/rails_callback_ex
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.3.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Insert/delete callbacks for rails.
120
+ test_files: []