action_args 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +149 -0
- data/Rakefile +2 -0
- data/action_args.gemspec +21 -0
- data/lib/action_args/abstract_controller.rb +10 -0
- data/lib/action_args/version.rb +3 -0
- data/lib/action_args.rb +11 -0
- data/lib/generators/rails/action_args_scaffold_controller_generator.rb +9 -0
- data/lib/generators/rails/templates/controller.rb +53 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2004-2011 David Heinemeier Hansson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
= ActionArgs
|
2
|
+
|
3
|
+
Controller action arguments parameterizer for Rails 3 + Ruby 1.9
|
4
|
+
|
5
|
+
|
6
|
+
== What is this?
|
7
|
+
|
8
|
+
ActionArgs is a Rails 3 plugin that extends your controller action methods to look and act like simple general Ruby methods with meaningful parameters, or in short, Merbish.
|
9
|
+
|
10
|
+
|
11
|
+
== The Controllers
|
12
|
+
|
13
|
+
Having the following controller code:
|
14
|
+
|
15
|
+
class HogeController < ApplicationController
|
16
|
+
def fuga(piyo)
|
17
|
+
render :text => piyo
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Hitting "/hoge/fuga?piyo=foo" will call fuga('foo') and output 'foo'.
|
22
|
+
So, you do never need to touch the ugly +params+ Hash to get the request parameters.
|
23
|
+
|
24
|
+
|
25
|
+
== The Scaffold Generator
|
26
|
+
|
27
|
+
To tell you the truth, Rails 3 is originally designed to be able to very easily add this functionality (because Carlhuda did the work on this), so perhaps the most characteristic feature of this plugin is the generator.
|
28
|
+
ActionArgs provides a custom scaffold controller generator that overwrites the default scaffold generator.
|
29
|
+
Thus, by hitting the following command:
|
30
|
+
|
31
|
+
% rails g scaffold user name:string
|
32
|
+
|
33
|
+
The following beautiful controller code will be generated:
|
34
|
+
|
35
|
+
# coding: utf-8
|
36
|
+
|
37
|
+
class UsersController < ApplicationController
|
38
|
+
# GET /users
|
39
|
+
def index
|
40
|
+
@users = User.all
|
41
|
+
end
|
42
|
+
|
43
|
+
# GET /users/1
|
44
|
+
def show(id)
|
45
|
+
@user = User.find(id)
|
46
|
+
end
|
47
|
+
|
48
|
+
# GET /users/new
|
49
|
+
def new
|
50
|
+
@user = User.new
|
51
|
+
end
|
52
|
+
|
53
|
+
# GET /users/1/edit
|
54
|
+
def edit(id)
|
55
|
+
@user = User.find(id)
|
56
|
+
end
|
57
|
+
|
58
|
+
# POST /users
|
59
|
+
def create(user)
|
60
|
+
@user = User.new(user)
|
61
|
+
|
62
|
+
if @user.save
|
63
|
+
redirect_to @user, :notice => 'User was successfully created.'
|
64
|
+
else
|
65
|
+
render :action => 'new'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# PUT /users/1
|
70
|
+
def update(id, user)
|
71
|
+
@user = User.find(id)
|
72
|
+
|
73
|
+
if @user.update_attributes(user)
|
74
|
+
redirect_to @user, :notice => 'User was successfully updated.'
|
75
|
+
else
|
76
|
+
render :action => 'edit'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# DELETE /users/1
|
81
|
+
def destroy(id)
|
82
|
+
@user = User.find(id)
|
83
|
+
@user.destroy
|
84
|
+
|
85
|
+
redirect_to users_url
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
You may notice that
|
90
|
+
* There are no globalish +params+ referrence
|
91
|
+
* It's quite easy to comprehend what's the actual input value for each action
|
92
|
+
* You may write the unit test code as if the actions are just normal Ruby methods
|
93
|
+
|
94
|
+
|
95
|
+
== Supported versions
|
96
|
+
|
97
|
+
* Ruby 1.9.2, 1.9.3 (trunk)
|
98
|
+
|
99
|
+
* Rails 3.0.x, 3.1 (edge)
|
100
|
+
|
101
|
+
|
102
|
+
== Installation
|
103
|
+
|
104
|
+
Put this line in your Gemfile:
|
105
|
+
gem 'action_args'
|
106
|
+
|
107
|
+
Then bundle:
|
108
|
+
% bundle
|
109
|
+
|
110
|
+
|
111
|
+
== Notes
|
112
|
+
|
113
|
+
=== Plain Old Action Methods
|
114
|
+
|
115
|
+
Of courese you still can use both Merbish style and plain old Rails style action methods even if this plugin is loaded. +params+ parameter is still alive as well. That means, this plugin won't break any existing controller API.
|
116
|
+
|
117
|
+
=== Argument Naming Convention
|
118
|
+
|
119
|
+
Each action method parameter name corresponds to +params+ key name. For example, the following beautifully written nested show action works perfectly (this might not be a very good example of effective querying, but that's another story).
|
120
|
+
|
121
|
+
Rails.application.routes.draw do
|
122
|
+
resources :authors do
|
123
|
+
resources :books
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class BooksController < ApplicationController
|
128
|
+
# GET /authors/:author_id/books/:id
|
129
|
+
def show(author_id, id)
|
130
|
+
@book = Author.find(author_id).books.find(id)
|
131
|
+
end
|
132
|
+
...
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
== Todo
|
137
|
+
|
138
|
+
* specs
|
139
|
+
* other Ruby implementations
|
140
|
+
|
141
|
+
|
142
|
+
== Conclusion
|
143
|
+
|
144
|
+
Ruby 1.9 on Rails 3 FTW!
|
145
|
+
|
146
|
+
|
147
|
+
== Copyright
|
148
|
+
|
149
|
+
Copyright (c) 2011 Asakusa.rb. See MIT-LICENSE for further details.
|
data/Rakefile
ADDED
data/action_args.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'action_args/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'action_args'
|
7
|
+
s.version = ActionArgs::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Akira Matsuda']
|
10
|
+
s.email = ['ronnie@dio.jp']
|
11
|
+
s.homepage = 'http://asakusa.rubyist.net/'
|
12
|
+
s.summary = 'Controller action arguments parameterizer for Rails 3 + Ruby 1.9'
|
13
|
+
s.description = 'Rails 3 plugin gem that supports Merbish style controller action arguments.'
|
14
|
+
|
15
|
+
s.rubyforge_project = 'action_args'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/action_args.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'action_args/abstract_controller')
|
2
|
+
|
3
|
+
module ActionArgs
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
if config.respond_to? :app_generators
|
6
|
+
config.app_generators.scaffold_controller = :action_args_scaffold_controller
|
7
|
+
else
|
8
|
+
config.generators.scaffold_controller = :action_args_scaffold_controller
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class ActionArgsScaffoldControllerGenerator < ::Rails::Generators::ScaffoldControllerGenerator
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
4
|
+
# GET <%= route_url %>
|
5
|
+
def index
|
6
|
+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
7
|
+
end
|
8
|
+
|
9
|
+
# GET <%= route_url %>/1
|
10
|
+
def show(id)
|
11
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
12
|
+
end
|
13
|
+
|
14
|
+
# GET <%= route_url %>/new
|
15
|
+
def new
|
16
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
|
17
|
+
end
|
18
|
+
|
19
|
+
# GET <%= route_url %>/1/edit
|
20
|
+
def edit(id)
|
21
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
22
|
+
end
|
23
|
+
|
24
|
+
# POST <%= route_url %>
|
25
|
+
def create(<%= singular_table_name %>)
|
26
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, singular_table_name) %>
|
27
|
+
|
28
|
+
if @<%= orm_instance.save %>
|
29
|
+
redirect_to @<%= singular_table_name %>, notice: '<%= human_name %> was successfully created.'
|
30
|
+
else
|
31
|
+
render action: 'new'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# PUT <%= route_url %>/1
|
36
|
+
def update(id, <%= singular_table_name %>)
|
37
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
38
|
+
|
39
|
+
if @<%= orm_instance.update_attributes(singular_table_name) %>
|
40
|
+
redirect_to @<%= singular_table_name %>, notice: '<%= human_name %> was successfully updated.'
|
41
|
+
else
|
42
|
+
render action: 'edit'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# DELETE <%= route_url %>/1
|
47
|
+
def destroy(id)
|
48
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
49
|
+
@<%= orm_instance.destroy %>
|
50
|
+
|
51
|
+
redirect_to <%= index_helper %>_url
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_args
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Akira Matsuda
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-17 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Rails 3 plugin gem that supports Merbish style controller action arguments.
|
22
|
+
email:
|
23
|
+
- ronnie@dio.jp
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- action_args.gemspec
|
37
|
+
- lib/action_args.rb
|
38
|
+
- lib/action_args/abstract_controller.rb
|
39
|
+
- lib/action_args/version.rb
|
40
|
+
- lib/generators/rails/action_args_scaffold_controller_generator.rb
|
41
|
+
- lib/generators/rails/templates/controller.rb
|
42
|
+
homepage: http://asakusa.rubyist.net/
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: action_args
|
71
|
+
rubygems_version: 1.8.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Controller action arguments parameterizer for Rails 3 + Ruby 1.9
|
75
|
+
test_files: []
|
76
|
+
|