action_args 2.0.0 → 2.1.0

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: e5f4f25a659c405f12ede335715b003888d85750
4
- data.tar.gz: 86b00b4457c7da799ce4d12d2baadf9a4c76761e
3
+ metadata.gz: e8f99c507ba7d9a77264851459c344095c6b3368
4
+ data.tar.gz: 8ae5a3690c88d10ff55270705ad37558b66a1efc
5
5
  SHA512:
6
- metadata.gz: 4b253edeedc59dfd20bfde649b17af128570eef09214947cb961db481aaddf5fcd1ab43cc9d7236016c60a7ca832c3c0f7392c682f081515846181da7a9bf016
7
- data.tar.gz: 9737e77da93a77c23b191ef085eb9ab40cb00a1249554da7723d51798b9cd6acc70ec24f5e00a8c530792b174ad8912b0092d43a491fd090ece7893459f73821
6
+ metadata.gz: b7b0b63137e252a121d71d202891841548bb43b40daae9289112811b42b4cc1afe4477c3fe73ca81d572e8bd7f4bdfd41d95fcb1aa78219af3feabc407918437
7
+ data.tar.gz: 16ad6ad19f3530e1773bc9f567d80a8c0c5b6366a9ba8e1c69bf6e3611d4817eaa58ba888d50c05766079fd1ca46fd131914007ae0c4838af0654c4ba2c43bcf
@@ -4,21 +4,31 @@ sudo: false
4
4
 
5
5
  rvm:
6
6
  - 2.0.0
7
- - 2.1
8
- - 2.2.3
7
+ - 2.1.10
8
+ - 2.2.5
9
+ - 2.3.1
9
10
  - ruby-head
10
- - jruby-9.0.0.0
11
- - rbx
11
+ - jruby-9.1.0.0
12
+ - rbx-3
12
13
  matrix:
13
14
  exclude:
15
+ - rvm: ruby-head
16
+ gemfile: gemfiles/rails_41.gemfile
17
+ - rvm: ruby-head
18
+ gemfile: gemfiles/rails_42.gemfile
19
+ - rvm: 2.0.0
20
+ gemfile: gemfiles/rails_50.gemfile
21
+ - rvm: 2.1.10
22
+ gemfile: gemfiles/rails_50.gemfile
14
23
  - rvm: 2.0.0
15
24
  gemfile: gemfiles/rails_edge.gemfile
16
- - rvm: 2.1
25
+ - rvm: 2.1.10
17
26
  gemfile: gemfiles/rails_edge.gemfile
18
27
  allow_failures:
19
- - rvm: jruby-9.0.0.0
20
- - rvm: rbx
28
+ - rvm: jruby-9.1.0.0
29
+ - rvm: rbx-3
21
30
  gemfile:
22
31
  - gemfiles/rails_41.gemfile
23
32
  - gemfiles/rails_42.gemfile
33
+ - gemfiles/rails_50.gemfile
24
34
  - gemfiles/rails_edge.gemfile
data/README.md CHANGED
@@ -220,9 +220,9 @@ You may notice that
220
220
 
221
221
  ## Supported versions
222
222
 
223
- * Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.0 (trunk), JRuby, & Rubinius with 2.0+ mode
223
+ * Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.0 (trunk), JRuby, & Rubinius with 2.0+ mode
224
224
 
225
- * Rails 4.1.x, 4.2.x, 5.0 (edge)
225
+ * Rails 4.1.x, 4.2.x, 5.0, 5.1 (edge)
226
226
 
227
227
  Please use Version 1.5.4 for Rails 4.0.x.
228
228
 
@@ -12,8 +12,6 @@ Gem::Specification.new do |s|
12
12
  s.summary = 'Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+'
13
13
  s.description = 'Rails plugin gem that supports Merbish style controller action arguments.'
14
14
 
15
- s.rubyforge_project = 'action_args'
16
-
17
15
  s.files = `git ls-files`.split("\n")
