action_args 2.7.3 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +37 -5
- data/.gitignore +1 -0
- data/CHANGELOG.md +31 -0
- data/Gemfile +21 -6
- data/README.md +11 -2
- data/action_args.gemspec +6 -2
- data/lib/action_args/abstract_controller.rb +33 -11
- data/lib/action_args/callbacks.rb +15 -12
- data/lib/action_args/params_handler.rb +39 -16
- data/lib/action_args/version.rb +1 -1
- data/lib/generators/rails/action_args_scaffold_controller_generator.rb +8 -1
- data/lib/generators/rails/templates/api_controller.rb +2 -6
- data/lib/generators/rails/templates/controller.rb +2 -6
- metadata +115 -77
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8b95c530c05d16ab4d4ef9049ee09aefe9b03441876df6503862b74320e38d48
|
|
4
|
+
data.tar.gz: f7a5f2e6e9bed257210d7a506120087cb4bc8eba6584d06a49ad8a1e040a0c06
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62f06975b32314df62bdfaa81127e0268fa50c3bd53af404331c4a8bc3bc609a84978255d6c7850010d243a973a7594f98a003bf08eda4afd4dfa47c8ddd0378
|
|
7
|
+
data.tar.gz: f18fefb9abdda8c41c61ff57e8caf8c57cc747c54d53cf5a820fae6b34534a68c2d751e8d5374eb66e8384940561195688999e13e9c3daed6a8841c620e0b21e
|
data/.github/workflows/main.yml
CHANGED
|
@@ -10,10 +10,34 @@ jobs:
|
|
|
10
10
|
build:
|
|
11
11
|
strategy:
|
|
12
12
|
matrix:
|
|
13
|
-
ruby_version: [ruby-head, '
|
|
14
|
-
rails_version: [edge, '
|
|
13
|
+
ruby_version: [ruby-head, '4.0', '3.4', '3.3', '3.2', '3.1']
|
|
14
|
+
rails_version: [edge, '8.1', '8.0', '7.2', '7.1', '7.0', '6.1']
|
|
15
15
|
|
|
16
16
|
include:
|
|
17
|
+
- ruby_version: 'truffleruby-head'
|
|
18
|
+
rails_version: 'edge'
|
|
19
|
+
|
|
20
|
+
- ruby_version: 'jruby'
|
|
21
|
+
rails_version: '6.1'
|
|
22
|
+
|
|
23
|
+
- ruby_version: '3.0'
|
|
24
|
+
rails_version: '7.1'
|
|
25
|
+
- ruby_version: '3.0'
|
|
26
|
+
rails_version: '7.0'
|
|
27
|
+
- ruby_version: '3.0'
|
|
28
|
+
rails_version: '6.1'
|
|
29
|
+
- ruby_version: '3.0'
|
|
30
|
+
rails_version: '6.0'
|
|
31
|
+
|
|
32
|
+
- ruby_version: '2.7'
|
|
33
|
+
rails_version: '7.1'
|
|
34
|
+
- ruby_version: '2.7'
|
|
35
|
+
rails_version: '7.0'
|
|
36
|
+
- ruby_version: '2.7'
|
|
37
|
+
rails_version: '6.1'
|
|
38
|
+
- ruby_version: '2.7'
|
|
39
|
+
rails_version: '6.0'
|
|
40
|
+
|
|
17
41
|
- ruby_version: '2.6'
|
|
18
42
|
rails_version: '6.1'
|
|
19
43
|
- ruby_version: '2.6'
|
|
@@ -44,18 +68,26 @@ jobs:
|
|
|
44
68
|
- ruby_version: '2.1'
|
|
45
69
|
rails_version: '4.2'
|
|
46
70
|
|
|
47
|
-
|
|
71
|
+
exclude:
|
|
72
|
+
- ruby_version: '3.1'
|
|
73
|
+
rails_version: edge
|
|
74
|
+
- ruby_version: '3.1'
|
|
75
|
+
rails_version: '8.1'
|
|
76
|
+
- ruby_version: '3.1'
|
|
77
|
+
rails_version: '8.0'
|
|
78
|
+
|
|
79
|
+
runs-on: ubuntu-24.04
|
|
48
80
|
|
|
49
81
|
env:
|
|
50
82
|
RAILS_VERSION: ${{ matrix.rails_version }}
|
|
51
83
|
|
|
52
84
|
steps:
|
|
53
|
-
- uses: actions/checkout@
|
|
85
|
+
- uses: actions/checkout@v4
|
|
54
86
|
|
|
55
87
|
- uses: ruby/setup-ruby@v1
|
|
56
88
|
with:
|
|
57
89
|
ruby-version: ${{ matrix.ruby_version }}
|
|
58
|
-
rubygems: ${{ matrix.ruby_version < '2.
|
|
90
|
+
rubygems: ${{ (matrix.ruby_version < '2.7' && 'default') || (matrix.ruby_version < '3' && '3.4.22') || 'latest' }}
|
|
59
91
|
bundler-cache: true
|
|
60
92
|
continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.ruby_version == 'jruby') || (matrix.rails_version == 'edge') }}
|
|
61
93
|
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.8.0
|
|
4
|
+
|
|
5
|
+
### Behavior changes
|
|
6
|
+
|
|
7
|
+
* `permits` is now inherited by subclasses, and a subclass can override it by calling `permits` again. Params of a subclass of a controller that declares `permits` are now filtered by the inherited allow-list
|
|
8
|
+
* A `*rest` parameter now receives the elements of an Array param (e.g. `ids[]=1&ids[]=2` for `def index(*ids)`), a one-element Array for a scalar param, and nothing when the param is missing. It used to receive nothing at all
|
|
9
|
+
* A missing required parameter that is defined before an optional parameter with a value now raises `ActionController::BadRequest`, instead of being silently passed as `nil`
|
|
10
|
+
|
|
11
|
+
### Bug fixes
|
|
12
|
+
|
|
13
|
+
* A positional Hash argument given to a mailer method is no longer dropped (when empty) or turned into keyword arguments
|
|
14
|
+
* Keyword arguments given to a mailer method are kept as keyword arguments on Rails < 7 with Ruby 3.2+
|
|
15
|
+
* `before_action` / `around_action` callbacks on `ActionController::API` controllers now receive their parameters from `params`
|
|
16
|
+
* Anonymous controller classes no longer crash with `NoMethodError`
|
|
17
|
+
* `permits` no longer breaks a parameterized mailer's (`.with(...)`) params
|
|
18
|
+
|
|
19
|
+
### Scaffold generator
|
|
20
|
+
|
|
21
|
+
* Generated controllers respond with `:unprocessable_content` on Rack 3.1+ (and `:unprocessable_entity` on older Racks)
|
|
22
|
+
* Dropped the `update_attributes` fallback, as all supported Rails versions have `update`
|
|
23
|
+
|
|
24
|
+
### Gem
|
|
25
|
+
|
|
26
|
+
* Declared runtime dependencies on actionpack and railties (>= 4.1)
|
|
27
|
+
* Declared `required_ruby_version` (>= 2.0.0)
|
|
28
|
+
|
|
29
|
+
## 2.7.3 and earlier
|
|
30
|
+
|
|
31
|
+
See the [git history](https://github.com/asakusarb/action_args/commits/master).
|
data/Gemfile
CHANGED
|
@@ -13,18 +13,33 @@ else
|
|
|
13
13
|
gem 'rails'
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
ruby_version = Gem::Version.new(RUBY_VERSION)
|
|
17
|
+
# nil for edge or the latest Rails
|
|
18
|
+
rails_version = Gem::Version.new(ENV['RAILS_VERSION']) if ENV['RAILS_VERSION'] && (ENV['RAILS_VERSION'] != 'edge')
|
|
17
19
|
|
|
18
20
|
platforms :ruby do
|
|
19
|
-
|
|
21
|
+
if rails_version && (rails_version <= Gem::Version.new('5.0'))
|
|
22
|
+
gem 'sqlite3', '< 1.4'
|
|
23
|
+
elsif (rails_version && (rails_version < Gem::Version.new('8'))) || (ruby_version < Gem::Version.new('3'))
|
|
24
|
+
gem 'sqlite3', '< 2'
|
|
25
|
+
else
|
|
26
|
+
gem 'sqlite3'
|
|
27
|
+
end
|
|
20
28
|
end
|
|
21
29
|
platforms :jruby do
|
|
22
30
|
gem 'activerecord-jdbcsqlite3-adapter'
|
|
23
31
|
end
|
|
24
32
|
|
|
25
|
-
gem 'nokogiri',
|
|
26
|
-
gem 'loofah',
|
|
33
|
+
gem 'nokogiri', ruby_version < Gem::Version.new('2.1') ? '~> 1.6.0' : '>= 1.7'
|
|
34
|
+
gem 'loofah', ruby_version < Gem::Version.new('2.5') ? '~> 2.20.0' : '>= 2.20'
|
|
27
35
|
|
|
28
|
-
gem 'selenium-webdriver' if rails_version >= '6.1'
|
|
36
|
+
gem 'selenium-webdriver' if !rails_version || (rails_version >= Gem::Version.new('6.1'))
|
|
29
37
|
|
|
30
|
-
gem 'net-smtp' if
|
|
38
|
+
gem 'net-smtp' if ruby_version >= Gem::Version.new('3.1')
|
|
39
|
+
|
|
40
|
+
if ruby_version >= Gem::Version.new('3.3')
|
|
41
|
+
gem 'bigdecimal'
|
|
42
|
+
gem 'mutex_m'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
gem 'benchmark' if ruby_version >= Gem::Version.new('4.0')
|
data/README.md
CHANGED
|
@@ -125,6 +125,15 @@ class MembersController < ApplicationController
|
|
|
125
125
|
end
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
+
`permits` is inherited by subclasses, and a subclass can override it by calling `permits` again.
|
|
129
|
+
Unless `:model_name` is given, the target model name is deduced from the subclass's own name:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
class Admin::UsersController < UsersController
|
|
133
|
+
# inherits `permits :name, :age` for the `user` parameter
|
|
134
|
+
end
|
|
135
|
+
```
|
|
136
|
+
|
|
128
137
|
|
|
129
138
|
## Filters
|
|
130
139
|
|
|
@@ -247,9 +256,9 @@ You may notice that
|
|
|
247
256
|
|
|
248
257
|
## Supported Versions
|
|
249
258
|
|
|
250
|
-
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3 (trunk), JRuby, & Rubinius with 2.0+ mode
|
|
259
|
+
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 4.0, 4.1 (trunk), JRuby, & Rubinius with 2.0+ mode
|
|
251
260
|
|
|
252
|
-
* Rails 4.1.x, 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1 (edge)
|
|
261
|
+
* Rails 4.1.x, 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 8.0, 8.1, 8.2 (edge)
|
|
253
262
|
|
|
254
263
|
For Rails 4.0.x, please use Version 1.5.4.
|
|
255
264
|
|
data/action_args.gemspec
CHANGED
|
@@ -10,18 +10,22 @@ Gem::Specification.new do |s|
|
|
|
10
10
|
s.platform = Gem::Platform::RUBY
|
|
11
11
|
s.authors = ['Akira Matsuda']
|
|
12
12
|
s.email = ['ronnie@dio.jp']
|
|
13
|
-
s.homepage = '
|
|
13
|
+
s.homepage = 'https://asakusa.rubyist.net/'
|
|
14
14
|
s.license = 'MIT'
|
|
15
15
|
s.metadata = {
|
|
16
16
|
'source_code_uri' => 'https://github.com/asakusarb/action_args'
|
|
17
17
|
}
|
|
18
|
-
s.summary = 'Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+'
|
|
18
|
+
s.summary = 'Controller action arguments parameterizer for Rails 4.1+ & Ruby 2.0+'
|
|
19
19
|
s.description = 'Rails plugin gem that supports Merbish style controller action arguments.'
|
|
20
20
|
|
|
21
21
|
s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
22
22
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
23
23
|
end
|
|
24
24
|
s.require_paths = ["lib"]
|
|
25
|
+
s.required_ruby_version = '>= 2.0.0'
|
|
26
|
+
|
|
27
|
+
s.add_dependency 'actionpack', '>= 4.1'
|
|
28
|
+
s.add_dependency 'railties', '>= 4.1'
|
|
25
29
|
|
|
26
30
|
s.add_development_dependency 'bundler'
|
|
27
31
|
s.add_development_dependency 'rake'
|
|
@@ -6,25 +6,20 @@ using ActionArgs::ParamsHandler
|
|
|
6
6
|
module ActionArgs
|
|
7
7
|
module AbstractControllerMethods
|
|
8
8
|
def send_action(method_name, *args)
|
|
9
|
-
unless args.empty?
|
|
10
|
-
kwargs = args.extract_options!
|
|
11
|
-
if kwargs.any?
|
|
12
|
-
return super(method_name, *args, **kwargs)
|
|
13
|
-
else
|
|
14
|
-
return super
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
9
|
+
return super unless args.empty?
|
|
18
10
|
return super if !defined?(params) || params.nil?
|
|
19
11
|
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
method_parameters = method(method_name).parameters
|
|
13
|
+
strengthen_params! method_name, method_parameters
|
|
14
|
+
values, kwargs_values = extract_method_arguments_from_params method_name, method_parameters
|
|
22
15
|
if kwargs_values.any?
|
|
23
16
|
super method_name, *values, **kwargs_values
|
|
24
17
|
else
|
|
25
18
|
super method_name, *values
|
|
26
19
|
end
|
|
27
20
|
end
|
|
21
|
+
# passes the caller's kwargs through as kwargs, and a positional Hash as a positional Hash
|
|
22
|
+
ruby2_keywords :send_action if respond_to?(:ruby2_keywords, true)
|
|
28
23
|
end
|
|
29
24
|
|
|
30
25
|
module AbstractControllerClassMethods
|
|
@@ -48,3 +43,30 @@ end
|
|
|
48
43
|
|
|
49
44
|
AbstractController::Base.send :prepend, ActionArgs::AbstractControllerMethods
|
|
50
45
|
AbstractController::Base.singleton_class.send :include, ActionArgs::AbstractControllerClassMethods
|
|
46
|
+
|
|
47
|
+
# Rails < 7 does not mark the methods that pass action arguments down to send_action with ruby2_keywords,
|
|
48
|
+
# and since Ruby 3.2 keyword arguments given to a mailer method arrive at send_action as a positional Hash.
|
|
49
|
+
# Mark them as Rails 7 does.
|
|
50
|
+
if (Rails::VERSION::MAJOR < 7) && Module.private_method_defined?(:ruby2_keywords)
|
|
51
|
+
module ActionArgs
|
|
52
|
+
def self.ruby2_keywords(mod, *method_names)
|
|
53
|
+
method_names.each do |method_name|
|
|
54
|
+
next unless mod.method_defined?(method_name, false) || mod.private_method_defined?(method_name, false)
|
|
55
|
+
|
|
56
|
+
parameters = mod.instance_method(method_name).parameters
|
|
57
|
+
next if parameters.none? {|type, _| type == :rest } || parameters.any? {|type, _| %i[key keyreq keyrest].include? type }
|
|
58
|
+
|
|
59
|
+
mod.send :ruby2_keywords, method_name
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
ActionArgs.ruby2_keywords AbstractController::Base, :process, :process_action
|
|
65
|
+
ActionArgs.ruby2_keywords AbstractController::Callbacks, :process_action
|
|
66
|
+
|
|
67
|
+
ActiveSupport.on_load :action_mailer do
|
|
68
|
+
ActionArgs.ruby2_keywords ActionMailer::Base, :process
|
|
69
|
+
ActionArgs.ruby2_keywords ActionMailer::Rescuable, :process
|
|
70
|
+
ActionArgs.ruby2_keywords ActionView::Rendering, :process
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -9,9 +9,10 @@ module ActionArgs
|
|
|
9
9
|
module MethodCallParameterizer
|
|
10
10
|
def make_lambda
|
|
11
11
|
lambda do |target, value, &block|
|
|
12
|
-
if ActionController::
|
|
13
|
-
target.
|
|
14
|
-
|
|
12
|
+
if ActionController::Metal === target
|
|
13
|
+
method_parameters = target.method(@method_name).parameters
|
|
14
|
+
target.strengthen_params! @method_name, method_parameters
|
|
15
|
+
arguments, keyword_arguments = target.extract_method_arguments_from_params @method_name, method_parameters
|
|
15
16
|
if keyword_arguments.any?
|
|
16
17
|
target.send(@method_name, *arguments, **keyword_arguments, &block)
|
|
17
18
|
else
|
|
@@ -25,20 +26,21 @@ module ActionArgs
|
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
::ActiveSupport::Callbacks::CallTemplate::MethodCall.prepend ActionArgs::ActiveSupport::CallTemplate::MethodCallParameterizer
|
|
28
|
-
elsif Rails.
|
|
29
|
+
elsif Rails.gem_version >= Gem::Version.new('5.1')
|
|
29
30
|
module CallbackParameterizer
|
|
30
31
|
# Extending AS::Callbacks::Callback's `make_lambda` not just to call specified
|
|
31
32
|
# method but to call the method with method parameters taken from `params`.
|
|
32
33
|
# This would happen only when
|
|
33
|
-
# * the target object is_a ActionController object
|
|
34
|
+
# * the target object is_a ActionController (Base or API) object
|
|
34
35
|
# * the filter was not defined via lambda
|
|
35
36
|
def make_lambda
|
|
36
37
|
lambda do |target, value, &block|
|
|
37
38
|
target, block, method, *arguments = expand(target, value, block)
|
|
38
39
|
|
|
39
|
-
if (ActionController::
|
|
40
|
-
target.
|
|
41
|
-
|
|
40
|
+
if (ActionController::Metal === target) && (method != :instance_exec) && arguments.empty?
|
|
41
|
+
method_parameters = target.method(method).parameters
|
|
42
|
+
target.strengthen_params! method, method_parameters
|
|
43
|
+
arguments, keyword_arguments = target.extract_method_arguments_from_params method, method_parameters
|
|
42
44
|
if keyword_arguments.any?
|
|
43
45
|
target.send(method, *arguments, **keyword_arguments, &block)
|
|
44
46
|
else
|
|
@@ -58,13 +60,14 @@ module ActionArgs
|
|
|
58
60
|
# method but to call the method with method parameters taken from `params`.
|
|
59
61
|
# This would happen only when
|
|
60
62
|
# * the filter was defined in Symbol form
|
|
61
|
-
# * the target object is_a ActionController object
|
|
63
|
+
# * the target object is_a ActionController (Base or API) object
|
|
62
64
|
def make_lambda(filter)
|
|
63
65
|
if Symbol === filter
|
|
64
66
|
lambda do |target, _, &blk|
|
|
65
|
-
if ActionController::
|
|
66
|
-
target.
|
|
67
|
-
|
|
67
|
+
if ActionController::Metal === target
|
|
68
|
+
method_parameters = target.method(filter).parameters
|
|
69
|
+
target.strengthen_params! filter, method_parameters
|
|
70
|
+
values, kwargs_values = target.extract_method_arguments_from_params filter, method_parameters
|
|
68
71
|
values << kwargs_values if kwargs_values.any?
|
|
69
72
|
target.send filter, *values, &blk
|
|
70
73
|
else
|
|
@@ -6,16 +6,20 @@ module ActionArgs
|
|
|
6
6
|
module ParamsHandler
|
|
7
7
|
refine AbstractController::Base do
|
|
8
8
|
# converts the request params Hash into an Array to be passed into the target Method
|
|
9
|
-
def extract_method_arguments_from_params(method_name)
|
|
10
|
-
method_parameters = method(method_name).parameters
|
|
9
|
+
def extract_method_arguments_from_params(method_name, method_parameters = method(method_name).parameters)
|
|
11
10
|
kwargs, missing_required_params = {}, []
|
|
12
|
-
|
|
11
|
+
values = []
|
|
12
|
+
has_optional_parameter_in_params = false
|
|
13
|
+
|
|
13
14
|
method_parameters.reverse_each do |type, key|
|
|
14
15
|
trimmed_key = key.to_s.sub(/_params\z/, '').to_sym
|
|
15
16
|
case type
|
|
16
17
|
when :req
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
if params.key? trimmed_key
|
|
19
|
+
values.unshift params[trimmed_key]
|
|
20
|
+
else
|
|
21
|
+
missing_required_params << key
|
|
22
|
+
end
|
|
19
23
|
when :keyreq
|
|
20
24
|
if params.key? trimmed_key
|
|
21
25
|
kwargs[key] = params[trimmed_key]
|
|
@@ -25,35 +29,54 @@ module ActionArgs
|
|
|
25
29
|
when :key
|
|
26
30
|
kwargs[key] = params[trimmed_key] if params.key? trimmed_key
|
|
27
31
|
when :opt
|
|
28
|
-
|
|
32
|
+
if params.key? trimmed_key
|
|
33
|
+
has_optional_parameter_in_params = true
|
|
34
|
+
values.unshift params[trimmed_key]
|
|
35
|
+
else
|
|
36
|
+
values.unshift nil if has_optional_parameter_in_params
|
|
37
|
+
end
|
|
38
|
+
when :rest
|
|
39
|
+
# splats an Array param, and the preceding optional parameters then need nil fillers to keep the values in place
|
|
40
|
+
if params.key? trimmed_key
|
|
41
|
+
rest_values = params[trimmed_key].is_a?(Array) ? params[trimmed_key] : [params[trimmed_key]]
|
|
42
|
+
has_optional_parameter_in_params = true unless rest_values.empty?
|
|
43
|
+
values.unshift(*rest_values)
|
|
44
|
+
end
|
|
29
45
|
end
|
|
30
|
-
#
|
|
31
|
-
parameter_names.delete key
|
|
46
|
+
# :keyrest, :nokey, and :block take nothing from params
|
|
32
47
|
end
|
|
33
48
|
if missing_required_params.any?
|
|
34
49
|
message = "Missing required parameters at #{self.class.name}##{method_name}: #{missing_required_params.join(', ')}"
|
|
35
|
-
if Rails
|
|
50
|
+
if Rails::VERSION::MAJOR >= 5
|
|
36
51
|
raise ActionController::BadRequest.new message
|
|
37
52
|
else
|
|
38
53
|
raise ActionController::BadRequest.new :required, ArgumentError.new(message)
|
|
39
54
|
end
|
|
40
55
|
end
|
|
41
56
|
|
|
42
|
-
values = parameter_names.map {|k| params[k.to_s.sub(/_params\z/, '').to_sym]}
|
|
43
57
|
[values, kwargs]
|
|
44
58
|
end
|
|
45
59
|
|
|
46
60
|
# permits declared model attributes in the params Hash
|
|
47
61
|
# note that this method mutates the given params Hash
|
|
48
|
-
def strengthen_params!(method_name)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
62
|
+
def strengthen_params!(method_name, method_parameters = method(method_name).parameters)
|
|
63
|
+
# params that are not ActionController::Parameters (e.g. a parameterized mailer's Hash) need no permitting
|
|
64
|
+
return unless params.respond_to?(:permit)
|
|
65
|
+
|
|
66
|
+
# `permits` is inherited from the nearest ancestor that declared it
|
|
67
|
+
permitting_class = self.class.ancestors.detect {|klass| klass.instance_variable_defined?(:@permitted_attributes) }
|
|
68
|
+
return unless permitting_class
|
|
69
|
+
|
|
70
|
+
# the model name cannot be deduced from an anonymous class
|
|
71
|
+
model_name = permitting_class.instance_variable_get(:@permitting_model_name) || (self.class.name && self.class.name.sub(/.+::/, '').sub(/Controller$/, ''))
|
|
72
|
+
return unless model_name
|
|
73
|
+
|
|
74
|
+
target_model_name = model_name.singularize.underscore.tr('/', '_').to_sym
|
|
75
|
+
permitted_attributes = permitting_class.instance_variable_get(:@permitted_attributes)
|
|
52
76
|
|
|
53
|
-
method_parameters = method(method_name).parameters
|
|
54
77
|
method_parameters.each do |type, key|
|
|
55
78
|
trimmed_key = key.to_s.sub(/_params\z/, '').to_sym
|
|
56
|
-
if
|
|
79
|
+
if trimmed_key == target_model_name
|
|
57
80
|
params.require(trimmed_key) if %i[req keyreq].include?(type)
|
|
58
81
|
params[trimmed_key] = params[trimmed_key].try :permit, *permitted_attributes if params.key? trimmed_key
|
|
59
82
|
end
|
data/lib/action_args/version.rb
CHANGED
|
@@ -3,12 +3,19 @@
|
|
|
3
3
|
# load original rails scaffold_controller generator
|
|
4
4
|
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
|
|
5
5
|
|
|
6
|
-
# override rails
|
|
6
|
+
# override rails generator to switch template directory
|
|
7
7
|
module Rails
|
|
8
8
|
module Generators
|
|
9
9
|
class ActionArgsScaffoldControllerGenerator < ::Rails::Generators::ScaffoldControllerGenerator
|
|
10
10
|
argument :attributes, type: :array, default: [], banner: 'field:type field:type'
|
|
11
11
|
source_root File.expand_path('../templates', __FILE__)
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
# Rack 3.1 renamed 422 from :unprocessable_entity to :unprocessable_content
|
|
15
|
+
def unprocessable_status
|
|
16
|
+
require 'rack/utils'
|
|
17
|
+
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key?(:unprocessable_content) ? ':unprocessable_content' : ':unprocessable_entity'
|
|
18
|
+
end
|
|
12
19
|
end
|
|
13
20
|
end
|
|
14
21
|
end
|
|
@@ -24,7 +24,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
|
24
24
|
if @<%= orm_instance.save %>
|
|
25
25
|
render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %>
|
|
26
26
|
else
|
|
27
|
-
render json: <%= "@#{orm_instance.errors}" %>, status:
|
|
27
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: <%= unprocessable_status %>
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
@@ -32,14 +32,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
|
32
32
|
def update(id, <%= singular_table_name %>)
|
|
33
33
|
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
|
34
34
|
|
|
35
|
-
<% if orm_instance.respond_to? :update -%>
|
|
36
35
|
if @<%= orm_instance.update(singular_table_name) %>
|
|
37
|
-
<% else -%>
|
|
38
|
-
if @<%= orm_instance.update_attributes(singular_table_name) %>
|
|
39
|
-
<% end -%>
|
|
40
36
|
render json: <%= "@#{singular_table_name}" %>
|
|
41
37
|
else
|
|
42
|
-
render json: <%= "@#{orm_instance.errors}" %>, status:
|
|
38
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: <%= unprocessable_status %>
|
|
43
39
|
end
|
|
44
40
|
end
|
|
45
41
|
|
|
@@ -30,7 +30,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
|
30
30
|
if @<%= orm_instance.save %>
|
|
31
31
|
redirect_to @<%= singular_table_name %>, notice: '<%= human_name %> was successfully created.'
|
|
32
32
|
else
|
|
33
|
-
render :new, status:
|
|
33
|
+
render :new, status: <%= unprocessable_status %>
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -38,14 +38,10 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
|
38
38
|
def update(id, <%= singular_table_name %>)
|
|
39
39
|
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
|
40
40
|
|
|
41
|
-
<% if orm_instance.respond_to? :update -%>
|
|
42
41
|
if @<%= orm_instance.update(singular_table_name) %>
|
|
43
|
-
<% else -%>
|
|
44
|
-
if @<%= orm_instance.update_attributes(singular_table_name) %>
|
|
45
|
-
<% end -%>
|
|
46
42
|
redirect_to @<%= singular_table_name %>, notice: '<%= human_name %> was successfully updated.'
|
|
47
43
|
else
|
|
48
|
-
render :edit, status:
|
|
44
|
+
render :edit, status: <%= unprocessable_status %>
|
|
49
45
|
end
|
|
50
46
|
end
|
|
51
47
|
|
metadata
CHANGED
|
@@ -1,102 +1,140 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: action_args
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Akira Matsuda
|
|
8
|
-
autorequire:
|
|
7
|
+
- Akira Matsuda
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
- !ruby/object:Gem::
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: actionpack
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: "4.1"
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: "4.1"
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: railties
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "4.1"
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "4.1"
|
|
44
|
+
- !ruby/object:Gem::Dependency
|
|
45
|
+
name: bundler
|
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
-
|
|
49
|
+
- ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
type: :development
|
|
53
|
+
prerelease: false
|
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
-
|
|
57
|
+
- ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: rake
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
-
|
|
65
|
+
- ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
-
|
|
73
|
+
- ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: "0"
|
|
76
|
+
- !ruby/object:Gem::Dependency
|
|
77
|
+
name: test-unit-rails
|
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
-
|
|
81
|
+
- ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: "0"
|
|
84
|
+
type: :development
|
|
85
|
+
prerelease: false
|
|
86
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
-
|
|
89
|
+
- ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: "0"
|
|
55
92
|
description: Rails plugin gem that supports Merbish style controller action arguments.
|
|
56
93
|
email:
|
|
57
|
-
- ronnie@dio.jp
|
|
94
|
+
- ronnie@dio.jp
|
|
58
95
|
executables: []
|
|
59
96
|
extensions: []
|
|
60
97
|
extra_rdoc_files: []
|
|
61
98
|
files:
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
-
|
|
67
|
-
-
|
|
68
|
-
-
|
|
69
|
-
-
|
|
70
|
-
-
|
|
71
|
-
- lib/action_args
|
|
72
|
-
- lib/action_args/
|
|
73
|
-
- lib/action_args/
|
|
74
|
-
- lib/action_args/
|
|
75
|
-
- lib/
|
|
76
|
-
- lib/generators/rails/
|
|
77
|
-
- lib/generators/rails/templates/
|
|
78
|
-
|
|
99
|
+
- .github/workflows/main.yml
|
|
100
|
+
- .gitignore
|
|
101
|
+
- CHANGELOG.md
|
|
102
|
+
- CONTRIBUTING.md
|
|
103
|
+
- Gemfile
|
|
104
|
+
- MIT-LICENSE
|
|
105
|
+
- README.md
|
|
106
|
+
- Rakefile
|
|
107
|
+
- action_args.gemspec
|
|
108
|
+
- lib/action_args.rb
|
|
109
|
+
- lib/action_args/abstract_controller.rb
|
|
110
|
+
- lib/action_args/callbacks.rb
|
|
111
|
+
- lib/action_args/params_handler.rb
|
|
112
|
+
- lib/action_args/version.rb
|
|
113
|
+
- lib/generators/rails/action_args_scaffold_controller_generator.rb
|
|
114
|
+
- lib/generators/rails/templates/api_controller.rb
|
|
115
|
+
- lib/generators/rails/templates/controller.rb
|
|
116
|
+
homepage: "https://asakusa.rubyist.net/"
|
|
79
117
|
licenses:
|
|
80
|
-
- MIT
|
|
118
|
+
- MIT
|
|
81
119
|
metadata:
|
|
82
|
-
source_code_uri: https://github.com/asakusarb/action_args
|
|
83
|
-
post_install_message:
|
|
120
|
+
source_code_uri: "https://github.com/asakusarb/action_args"
|
|
84
121
|
rdoc_options: []
|
|
85
122
|
require_paths:
|
|
86
|
-
- lib
|
|
123
|
+
- lib
|
|
87
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
125
|
requirements:
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
126
|
+
-
|
|
127
|
+
- ">="
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
version: 2.0.0
|
|
92
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
131
|
requirements:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
132
|
+
-
|
|
133
|
+
- ">="
|
|
134
|
+
- !ruby/object:Gem::Version
|
|
135
|
+
version: "0"
|
|
97
136
|
requirements: []
|
|
98
|
-
rubygems_version:
|
|
99
|
-
signing_key:
|
|
137
|
+
rubygems_version: 4.1.0.beta2
|
|
100
138
|
specification_version: 4
|
|
101
|
-
summary: Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+
|
|
139
|
+
summary: Controller action arguments parameterizer for Rails 4.1+ & Ruby 2.0+
|
|
102
140
|
test_files: []
|