action_args 1.3.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 934fd8b7952d38e30c4ddd6a38ae073f28ba622e
4
- data.tar.gz: cd9f1a095d88748cdbeb2f0457711f59bfd70ebb
3
+ metadata.gz: 08400217d2c2140b99d452215b2c61abf84e4970
4
+ data.tar.gz: 497ce69932a416822d0091fac672635da7c74e1d
5
5
  SHA512:
6
- metadata.gz: 27612eea7d1ed88309d165b557804f7c8be1a785ceb2e1734fc2401229726b308ab35e11a0c1f4b2b117401d905f76a6661a6bc12e3cc96e330f47ebe535453f
7
- data.tar.gz: b9e3a78501ce177f5f30dfb25b5e99271ddd380699d4b5497216d9992c2648b452f231c95d073410411903c2aac4ca8842ed2273a9f565dde5391ee79014d2c7
6
+ metadata.gz: 611853b0ba4d24a9af960f3e3c39a17155efab017b4bf4703dbf3bb19db13633da2f7f8a66c53270de3aa21fdec0afd7e4753d45978fc602c2dff406d9374165
7
+ data.tar.gz: 1c513207a195c7293122f487c24aa1f493e8747823191e09a1d0e8c9682b0372b908cb0c3f98c5b7b1e11b811739ed780a358b00ee4aa1f9a8467c15a79b3b17
@@ -3,7 +3,7 @@ language: ruby
3
3
  rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
- - 2.1.0
6
+ - 2.1
7
7
  - ruby-head
8
8
  - jruby
9
9
  - rbx-19mode
@@ -15,3 +15,4 @@ matrix:
15
15
  gemfile:
16
16
  - gemfiles/rails_32.gemfile
17
17
  - gemfiles/rails_40.gemfile