18
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 5.0.0'
4
+
5
+ gem 'rails-controller-testing'
6
+
7
+ gemspec :path => '../'
8
+
9
+ platforms :ruby do
10
+ gem 'sqlite3'
11
+ end
12
+
13
+ platforms :jruby do
14
+ gem 'activerecord-jdbcsqlite3-adapter'
15
+ end
@@ -1,15 +1,19 @@
1
1
  require_relative 'params_handler'
2
2
  using ActionArgs::ParamsHandler
3
3
 
4
- module AbstractController
5
- class Base
4
+ module ActionArgs
5
+ module AbstractControllerMethods
6
6
  def send_action(method_name, *args)
7
- return send method_name, *args unless args.empty?
8
- return send method_name, *args unless defined?(params)
7
+ return super unless args.empty?
8
+ return super unless defined?(params)
9
9
 
10
- send_with_method_parameters_from_params method_name
10
+ strengthen_params! method_name
11
+ values = extract_method_arguments_from_params method_name
12
+ super method_name, *values
11
13
  end
14
+ end
12
15
 
16
+ module AbstractControllerClassMethods
13
17
  # You can configure StrongParameters' `permit` attributes using this DSL method.
14
18
  # The `permit` call will be invoked only against parameters having the resource
15
19
  # model name inferred from the controller class name.
@@ -22,8 +26,8 @@ module AbstractController
22
26
  # end
23
27
  # end
24
28
  #
25
- def self.permits(*attributes)
26
- if attributes.last.is_a?(Hash) && attributes.last.extractable_options? && attributes.last.has_key?(:model_name)
29
+ def permits(*attributes)
30
+ if attributes.last.is_a?(Hash) && attributes.last.extractable_options? && attributes.last.key?(:model_name)
27
31
  options = attributes.pop
28
32
  @permitting_model_name = options[:model_name]
29
33
  end
@@ -31,3 +35,6 @@ module AbstractController
31
35
  end
32
36
  end
33
37
  end
38
+
39
+ AbstractController::Base.send :prepend, ActionArgs::AbstractControllerMethods
40
+ AbstractController::Base.singleton_class.send :include, ActionArgs::AbstractControllerClassMethods
@@ -12,7 +12,9 @@ module ActionArgs
12
12
  if Symbol === filter
13
13
  lambda do |target, _, &blk|
14
14
  if ActionController::Base === target
15
- target.send_with_method_parameters_from_params filter, &blk
15
+ target.strengthen_params! filter
16
+ values = target.extract_method_arguments_from_params filter
17
+ target.send filter, *values, &blk
16
18
  else
17
19
  target.send filter, &blk
18
20
  end
@@ -1,12 +1,6 @@
1
1
  module ActionArgs
2
2
  module ParamsHandler
3
3
  refine AbstractController::Base do
4
- def send_with_method_parameters_from_params(method_name, &blk)
5
- strengthen_params! method_name
6
- values = extract_method_arguments_from_params method_name
7
- send method_name, *values, &blk
8
- end
9
-
10
4
  # converts the request params Hash into an Array to be passed into the target Method
11
5
  def extract_method_arguments_from_params(method_name)
12
6
  method_parameters = method(method_name).parameters
@@ -15,24 +9,29 @@ module ActionArgs
15
9
  method_parameters.reverse_each do |type, key|
16
10
  case type
17
11
  when :req
18
- missing_required_params << key unless params.has_key? key
12
+ missing_required_params << key unless params.key? key
19
13
  next
20
14
  when :keyreq
21
- if params.has_key? key
15
+ if params.key? key
22
16
  kwargs[key] = params[key]
23
17
  else
24
18
  missing_required_params << key
25
19
  end
26
20
  when :key
27
- kwargs[key] = params[key] if params.has_key? key
21
+ kwargs[key] = params[key] if params.key? key
28
22
  when :opt
29
- break if params.has_key? key
23
+ break if params.key? key
30
24
  end
31
25
  # omitting parameters that are :block, :rest, :opt without a param, and :key without a param
32
26
  parameter_names.delete key
33
27
  end
34
28
  if missing_required_params.any?
