action_args 2.3.2 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,20 +9,21 @@ module ActionArgs
9
9
  kwargs, missing_required_params = {}, []
10
10
  parameter_names = method_parameters.map(&:last)
11
11
  method_parameters.reverse_each do |type, key|
12
+ trimmed_key = key.to_s.sub(/_params\z/, '').to_sym
12
13
  case type
13
14
  when :req
14
- missing_required_params << key unless params.key? key
15
+ missing_required_params << key unless params.key? trimmed_key
15
16
  next
16
17
  when :keyreq
17
- if params.key? key
18
- kwargs[key] = params[key]
18
+ if params.key? trimmed_key
19
+ kwargs[key] = params[trimmed_key]
19
20
  else
20
21
  missing_required_params << key
21
22
  end
22
23
  when :key
23
- kwargs[key] = params[key] if params.key? key
24
+ kwargs[key] = params[trimmed_key] if params.key? trimmed_key
24
25
  when :opt
25
- break if params.key? key
26
+ break if params.key? trimmed_key
26
27
  end
27
28
  # omitting parameters that are :block, :rest, :opt without a param, and :key without a param
28
29
  parameter_names.delete key
@@ -36,9 +37,8 @@ module ActionArgs
36
37
  end
37
38
  end
38
39
 
39
- values = parameter_names.map {|k| params[k]}
40
- values << kwargs if kwargs.any?
41
- values
40
+ values = parameter_names.map {|k| params[k.to_s.sub(/_params\z/, '').to_sym]}
41
+ [values, kwargs]
42
42
  end
43
43
 
44
44
  # permits declared model attributes in the params Hash
@@ -50,9 +50,10 @@ module ActionArgs
50
50
 
51
51
  method_parameters = method(method_name).parameters
52
52
  method_parameters.each do |type, key|
53
- if (key == target_model_name) && permitted_attributes
54
- params.require(key) if %i[req keyreq].include?(type)
55
- params[key] = params[key].try :permit, *permitted_attributes if params.key? key
53
+ trimmed_key = key.to_s.sub(/_params\z/, '').to_sym
54
+ if (trimmed_key == target_model_name) && permitted_attributes
55
+ params.require(trimmed_key) if %i[req keyreq].include?(type)
56
+ params[trimmed_key] = params[trimmed_key].try :permit, *permitted_attributes if params.key? trimmed_key
56
57
  end
57
58
  end
58
59
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionArgs
4
- VERSION = '2.3.2'
4
+ VERSION = '2.7.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.2
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-22 00:00:00.000000000 Z
11
+ date: 2021-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,8 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".github/workflows/main.yml"
62
63
  - ".gitignore"
63
- - ".travis.yml"
64
64
  - CONTRIBUTING.md
65
65
  - Gemfile
66
66
  - MIT-LICENSE
@@ -72,6 +72,9 @@ files:
72
72
  - gemfiles/rails_50.gemfile
73
73
  - gemfiles/rails_51.gemfile
74
74
  - gemfiles/rails_52.gemfile
75
+ - gemfiles/rails_60.gemfile
76
+ - gemfiles/rails_61.gemfile
77
+ - gemfiles/rails_70.gemfile
75
78
  - gemfiles/rails_edge.gemfile
76
79
  - lib/action_args.rb
77
80
  - lib/action_args/abstract_controller.rb
@@ -80,23 +83,12 @@ files:
80
83
  - lib/action_args/version.rb
81
84
  - lib/generators/rails/action_args_scaffold_controller_generator.rb
82
85
  - lib/generators/rails/templates/controller.rb
83
- - test/controllers/action_args_controller_test.rb
84
- - test/controllers/hooks_test.rb
85
- - test/controllers/kwargs_controller_test.rb
86
- - test/controllers/kwargs_keyreq_controller_test.rb
87
- - test/controllers/ordinal_controller_test.rb
88
- - test/controllers/strong_parameters_test.rb
89
- - test/fake_app.rb
90
- - test/kwargs_controllers.rb
91
- - test/kwargs_keyreq_controllers.rb
92
- - test/mailers/action_mailer_test.rb
93
- - test/params_handler/params_handler_test.rb
94
- - test/test_helper.rb
95
86
  homepage: http://asakusa.rubyist.net/
96
87
  licenses:
