action_args 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -25,7 +25,28 @@ So, you do never need to touch the ugly +params+ Hash in order to fetch the requ
25
25
  == StrongParameters
26
26
 
27
27
  ActionArgs plays very nice with Rails 4 StrongParameters.
28
- Hashes in the action method arguments simply responds to the `permit` method.
28
+
29
+ In this `show` action, ActionArgs `require`s the `id` parameter.
30
+ Hence, raises an error if the `id` value has not been specified, in the same way as usual Ruby methods do.
31
+
32
+ class UsersController < ApplicationController
33
+ # the `id` parameter is mandatory
34
+ def show(id)
35
+ @user = User.find id
36
+ end
37
+ end
38
+
39
+ If you don't want ActionArgs to check the existence of some action parameters, you can make them optional by defining option values.
40
+ Again, it just acts in the same way as usual Ruby methods do.
41
+
42
+ class UsersController < ApplicationController
43
+ # the `page` parameter is optional
44
+ def index(page = nil)
45
+ @users = User.page(page).per(50)
46
+ end
47
+ end
48
+
49
+ Hashes in the action method arguments simply respond to the StrongParameters' `permit` method.
29
50
 
30
51
  class UsersController < ApplicationController
31
52
  def create(user)
@@ -55,7 +76,7 @@ The `permits` method assumes the model class from the controller name, and
55
76
  ActionArgs provides a custom scaffold controller generator that overwrites the default scaffold generator.
56
77
  Thus, by hitting the following command:
57
78
 
58
- % rails g scaffold user name:string
79
+ % rails g scaffold user name age:integer email
59
80
 
60
81
  The following beautiful controller code will be generated:
61
82
 
@@ -6,15 +6,17 @@ module AbstractController
6
6
  values = if defined? ActionController::StrongParameters
7
7
  target_model_name = self.class.name.sub(/Controller$/, '').singularize.underscore.to_sym
8
8
  permitted_attributes = self.class.instance_variable_get '@permitted_attributes'
9
- method(method_name).parameters.reject {|kind, _| kind == :block }.map(&:last).map do |k|
10
- if (k == target_model_name) && permitted_attributes
11
- params.require(k).permit(*permitted_attributes)
9
+ method(method_name).parameters.map {|type, key|
10
+ next if type == :block
11
+ params.require key if type == :req
12
+ if (key == target_model_name) && permitted_attributes
13
+ params[key].try :permit, *permitted_attributes
12
14
  else
13
- params.require(k)
15
+ params[key]
14
16
  end
15
- end
17
+ }.compact
16
18
  else
17
- method(method_name).parameters.reject {|kind, _| kind == :block }.map(&:last).map {|k| params[k]}
19
+ method(method_name).parameters.map {|type, key| params[key] unless type == :block }.compact
18
20
  end
19
21
  send method_name, *values
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module ActionArgs
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -1,6 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BooksController do
4
+ describe 'GET index (having an optional parameter)' do
5
+ context 'without page parameter' do
6
+ before { get :index }
7
+ its(:response) { should be_success }
8
+ end
9
+
10
+ context 'with page parameter' do
11
+ before { get :index, page: 3 }
12
+ its(:response) { should be_success }
13
+ end
14
+ end
15
+
4
16
  describe 'GET show' do
5
17
  let(:rhg) { Book.create! title: 'RHG' }
6
18
  before { get :show, :id => rhg.id }
data/spec/fake_app.rb CHANGED
@@ -39,6 +39,12 @@ class AuthorsController < ApplicationController
39
39
  end
40
40
  end
41
41
  class BooksController < ApplicationController
42
+ # optional parameter
43
+ def index(page = 1)
44
+ @books = Book.limit(10).offset((page.to_i - 1) * 10)
45
+ render text: 'index'
46
+ end
47
+
42
48
  def show(id)
43
49
  @book = Book.find(id)
44
50
  render text: @book.title
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3