35
- raise ActionController::BadRequest.new(:required, ArgumentError.new("Missing required parameters at #{self.class.name}##{method_name}: #{missing_required_params.join(', ')}"))
29
+ message = "Missing required parameters at #{self.class.name}##{method_name}: #{missing_required_params.join(', ')}"
30
+ if Rails.version > '5'
31
+ raise ActionController::BadRequest.new message
32
+ else
33
+ raise ActionController::BadRequest.new :required, ArgumentError.new(message)
34
+ end
36
35
  end
37
36
 
38
37
  values = parameter_names.map {|k| params[k]}
@@ -50,7 +49,8 @@ module ActionArgs
50
49
  method_parameters = method(method_name).parameters
51
50
  method_parameters.each do |type, key|
52
51
  if (key == target_model_name) && permitted_attributes
53
- params[key] = params.require(key).try :permit, *permitted_attributes
52
+ params.require(key) if %i[req keyreq].include?(type)
53
+ params[key] = params[key].try :permit, *permitted_attributes
54
54
  end
55
55
  end
56
56
  end
@@ -1,3 +1,3 @@
1
1
  module ActionArgs
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -15,7 +15,7 @@ class BooksControllerTest < ActionController::TestCase
15
15
  end
16
16
 
17
17
  test 'with page parameter' do
18
- get :index, page: 3
18
+ get :index, params: {page: 3}
19
19
  assert 200, response.code
20
20
  assert_equal @books[20..29], assigns(:books)
21
21
  end
@@ -23,21 +23,21 @@ class BooksControllerTest < ActionController::TestCase
23
23
  test 'first param is nil and second is not nil' do
24
24
  rhg = Book.create! title: 'RHG'
25
25
  Book.create! title: 'AWDwR'
26
- get :index, q: 'RH'
26
+ get :index, params: {q: 'RH'}
27
27
  assert_equal [rhg], assigns(:books)
28
28
  end
29
29
  end
30
30
 
31
31
  test 'GET show' do
32
32
  rhg = Book.create! title: 'RHG'
33
- get :show, :id => rhg.id
33
+ get :show, params: {id: rhg.id}
34
34
  assert_equal rhg, assigns(:book)
35
35
  end
36
36
 
37
37
  test 'POST create' do
38
38
  Book.create! title: 'RHG'
39
39
  books_count_was = Book.count
40
- post :create, :book => {title: 'AWDwR', price: 24}
40
+ post :create, params: {book: {title: 'AWDwR', price: 24}}
41
41
  assert_equal 1, Book.count - books_count_was
42
42
  end
43
43
  end
@@ -4,7 +4,7 @@ class BooksControllerTest < ActionController::TestCase
4
4
  setup do
5
5
  Book.delete_all
6
6
  @book = Book.create! title: 'Head First ActionArgs'
7
- get :show, id: @book.id
7
+ get :show, params: {id: @book.id}
8
8
  end
9
9
 
10
10
  sub_test_case 'before_action' do
@@ -7,12 +7,12 @@ class KwBooksControllerTest < ActionController::TestCase
7
7
  end
8
8
 
9
9
  test 'without giving any optional kw parameter' do
10
- get :index, author_name: 'nari'
10
+ get :index, params: {author_name: 'nari'}
11
11
  assert 200, response.code
12
12
  end
13
13
 
14
14
  test 'with kw parameter defaults to non-nil value' do
15
- get :index, author_name: 'nari', page: 3
15
+ get :index, params: {author_name: 'nari', page: 3}
16
16
  body = eval response.body
17
17
  assert_equal 'nari', body[:author_name]
18
18
  assert_equal '3', body[:page]
@@ -20,7 +20,7 @@ class KwBooksControllerTest < ActionController::TestCase
20
20
  end
21
21
 
22
22
  test 'with kw parameter defaults to nil' do
23
- get :index, author_name: 'nari', q: 'Rails'
23
+ get :index, params: {author_name: 'nari', q: 'Rails'}
24
24
  body = eval response.body
25
25
  assert_equal 'nari', body[:author_name]
26
26
  assert_equal '1', body[:page]
@@ -7,12 +7,12 @@ class KwKeyreqBooksControllerTest < ActionController::TestCase
7
7
  end
8
8
 
9
9
  test 'without giving any kw parameter' do