97
88
  - MIT
98
- metadata: {}
99
- post_install_message:
89
+ metadata:
90
+ source_code_uri: https://github.com/asakusarb/action_args
91
+ post_install_message:
100
92
  rdoc_options: []
101
93
  require_paths:
102
94
  - lib
@@ -111,20 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
103
  - !ruby/object:Gem::Version
112
104
  version: '0'
113
105
  requirements: []
114
- rubygems_version: 3.0.1
115
- signing_key:
106
+ rubygems_version: 3.3.0.dev
107
+ signing_key:
116
108
  specification_version: 4
117
109
  summary: Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+
118
- test_files:
119
- - test/controllers/action_args_controller_test.rb
120
- - test/controllers/hooks_test.rb
121
- - test/controllers/kwargs_controller_test.rb
122
- - test/controllers/kwargs_keyreq_controller_test.rb
123
- - test/controllers/ordinal_controller_test.rb
124
- - test/controllers/strong_parameters_test.rb
125
- - test/fake_app.rb
126
- - test/kwargs_controllers.rb
127
- - test/kwargs_keyreq_controllers.rb
128
- - test/mailers/action_mailer_test.rb
129
- - test/params_handler/params_handler_test.rb
130
- - test/test_helper.rb
110
+ test_files: []
data/.travis.yml DELETED
@@ -1,64 +0,0 @@
1
- script: 'bundle exec rake -t test'
2
- language: ruby
3
- sudo: false
4
-
5
- before_install:
6
- - "ruby -e 'exit RUBY_VERSION.to_f >= 2.3' && gem up --system || gem i rubygems-update -v '<3' && update_rubygems"
7
- - gem up bundler
8
-
9
- # rvm:
10
- # - 2.6.0
11
- # - 2.5.3
12
- # - 2.4.5
13
- # - 2.3.7
14
- # - 2.2.10
15
- # - 2.1.10
16
- # - 2.0.0
17
- # - ruby-head
18
- # gemfile:
19
- # - gemfiles/rails_52.gemfile
20
- # - gemfiles/rails_51.gemfile
21
- # - gemfiles/rails_50.gemfile
22
- # - gemfiles/rails_42.gemfile
23
- # - gemfiles/rails_41.gemfile
24
- # - gemfiles/rails_edge.gemfile
25
-
26
- matrix:
27
- include:
28
- - rvm: 2.6.0
29
- gemfile: gemfiles/rails_52.gemfile
30
- - rvm: 2.6.0
31
- gemfile: gemfiles/rails_51.gemfile
32
- - rvm: 2.6.0
33
- gemfile: gemfiles/rails_50.gemfile
34
- - rvm: 2.6.0
35
- gemfile: gemfiles/rails_edge.gemfile
36
- - rvm: 2.5.3
37
- gemfile: gemfiles/rails_52.gemfile
38
- - rvm: 2.4.5
39
- gemfile: gemfiles/rails_52.gemfile
40
- - rvm: 2.3.8
41
- gemfile: gemfiles/rails_52.gemfile
42
- - rvm: 2.2.10
43
- gemfile: gemfiles/rails_42.gemfile
44
- - rvm: 2.2.10
45
- gemfile: gemfiles/rails_41.gemfile
46
- - rvm: 2.1.10
47
- gemfile: gemfiles/rails_42.gemfile
48
- - rvm: 2.0.0
49
- gemfile: gemfiles/rails_42.gemfile
50
-
51
- - rvm: ruby-head
52
- gemfile: gemfiles/rails_52.gemfile
53
- - rvm: ruby-head
54
- gemfile: gemfiles/rails_edge.gemfile
55
-
56
- - rvm: jruby-9.2.0.0
57
- gemfile: gemfiles/rails_52.gemfile
58
- - rvm: rubinius-3
59
- gemfile: gemfiles/rails_52.gemfile
60
-
61
- allow_failures:
62
- - rvm: jruby-9.2.0.0
63
- - rvm: rubinius-3
64
- - gemfile: gemfiles/rails_edge.gemfile
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class BooksControllerTest < ActionController::TestCase
6
- sub_test_case 'GET index (having an optional parameter)' do
7
- setup do
8
- @books = []
9
- Book.delete_all
10
- 100.times {|i| @books << Book.create!(title: 'book'+i.to_s) }
11
- end
12
-
13
- test 'without page parameter' do
14
- get :index
15
- assert 200, response.code
16
- assert_equal @books[0..9], assigns(:books)
17
- end
18
-
19
- test 'with page parameter' do
20
- get :index, params: {page: 3}
21
- assert 200, response.code
22
- assert_equal @books[20..29], assigns(:books)
23
- end
24
-
25
- test 'first param is nil and second is not nil' do
26
- rhg = Book.create! title: 'RHG'
27
- Book.create! title: 'AWDwR'
28
- get :index, params: {q: 'RH'}
29
- assert_equal [rhg], assigns(:books)
30
- end
31
- end
32
-
33
- test 'GET show' do
34
- rhg = Book.create! title: 'RHG'
35
- get :show, params: {id: rhg.id}
36
- assert_equal rhg, assigns(:book)
37
- end
38
-
39
- test 'POST create' do
40
- Book.create! title: 'RHG'
41
- books_count_was = Book.count
42
- post :create, params: {book: {title: 'AWDwR', price: 24}}
43
- assert_equal 1, Book.count - books_count_was
44
- end
45
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class BooksControllerTest < ActionController::TestCase
6
- setup do
7
- Book.delete_all
8
- @book = Book.create! title: 'Head First ActionArgs'
9
- get :show, params: {id: @book.id}
10
- end
11
-
12
- sub_test_case 'before_action' do
13
- test 'via Symbol' do
14
- assert_equal @book, assigns(:book)
15
- end
16
-
17
- if Rails.version < '5.1'
18
- test 'via String' do
19
- assert assigns(:string_filter_executed)
20
- end
21
- end
22
-
23
- test 'via Proc' do
24
- assert assigns(:proc_filter_executed)
25
- end
26
- end
27
-
28
- test 'around_action' do
29
- assert_not_nil assigns(:elapsed_time)
30
- end
31
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class KwBooksControllerTest < ActionController::TestCase
6
- sub_test_case 'GET index (having an optional parameter)' do
7
- test 'without giving any kw parameter (not even giving :required one)' do
8
- assert_raises(ActionController::BadRequest) { get :index }
9
- end
10
-
11
- test 'without giving any optional kw parameter' do
12
- get :index, params: {author_name: 'nari'}
13
- assert 200, response.code
14
- end
15
-
16
- test 'with kw parameter defaults to non-nil value' do
17
- get :index, params: {author_name: 'nari', page: 3}
18
- body = eval response.body
19
- assert_equal 'nari', body[:author_name]
20
- assert_equal '3', body[:page]
21
- assert_nil body[:q]
22
- end
23
-
24
- test 'with kw parameter defaults to nil' do
25
- get :index, params: {author_name: 'nari', q: 'Rails'}
26
- body = eval response.body
27
- assert_equal 'nari', body[:author_name]
28
- assert_equal '1', body[:page]
29
- assert_equal 'Rails', body[:q]
30
- end
31
- end
32
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class KwKeyreqBooksControllerTest < ActionController::TestCase
6
- sub_test_case 'GET index (having an optional parameter)' do
7
- test 'without giving any kw parameter (not even giving :required one)' do
8
- assert_raises(ActionController::BadRequest) { get :index }
9
- end
10
-
11
- test 'without giving any kw parameter' do
12
- get :index, params: {author_name: 'nari'}
13
- assert 200, response.code
14
- end
15
-
16
- test 'with kw parameter defaults to non-nil value' do
17
- get :index, params: {author_name: 'nari', page: 3}
18
- body = eval response.body
19
- assert_equal 'nari', body[:author_name]
20
- assert_equal '3', body[:page]
21
- assert_nil body[:q]
22
- end
23
-
24
- test 'with kw parameter defaults to nil' do
25
- get :index, params: {author_name: 'nari', q: 'Rails'}
26
- body = eval response.body
27
- assert_equal 'nari', body[:author_name]
28
- assert_equal '1', body[:page]
29
- assert_equal 'Rails', body[:q]
30
- end
31
- end
32
- end if RUBY_VERSION >= '2.1'
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class AuthorsControllerTest < ActionController::TestCase
6
- test 'GET show' do
7
- matz = Author.create! name: 'Matz'
8
- get :show, params: {id: matz.id}
9
- assert_equal matz, assigns(:author)
10
- end
11
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class StoresControllerTest < ActionController::TestCase
6
- test 'GET show' do
7
- tatsu_zine = Store.create! name: 'Tatsu-zine'
8
- get :show, params: {id: tatsu_zine.id}
9
-
10
- assert_equal tatsu_zine, assigns(:store)
11
- end
12
-
13
- sub_test_case 'GET new' do
14
- test 'without store parameter' do
15
- get :new
16
- assert 200, response.code
17
- assert_equal 'PragProg', assigns(:store).name
18
- end
19
- test 'with store parameter' do
20
- get :new, params: {store: {name: 'Tatsu-zine'}}
21
- assert 200, response.code
22
- assert_equal 'Tatsu-zine', assigns(:store).name
23
- end
24
- end
25
-
26
- test 'POST create' do
27
- store_count_was = Store.count
28
- post :create, params: {store: {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}}
29
-
30
- assert_equal 1, Store.count - store_count_was
31
- end
32
- end
33
-
34
- class MoviesControllerTest < ActionController::TestCase
35
- test 'POST create' do
36
- movie_count_was = Movie.count
37
- post :create, params: {movie: {title: 'Dr. No', actors_attributes: [{name: 'Bernard Lee'}]}}
38
-
39
- assert_equal 1, Movie.count - movie_count_was
40
- end
41
- end
42
-
43
- # this controller doesn't permit price of new book do
44
- class Admin::BooksControllerTest < ActionController::TestCase
45
- test 'POST create' do
46
- post :create, params: {book: {title: 'naruhoUnix', price: 30}}
47
-
48
- assert_nil Book.last.price
49
- end
50
- end
51
-
52
- class Admin::AccountsControllerTest < ActionController::TestCase
53
- test 'POST create' do
54
- admin_account_count_was = Admin::Account.count
55
- post :create, params: {admin_account: {name: 'amatsuda'}}
56
-
57
- assert_equal 1, Admin::Account.count - admin_account_count_was
58
- end
59
- end
data/test/fake_app.rb DELETED
@@ -1,193 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # config
5
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
-
7
- module ActionArgsTestApp
8
- class Application < Rails::Application
9
- config.secret_key_base = config.secret_token = [*'A'..'z'].join
10
- config.session_store :cookie_store, :key => '_myapp_session'
11
- config.active_support.deprecation = :log
12
- config.eager_load = false
13
- end
14
- end
15
- ActionArgsTestApp::Application.initialize!
16
-
17
- # routes
18
- ActionArgsTestApp::Application.routes.draw do
19
- resources :authors
20
- resources :books
21
- resources :kw_books # 2.0+ only
22
- resources :kw_keyreq_books # 2.1+ only
23
- resources :stores
24
- resources :movies
25
-
26
- namespace :admin do
27
- resources :accounts
28
- resources :books
29
- end
30
- end
31
-
32
- # models
33
- class Author < ActiveRecord::Base
34
- end
35
- class Book < ActiveRecord::Base
36
- end
37
- class Store < ActiveRecord::Base
38
- end
39
- class Movie < ActiveRecord::Base
40
- has_many :actors
41
- accepts_nested_attributes_for :actors
42
- end
43
- class Actor < ActiveRecord::Base
44
- belongs_to :movie
45
- end
46
- module Admin
47
- def self.table_name_prefix() 'admin_' end
48
- class Account < ActiveRecord::Base
49
- end
50
- end
51
-
52
- # mailers
53
- require "action_mailer/railtie"
54
- class UserMailer < ActionMailer::Base
55
- def send_email_without_args
56
- mail(
57
- to: 'to@example.com',
58
- from: 'from@example.com',
59
- subject: 'Action Args!!!',
60
- body: 'test'
61
- )
62
- end
63
-
64
- def send_email_with_optional_args(subject = 'Action Args!!!')
65
- mail(
66
- to: 'to@example.com',
67
- from: 'from@example.com',
68
- subject: subject,
69
- body: 'test'
70
- )
71
- end
72
- end
73
-
74
- # helpers
75
- module ApplicationHelper; end
76
-
77
- # controllers
78
- class ApplicationController < ActionController::Base
79
- end
80
- class AuthorsController < ApplicationController
81
- def show
82
- @author = Author.find params[:id]
83
- render plain: @author.name
84
- end
85
- end
86
- class BooksController < ApplicationController
87
- before_action :set_book, only: :show
88
- before_action -> { @proc_filter_executed = true }, only: :show
89
- if Rails.version < '5.1'
90
- before_action '@string_filter_executed = true', only: :show
91
- end
92
- around_action :benchmark_action
93
- before_action :omg
94
- skip_before_action :omg
95
-
96
- # optional parameter
97
- def index(page = 1, q = nil, limit = 10)
98
- @books = Book.limit(limit.to_i).offset(([page.to_i - 1, 0].max) * 10)
99
- @books = @books.where('title like ?', "%#{q}%") unless q.blank?
100
- render plain: 'index', books: @books
101
- end
102
-
103
- def show(id)
104
- render plain: @book.title
105
- end
106
-
107
- def create(book)
108
- book = book.permit :title, :price
109
- @book = Book.create! book
110
- render plain: @book.title
111
- end
112
-
113
- private
114
- def set_book(id)
115
- @book = Book.find(id)
116
- end
117
-
118
- def benchmark_action
119
- start = Time.now
120
- yield
121
- @elapsed_time = Time.now - start
122
- end
123
-
124
- def omg
125
- raise '💣'
126
- end
127
- end
128
- class MoviesController < ApplicationController
129
- permits :title, actors_attributes: [:name]
130
-
131
- def create(movie)
132
- @movie = Movie.create! movie
133
- render plain: @movie.title
134
- end
135
- end
136
- class StoresController < ApplicationController
137
- permits :name, :url
138
-
139
- def show(id)
140
- @store = Store.find(id)
141
- render plain: @store.name
142
- end
143
-
144
- def new(store = {name: 'PragProg'})
145
- @store = Store.new store
146
- render plain: @store.name
147
- end
148
-
149
- def create(store)
150
- @store = Store.create! store
151
- render plain: @store.name
152
- end
153
- end
154
- module Admin
155
- class AccountsController < ::ApplicationController
156
- permits :name, model_name: 'Admin::Account'
157
-
158
- def create(admin_account)
159
- @admin_account = Admin::Account.create! admin_account
160
- render plain: @admin_account.name
161
- end
162
- end
163
-
164
- class BooksController < ::ApplicationController
165
- permits :title
166
-
167
- def create(book)
168
- @book = Book.create! book
169
- render plain: @book.title
170
- end
171
- end
172
- end
173
-
174
- require_relative 'kwargs_controllers'
175
- require_relative 'kwargs_keyreq_controllers' if RUBY_VERSION >= '2.1'
176
-
177
- # migrations
178
- class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
179
- def self.up
180
- create_table(:authors) {|t| t.string :name}
181
- create_table(:books) {|t| t.string :title; t.integer :price}
182
- create_table(:stores) {|t| t.string :name; t.string :url}
183
- create_table(:admin_accounts) {|t| t.string :name}
184
- create_table(:movies) {|t| t.string :title}
185
- create_table(:actors) {|t| t.string :name; t.references :movie}
186
- end
187
- end
188
-
189
- if ActiveRecord::Base.connection.respond_to? :data_source_exists?
190
- CreateAllTables.up unless ActiveRecord::Base.connection.data_source_exists? 'authors'
191
- else
192
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
193
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # this file should not be loaded from Ruby <2.0
4
-
5
- class KwBooksController < ApplicationController
6
- # keyword arguments
7
- def index(author_name, page: '1', q: nil)
8
- render plain: {author_name: author_name, page: page, q: q}.inspect
9
- end
10
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # this file should not be loaded from Ruby <2.1
4
-
5
- class KwKeyreqBooksController < ApplicationController
6
- # keyword arguments
7
- def index(author_name:, page: '1', q: nil)
8
- render plain: {author_name: author_name, page: page, q: q}.inspect
9
- end
10
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class UserMailerTest < ActionMailer::TestCase
6
- test '#send_email_without_args' do
7
- #it should not raise NameError: undefined local variable or method `params' for ...
8
- assert UserMailer.send_email_without_args
9
- end
10
-
11
- test '#send_email_with_optional_args' do
12
- #it should not raise NoMethodError: undefined method for nil:NilClass
13
- assert UserMailer.send_email_with_optional_args
14
- end
15
- end