18
+ - gemfiles/rails_41.gemfile
@@ -0,0 +1,16 @@
1
+ # Contributing
2
+
3
+ ## Running specs
4
+
5
+ ActionArgs has several Bundler Gemfiles, each of which bundles different version of Rails.
6
+
7
+ % ls gemfiles/*.gemfile
8
+ gemfiles/rails_32.gemfile gemfiles/rails_40.gemfile gemfiles/rails_41.gemfile
9
+
10
+ Via BUNDLE_GEMFILE ENV variable, you can tell Bundler which version of Rails to bundle.
11
+
12
+ $ BUNDLE_GEMFILE=gemfiles/rails_40.gemfile bundle ex rake spec
13
+
14
+ Simply executing this command would probably run all tests against each version of Rails:
15
+
16
+ % rake spec:all
data/README.md CHANGED
@@ -24,13 +24,11 @@ Hitting "/hoge/fuga?piyo=foo" will call `fuga('foo')` and output 'foo'.
24
24
  This allows you to explicitly state which members of the `params` Hash are used in your controller actions,
25
25
 
26
26
 
27
- ## StrongParameters
27
+ ## Method parameter types in Ruby, and how ActionArgs handles parameters
28
28
 
29
- ActionArgs plays very nice with Rails 4 StrongParameters.
30
-
31
- ### Required Parameters
29
+ ### Required Parameters (:req)
32
30
  Method parameters that you specify are required. If a key of the same name does not exist in the params Hash,
33
- an ArgumentError is raised.
31
+ ActionContrller::BadRequest (or ArgumentError in Rails 3) is raised.
34
32
 
35
33
  In this `show` action, ActionArgs will require that `id` parameter is provided.
36
34
  ```ruby
@@ -42,7 +40,7 @@ class UsersController < ApplicationController
42
40
  end
43
41
  ```
44
42
 
45
- ### Optional Parameters
43
+ ### Optional Parameters (:opt)
46
44
  Default parameter values are assigned in the standard way. Parameters with a default value will not require a matching item in the `params` Hash.
47
45
 
48
46
  ```ruby
@@ -54,8 +52,37 @@ class UsersController < ApplicationController
54
52
  end
55
53
  ```
56
54
 
55
+ ### Keyword Argument (:key)
56
+ If you think this Ruby 2.0 syntax reads better, you can choose this style for defining your action methods.
57
+ This just works in the same way as :opt here.
58
+
59
+ ```ruby
60
+ class UsersController < ApplicationController
61
+ # the `page` parameter is optional
62
+ def index(page: nil)
63
+ @users = User.page(page).per(50)
64
+ end
65
+ end
66
+ ```
67
+
68
+ ### Required Keyword Argument (:keyreq)
69
+ :keyreq is the required version of :key, which was introduced in Ruby 2.1.
70
+ You can use this syntax instead of :req.
71
+
72
+ ```ruby
73
+ class CommentsController < ApplicationController
74
+ def create(post_id:, comment:)
75
+ post = Post.find post_id
76
+ if post.create comment
77
+ ...
78
+ end
79
+ end
80
+ ```
81
+
57
82
  ### StrongParameters - permit
58
83
 
84
+ ActionArgs plays very nice with Rails 4 StrongParameters.
85
+
59
86
  1. Inline declaration
60
87
 
61
88
  Hashes simply respond to the StrongParameters' `permit` method.
@@ -174,9 +201,9 @@ You may notice that
174
201
 
175
202
  ## Supported versions
176
203
 
177
- * Ruby 1.9.2, 1.9.3, 2.0.0, 2.1.0 (trunk), JRuby & Rubinius with 1.9 mode
204
+ * Ruby 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.0 (trunk), JRuby, & Rubinius with 1.9+ mode
178
205
 
179
- * Rails 3.0.x, 3.1.x, 3.2.x, 4.0 (edge)
206
+ * Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 4.2 (edge)
180
207
 
181
208
 
182
209
  ## Installation
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ end
11
11
  task :default => 'spec:all'
12
12
 
13
13
  namespace :spec do
14
- %w(rails_32 rails_40).each do |gemfile|
14
+ %w(rails_32 rails_40 rails_41).each do |gemfile|
15
15
  desc "Run Tests against #{gemfile}"
16
16
  task gemfile do
17
17
  sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
@@ -21,7 +21,7 @@ namespace :spec do
21
21
 
22
22
  desc 'Run Tests against all Rails versions'
23
23
  task :all do
24
- %w(rails_32 rails_40).each do |gemfile|
24
+ %w(rails_32 rails_40 rails_41).each do |gemfile|
25
25
  sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
26
26
  sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
27
27
  end
@@ -18,4 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'bundler'
23
+ s.add_development_dependency 'rake'
21
24
  end
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '>= 3.2.9'
4
- gem 'rspec-rails', '>= 2.0'
3
+ gem 'rails', '~> 3.2.9'
4
+ gem 'rspec-rails', '~> 2.0', '< 2.99'
5
5
 
6
6
  gemspec :path => '../'
7
7
 
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '>= 4.0.0'
4
- gem 'rspec-rails', '>= 2.0'
3
+ gem 'rails', '~> 4.0.0'
4
+ gem 'rspec-rails', '~> 2.0', '< 2.99'
5
5
 
6
6
  gemspec :path => '../'
7
7
 
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.1.0'
4
+ gem 'rspec-rails', '~> 2.0', '< 2.99'
5
+
6
+ gemspec :path => '../'
7
+
8
+ platforms :ruby do
9
+ gem 'sqlite3'
10
+ end
11
+
12
+ platforms :jruby do
13
+ gem 'activerecord-jdbcsqlite3-adapter'
14
+ end
@@ -2,12 +2,19 @@ module ActionArgs
2
2
  module ParamsHandler
3
3
  # converts the request params Hash into an Array to be passed into the target Method
4
4
  def self.extract_method_arguments_from_params(method_parameters, params)
5
- kwargs = {}
5
+ kwargs, missing_required_params = {}, []
6
6
  parameter_names = method_parameters.map(&:last)
7
7
  method_parameters.reverse_each do |type, key|
8
8
  case type
9
9
  when :req
10
+ missing_required_params << key unless params.has_key? key
10
11
  next
12
+ when :keyreq
13
+ if params.has_key? key
14
+ kwargs[key] = params[key]
15
+ else
16
+ missing_required_params << key
17
+ end
11
18
  when :key
12
19
  kwargs[key] = params[key] if params.has_key? key
13
20
  when :opt
@@ -16,6 +23,13 @@ module ActionArgs
16
23
  # omitting parameters that are :block, :rest, :opt without a param, and :key without a param
17
24
  parameter_names.delete key
18
25
  end
26
+ if missing_required_params.any?
27
+ if defined? ActionController::BadRequest
28
+ raise ActionController::BadRequest.new(:required, ArgumentError.new("Missing required parameters: #{missing_required_params.join(', ')}"))
29
+ else
30
+ raise ArgumentError, "Missing required parameters: #{missing_required_params.join(', ')}"
31
+ end
32
+ end
19
33
 
20
34
  values = parameter_names.map {|k| params[k]}
21
35
  values << kwargs if kwargs.any?
@@ -1,3 +1,3 @@
1
1
  module ActionArgs
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'.freeze
3
3
  end
@@ -2,7 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe KwBooksController do
4
4
  describe 'GET index (having an optional parameter)' do
5
- context 'without giving any kw parameter' do
5
+ context 'without giving any kw parameter (not even giving :required one)' do
6
+ if ActionPack::VERSION::MAJOR >= 4
7
+ it { expect { get :index }.to raise_error ActionController::BadRequest }
8
+ else
9
+ it { expect { get :index }.to raise_error ArgumentError }
10
+ end
11
+ end
12
+
13
+ context 'without giving any optional kw parameter' do
6
14
  before { get :index, author_name: 'nari' }
7
15
  its(:response) { should be_success }
8
16
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe KwKeyreqBooksController do
4
+ describe 'GET index (having an optional parameter)' do
5
+ context 'without giving any kw parameter (not even giving :required one)' do
6
+ if ActionPack::VERSION::MAJOR >= 4
7
+ it { expect { get :index }.to raise_error ActionController::BadRequest }
8
+ else
9
+ it { expect { get :index }.to raise_error ArgumentError }
10
+ end
11
+ end
12
+
13
+ context 'without giving any kw parameter' do
14
+ before { get :index, author_name: 'nari' }
15
+ its(:response) { should be_success }
16
+ end
17
+
18
+ context 'with kw parameter defaults to non-nil value' do
19
+ before { get :index, author_name: 'nari', page: 3 }
20
+ subject { eval response.body }
21
+ its([:author_name]) { should == 'nari' }
22
+ its([:page]) { should == '3' }
23
+ its([:q]) { should == nil }
24
+ end
25
+
26
+ context 'with kw parameter defaults to nil' do
27
+ before { get :index, author_name: 'nari', q: 'Rails' }
28
+ subject { eval response.body }
29
+ its([:author_name]) { should == 'nari' }
30
+ its([:page]) { should == '1' }
31
+ its([:q]) { should == 'Rails' }
32
+ end
33
+ end
34
+ end if RUBY_VERSION >= '2.1'
@@ -16,6 +16,7 @@ ActionArgsTestApp::Application.routes.draw do
16
16
  resources :authors
17
17
  resources :books
18
18
  resources :kw_books # 2.0+ only
19
+ resources :kw_keyreq_books # 2.1+ only
19
20
  resources :stores
20
21
 
21
22
  namespace :admin do
@@ -102,6 +103,7 @@ if Rails::VERSION::MAJOR >= 4
102
103
  end
103
104
 
104
105
  require_relative 'kwargs_controllers' if RUBY_VERSION >= '2'
106
+ require_relative 'kwargs_keyreq_controllers' if RUBY_VERSION >= '2.1'
105
107
 
106
108
  # migrations
107
109
  class CreateAllTables < ActiveRecord::Migration
@@ -0,0 +1,8 @@
1
+ # this file should not be loaded from Ruby <2.1
2
+
3
+ class KwKeyreqBooksController < ApplicationController
4
+ # keyword arguments
5
+ def index(author_name:, page: '1', q: nil)
6
+ render text: {author_name: author_name, page: page, q: q}.inspect
7
+ end
8
+ end
@@ -102,12 +102,60 @@ describe ActionArgs::ParamsHandler do
102
102
  end
103
103
  it { should == [nil, '2', '1'] }
104
104
  end
105
+
105
106
  context 'opt with value, opt without value, req' do
106
107
  before do
107
108
  def m(b = 'b', x = 'x', a) end
108
109
  end
109
110
  it { should == ['2', '1'] }
110
111
  end
112
+
113
+ if Rails::VERSION::MAJOR >= 4
114
+ context 'req without a value' do
115
+ before do
116
+ def m(x) end
117
+ end
118
+ it { expect { subject }.to raise_error ActionController::BadRequest }
119
+ end
120
+ end
121
+
122
+ if RUBY_VERSION >= '2'
123
+ eval <<-KWARGS_TEST
124
+ context 'key' do
125
+ before do
126
+ def m(a: nil) end
127
+ end
128
+ it { should == [a: '1'] }
129
+ end
130
+
131
+ context 'key, key without value' do
132
+ before do
133
+ def m(a: nil, x: 'x') end
134
+ end
135
+ it { should == [a: '1'] }
136
+ end
137
+ KWARGS_TEST
138
+ end
139
+
140
+ if RUBY_VERSION >= '2.1'
141
+ eval <<-KWARGS_KEYREQ_TEST
142
+ context 'keyreq' do
143
+ before do
144
+ def m(a:) end
145
+ end
146
+ it { should == [a: '1'] }
147
+ end
148
+
149
+ if Rails::VERSION::MAJOR >= 4
150
+ context 'keyreq, keyreq without value' do
151
+ before do
152
+ def m(a:, x:) end
153
+ end
154
+ it { expect { subject }.to raise_error ::ActionController::BadRequest }
155
+ end
156
+ end
157
+ KWARGS_KEYREQ_TEST
158
+ end
111
159
  end
112
160
 
113
161
  if defined? ActionController::StrongParameters
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.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: 2014-05-24 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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'
13
41
  description: Rails plugin gem that supports Merbish style controller action arguments.
14
42
  email:
15
43
  - ronnie@dio.jp
@@ -19,6 +47,7 @@ extra_rdoc_files: []
19
47
  files:
20
48
  - ".gitignore"
21
49
  - ".travis.yml"
50
+ - CONTRIBUTING.md
22
51
  - Gemfile
23
52
  - MIT-LICENSE
24
53
  - README.md
@@ -26,6 +55,7 @@ files:
26
55
  - action_args.gemspec
27
56
  - gemfiles/rails_32.gemfile
28
57
  - gemfiles/rails_40.gemfile
58
+ - gemfiles/rails_41.gemfile
29
59
  - lib/action_args.rb
30
60
  - lib/action_args/abstract_controller.rb
31
61
  - lib/action_args/params_handler.rb
@@ -36,10 +66,12 @@ files:
36
66
  - lib/generators/rails/templates/controller.rb
37
67
  - spec/controllers/action_args_controller_spec.rb
38
68
  - spec/controllers/kwargs_controller_spec.rb
69
+ - spec/controllers/kwargs_keyreq_controller_spec.rb
39
70
  - spec/controllers/ordinal_controller_spec.rb
40
71
  - spec/controllers/strong_parameters_spec.rb
41
72
  - spec/fake_app.rb
42
73
  - spec/kwargs_controllers.rb
74
+ - spec/kwargs_keyreq_controllers.rb
43
75
  - spec/mailers/action_mailer_spec.rb
44
76
  - spec/params_handler/params_handler_spec.rb
45
77
  - spec/spec_helper.rb
@@ -69,10 +101,12 @@ summary: Controller action arguments parameterizer for Rails 3+ & Ruby 1.9+
69
101
  test_files:
70
102
  - spec/controllers/action_args_controller_spec.rb
71
103
  - spec/controllers/kwargs_controller_spec.rb
104
+ - spec/controllers/kwargs_keyreq_controller_spec.rb
72
105
  - spec/controllers/ordinal_controller_spec.rb
73
106
  - spec/controllers/strong_parameters_spec.rb
74
107
  - spec/fake_app.rb
75
108
  - spec/kwargs_controllers.rb
109
+ - spec/kwargs_keyreq_controllers.rb
76
110
  - spec/mailers/action_mailer_spec.rb
77
111
  - spec/params_handler/params_handler_spec.rb
78
112
  - spec/spec_helper.rb