10
- get :index, author_name: 'nari'
10
+ get :index, params: {author_name: 'nari'}
11
11
  assert 200, response.code
12
12
  end
13
13
 
14
14
  test 'with kw parameter defaults to non-nil value' do
15
- get :index, author_name: 'nari', page: 3
15
+ get :index, params: {author_name: 'nari', page: 3}
16
16
  body = eval response.body
17
17
  assert_equal 'nari', body[:author_name]
18
18
  assert_equal '3', body[:page]
@@ -20,7 +20,7 @@ class KwKeyreqBooksControllerTest < ActionController::TestCase
20
20
  end
21
21
 
22
22
  test 'with kw parameter defaults to nil' do
23
- get :index, author_name: 'nari', q: 'Rails'
23
+ get :index, params: {author_name: 'nari', q: 'Rails'}
24
24
  body = eval response.body
25
25
  assert_equal 'nari', body[:author_name]
26
26
  assert_equal '1', body[:page]
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class AuthorsControllerTest < ActionController::TestCase
4
4
  test 'GET show' do
5
5
  matz = Author.create! name: 'Matz'
6
- get :show, :id => matz.id
6
+ get :show, params: {id: matz.id}
7
7
  assert_equal matz, assigns(:author)
8
8
  end
9
9
  end
@@ -3,14 +3,26 @@ require 'test_helper'
3
3
  class StoresControllerTest < ActionController::TestCase
4
4
  test 'GET show' do
5
5
  tatsu_zine = Store.create! name: 'Tatsu-zine'
6
- get :show, :id => tatsu_zine.id
6
+ get :show, params: {id: tatsu_zine.id}
7
7
 
8
8
  assert_equal tatsu_zine, assigns(:store)
9
9
  end
10
10
 
11
+ sub_test_case 'GET new' do
12
+ test 'without store parameter' do
13
+ get :new
14
+ assert 200, response.code
15
+ end
16
+ test 'with store parameter' do
17
+ get :new, params: {store: {name: 'Tatsu-zine'}}
18
+ assert 200, response.code
19
+ assert_equal 'Tatsu-zine', assigns(:store).name
20
+ end
21
+ end
22
+
11
23
  test 'POST create' do
12
24
  store_count_was = Store.count
13
- post :create, :store => {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}
25
+ post :create, params: {store: {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}}
14
26
 
15
27
  assert_equal 1, Store.count - store_count_was
16
28
  end
@@ -19,7 +31,7 @@ end
19
31
  # this controller doesn't permit price of new book do
20
32
  class Admin::BooksControllerTest < ActionController::TestCase
21
33
  test 'POST create' do
22
- post :create, :book => {title: 'naruhoUnix', price: 30}
34
+ post :create, params: {book: {title: 'naruhoUnix', price: 30}}
23
35
 
24
36
  assert_nil Book.last.price
25
37
  end
@@ -28,7 +40,7 @@ end
28
40
  class Admin::AccountsControllerTest < ActionController::TestCase
29
41
  test 'POST create' do
30
42
  admin_account_count_was = Admin::Account.count
31
- post :create, :admin_account => {name: 'amatsuda'}
43
+ post :create, params: {admin_account: {name: 'amatsuda'}}
32
44
 
33
45
  assert_equal 1, Admin::Account.count - admin_account_count_was
34
46
  end
@@ -5,7 +5,7 @@ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':me
5
5
 
6
6
  module ActionArgsTestApp
7
7
  class Application < Rails::Application
8
- config.secret_token = [*'A'..'z'].join
8
+ config.secret_key_base = config.secret_token = [*'A'..'z'].join
9
9
  config.session_store :cookie_store, :key => '_myapp_session'
10
10
  config.active_support.deprecation = :log
11
11
  config.eager_load = false
@@ -62,7 +62,7 @@ end
62
62
  class AuthorsController < ApplicationController
63
63
  def show
64
64
  @author = Author.find params[:id]
65
- render text: @author.name
65
+ render plain: @author.name
66
66
  end
67
67
  end
68
68
  class BooksController < ApplicationController
@@ -77,17 +77,17 @@ class BooksController < ApplicationController
77
77
  def index(page = 1, q = nil, limit = 10)
78
78
  @books = Book.limit(limit.to_i).offset(([page.to_i - 1, 0].max) * 10)
79
79
  @books = @books.where('title like ?', "%#{q}%") unless q.blank?
80
- render text: 'index', books: @books
80
+ render plain: 'index', books: @books
81
81
  end
82
82
 
83
83
  def show(id)
84
- render text: @book.title
84
+ render plain: @book.title
85
85
  end
86
86
 
87
87
  def create(book)
88
88
  book = book.permit :title, :price
89
89
  @book = Book.create! book
90
- render text: @book.title
90
+ render plain: @book.title
91
91
  end
92
92
 
93
93
  private
@@ -110,12 +110,17 @@ class StoresController < ApplicationController
110
110
 
111
111
  def show(id)
112
112
  @store = Store.find(id)
113
- render text: @store.name
113
+ render plain: @store.name
114
+ end
115
+
116
+ def new(store = nil)
117
+ @store = Store.new store
118
+ render plain: @store.name
114
119
  end
115
120
 
116
121
  def create(store)
117
122
  @store = Store.create! store
118
- render text: @store.name
123
+ render plain: @store.name
119
124
  end
120
125
  end
121
126
  module Admin
@@ -124,7 +129,7 @@ module Admin
124
129
 
125
130
  def create(admin_account)
126
131
  @admin_account = Admin::Account.create! admin_account
127
- render text: @admin_account.name
132
+ render plain: @admin_account.name
128
133
  end
129
134
  end
130
135
 
@@ -133,7 +138,7 @@ module Admin
133
138
 
134
139
  def create(book)
135
140
  @book = Book.create! book
136
- render text: @book.title
141
+ render plain: @book.title
137
142
  end
138
143
  end
139
144
  end
@@ -151,4 +156,8 @@ class CreateAllTables < ActiveRecord::Migration
151
156
  end
152
157
  end
153
158
 
154
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
159
+ if ActiveRecord::Base.connection.respond_to? :data_source_exists?
160
+ CreateAllTables.up unless ActiveRecord::Base.connection.data_source_exists? 'authors'
161
+ else
162
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
163
+ end
@@ -3,6 +3,6 @@
3
3
  class KwBooksController < ApplicationController
4
4
  # keyword arguments
5
5
  def index(author_name, page: '1', q: nil)
6
- render text: {author_name: author_name, page: page, q: q}.inspect
6
+ render plain: {author_name: author_name, page: page, q: q}.inspect
7
7
  end
8
8
  end
@@ -3,6 +3,6 @@
3
3
  class KwKeyreqBooksController < ApplicationController
4
4
  # keyword arguments
5
5
  def index(author_name:, page: '1', q: nil)
6
- render text: {author_name: author_name, page: page, q: q}.inspect
6
+ render plain: {author_name: author_name, page: page, q: q}.inspect
7
7
  end
8
8
  end
@@ -8,3 +8,17 @@ require 'action_args'
8
8
  require 'fake_app'
9
9
  require 'test/unit/rails/test_help'
10
10
  Bundler.require
11
+
12
+ if Rails.version < '5'
13
+ module ActionControllerTestingMonkey
14
+ def get(path, params: nil, session: nil)
15
+ super path, params, session
16
+ end
17
+
18
+ def post(path, params: nil, session: nil)
19
+ super path, params, session
20
+ end
21
+ end
22
+
23
+ ActionController::TestCase.send :prepend, ActionControllerTestingMonkey
24
+ 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.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-02 00:00:00.000000000 Z
11
+ date: 2016-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - action_args.gemspec
70
70
  - gemfiles/rails_41.gemfile
71
71
  - gemfiles/rails_42.gemfile
72
+ - gemfiles/rails_50.gemfile
72
73
  - gemfiles/rails_edge.gemfile
73
74
  - lib/action_args.rb
74
75
  - lib/action_args/abstract_controller.rb
@@ -107,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
- rubyforge_project: action_args
111
- rubygems_version: 2.4.8
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.4
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+