rails-action-args 0.1.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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.markdown +64 -0
- data/Rakefile +53 -0
- data/VERSION.yml +4 -0
- data/lib/rails_action_args.rb +2 -0
- data/lib/rails_action_args/abstract_controller.rb +47 -0
- data/lib/rails_action_args/get_args.rb +15 -0
- data/lib/rails_action_args/jruby_args.rb +67 -0
- data/lib/rails_action_args/mri_args.rb +102 -0
- data/lib/rails_action_args/vm_args.rb +27 -0
- data/rails-action-args.gemspec +65 -0
- data/spec/controllers/action_args_controller.rb +66 -0
- data/spec/rails_action_args_spec.rb +63 -0
- data/spec/spec_helper.rb +12 -0
- metadata +104 -0
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andy Delcambre
|
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.markdown
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# rails-action-args
|
2
|
+
|
3
|
+
Rails Action Args allows you to accept the parameters to you action as
|
4
|
+
arguments to the action method.
|
5
|
+
|
6
|
+
The best way to demonstrate this is with a quick example.
|
7
|
+
|
8
|
+
First you need to include ActionArgs in your ApplicationController
|
9
|
+
|
10
|
+
class ApplicationController < ActionController::Base
|
11
|
+
include ActionArgs
|
12
|
+
end
|
13
|
+
|
14
|
+
Then you simply accept arguments to your methods.
|
15
|
+
|
16
|
+
class PostsController < ApplicationController
|
17
|
+
respond_to :json
|
18
|
+
|
19
|
+
def show(id)
|
20
|
+
@post = Post.find(id)
|
21
|
+
respond_with @post
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class MyApp < Rails::Application
|
26
|
+
root :to => "posts#show"
|
27
|
+
end
|
28
|
+
|
29
|
+
Now if this is called with {:id => 1} in the params, the id will be set to 1.
|
30
|
+
|
31
|
+
> curl http://localhost:3000?id=1
|
32
|
+
{"id": 1, "title":"Lorem ipsum dolor"}
|
33
|
+
|
34
|
+
If you call the action without the correct parameter, you get a 400 Bad Request
|
35
|
+
|
36
|
+
> curl http://localhost:3000
|
37
|
+
HTTP/1.1 400 Bad Request
|
38
|
+
|
39
|
+
|
40
|
+
You can also supply default arguments as normal.
|
41
|
+
|
42
|
+
class PostsController < ApplicationController
|
43
|
+
respond_to :json
|
44
|
+
|
45
|
+
def show(id=4)
|
46
|
+
@post = Post.find(id)
|
47
|
+
respond_with @post
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
> curl http://localhost:3000?id=1
|
52
|
+
{"id": 4, "title":"Consectur sit amet"}
|
53
|
+
|
54
|
+
## Authors
|
55
|
+
|
56
|
+
This plugin is very heavily influenced by the merb-action-args plugin.
|
57
|
+
All authors of that plugin helped make this one happen.
|
58
|
+
|
59
|
+
Special thanks to Yehuda Katz for writing much of the original merb plugin
|
60
|
+
and helping convert this to Rails 3.
|
61
|
+
|
62
|
+
## Copyright
|
63
|
+
|
64
|
+
Copyright (c) 2010 Andy Delcambre. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "rails-action-args"
|
9
|
+
gem.summary = %Q{A port of merb-action-args to Rails 3}
|
10
|
+
gem.description = "Rails Action Args allows you to accept the parameters to you action as arguments to the action method."
|
11
|
+
gem.email = "adelcambre@engineyard.com"
|
12
|
+
gem.homepage = "http://github.com/adelcambre/rails-action-args"
|
13
|
+
gem.authors = ["Andy Delcambre"]
|
14
|
+
|
15
|
+
gem.add_dependency "actionpack"
|
16
|
+
gem.add_dependency "activesupport"
|
17
|
+
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
if File.exist?('VERSION.yml')
|
42
|
+
config = YAML.load(File.read('VERSION.yml'))
|
43
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
44
|
+
else
|
45
|
+
version = ""
|
46
|
+
end
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "rails-action-args #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
53
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'abstract_controller'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
# Hook up the BadRequest exception to return 400 Bad Request
|
5
|
+
class AbstractController::BadRequest < StandardError; end
|
6
|
+
ActionDispatch::ShowExceptions.rescue_responses["AbstractController::BadRequest"] = :bad_request
|
7
|
+
|
8
|
+
module ActionArgs
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def action_arguments(action)
|
13
|
+
@action_arguments ||= {}
|
14
|
+
return @action_arguments[action] if @action_arguments[action]
|
15
|
+
|
16
|
+
arguments = instance_method(action).get_args.first || []
|
17
|
+
|
18
|
+
defaults = arguments.map do |arg|
|
19
|
+
if arg.size == 2
|
20
|
+
arg.first
|
21
|
+
end
|
22
|
+
end.compact
|
23
|
+
@action_arguments[action] = [arguments, defaults]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Calls an action and maps the params hash to the action parameters.
|
28
|
+
#
|
29
|
+
# ==== Parameters
|
30
|
+
# action<Symbol>:: The action to call
|
31
|
+
#
|
32
|
+
# ==== Raises
|
33
|
+
# BadRequest:: The params hash doesn't have a required parameter.
|
34
|
+
def send_action(action)
|
35
|
+
arguments, defaults = self.class.action_arguments(action.to_s)
|
36
|
+
|
37
|
+
args = arguments.map do |arg, default|
|
38
|
+
p = params.key?(arg.to_sym)
|
39
|
+
unless p || (defaults && defaults.include?(arg))
|
40
|
+
missing = arguments.reject {|arg| params.key?(arg[0].to_sym || arg[1])}
|
41
|
+
raise AbstractController::BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}"
|
42
|
+
end
|
43
|
+
p ? params[arg.to_sym] : default
|
44
|
+
end
|
45
|
+
super(action, *args)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
if RUBY_PLATFORM == "java"
|
2
|
+
require File.join(File.dirname(__FILE__), "jruby_args")
|
3
|
+
elsif RUBY_VERSION < "1.9"
|
4
|
+
require File.join(File.dirname(__FILE__), "mri_args")
|
5
|
+
else
|
6
|
+
require File.join(File.dirname(__FILE__), "vm_args")
|
7
|
+
end
|
8
|
+
|
9
|
+
class UnboundMethod
|
10
|
+
include GetArgs
|
11
|
+
end
|
12
|
+
|
13
|
+
class Method
|
14
|
+
include GetArgs
|
15
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'jruby'
|
3
|
+
|
4
|
+
module GetArgs
|
5
|
+
Methods = org.jruby.internal.runtime.methods
|
6
|
+
|
7
|
+
def get_args
|
8
|
+
real_method = JRuby.reference(self)
|
9
|
+
|
10
|
+
# hack to expose a protected field; could be improved in 1.1.5
|
11
|
+
method_field = org.jruby.RubyMethod.java_class.declared_field(:method)
|
12
|
+
|
13
|
+
method_field.accessible = true
|
14
|
+
|
15
|
+
dyn_method = method_field.value(real_method)
|
16
|
+
|
17
|
+
case dyn_method
|
18
|
+
when Methods.MethodArgs
|
19
|
+
return build_args(dyn_method.args_node)
|
20
|
+
else
|
21
|
+
raise "Can't get args from method: #{self}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_args(args_node)
|
26
|
+
args = []
|
27
|
+
required = []
|
28
|
+
optional = []
|
29
|
+
|
30
|
+
# required args
|
31
|
+
if (args_node.args && args_node.args.size > 0)
|
32
|
+
required << args_node.args.child_nodes.map { |arg| [arg.name.to_s.intern] }
|
33
|
+
end
|
34
|
+
|
35
|
+
# optional args
|
36
|
+
if (args_node.opt_args && args_node.opt_args.size > 0)
|
37
|
+
optional << args_node.opt_args.child_nodes.map do |arg|
|
38
|
+
name = arg.name.to_s.intern
|
39
|
+
value_node = arg.value_node
|
40
|
+
case value_node
|
41
|
+
when org.jruby.ast::FixnumNode
|
42
|
+
value = value_node.value
|
43
|
+
when org.jruby.ast::SymbolNode
|
44
|
+
value = value_node.get_symbol(JRuby.runtime)
|
45
|
+
when org.jruby.ast::StrNode
|
46
|
+
value = value_node.value
|
47
|
+
else
|
48
|
+
value = nil
|
49
|
+
end
|
50
|
+
[name, value]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
first_args = required.first
|
55
|
+
optional.first.each {|arg| first_args << arg} if optional.first
|
56
|
+
|
57
|
+
args = [first_args]
|
58
|
+
|
59
|
+
rest = args_node.rest_arg_node
|
60
|
+
args << (rest ? rest.name.to_s.intern : nil)
|
61
|
+
|
62
|
+
block = args_node.block_arg_node
|
63
|
+
args << (block ? block.name.to_s.intern : nil)
|
64
|
+
|
65
|
+
args
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'parse_tree'
|
2
|
+
require 'ruby2ruby'
|
3
|
+
|
4
|
+
class Object
|
5
|
+
def full_const_get(name)
|
6
|
+
list = name.split("::")
|
7
|
+
list.shift if list.first.blank?
|
8
|
+
obj = self
|
9
|
+
list.each do |x|
|
10
|
+
# This is required because const_get tries to look for constants in the
|
11
|
+
# ancestor chain, but we only want constants that are HERE
|
12
|
+
obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
|
13
|
+
end
|
14
|
+
obj
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ParseTreeArray < Array
|
19
|
+
R2R = Object.const_defined?(:Ruby2Ruby) ? Ruby2Ruby : RubyToRuby
|
20
|
+
|
21
|
+
def self.translate(*args)
|
22
|
+
sexp = ParseTree.translate(*args)
|
23
|
+
# ParseTree.translate returns [nil] if called on an inherited method, so walk
|
24
|
+
# up the inheritance chain to find the class that the method was defined in
|
25
|
+
unless sexp.first
|
26
|
+
klass = args.first.ancestors.detect do |klass|
|
27
|
+
klass.public_instance_methods(false).include?(args.last.to_s)
|
28
|
+
end
|
29
|
+
sexp = ParseTree.translate(klass, args.last) if klass
|
30
|
+
end
|
31
|
+
sexp = Unifier.new.process(sexp)
|
32
|
+
self.new(sexp)
|
33
|
+
end
|
34
|
+
|
35
|
+
def deep_array_node(type = nil)
|
36
|
+
each do |node|
|
37
|
+
return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
|
38
|
+
next unless node.is_a?(Array)
|
39
|
+
return ParseTreeArray.new(node).deep_array_node(type)
|
40
|
+
end
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def arg_nodes
|
45
|
+
self[1..-1].inject([]) do |sum,item|
|
46
|
+
sum << [item] unless item.is_a?(Array)
|
47
|
+
sum
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_args
|
52
|
+
if arg_node = deep_array_node(:args)
|
53
|
+
# method defined with def keyword
|
54
|
+
args = arg_node.arg_nodes
|
55
|
+
default_node = arg_node.deep_array_node(:block)
|
56
|
+
return [args, []] unless default_node
|
57
|
+
else
|
58
|
+
# assuming method defined with Module#define_method
|
59
|
+
return [[],[]]
|
60
|
+
end
|
61
|
+
|
62
|
+
# if it was defined with def, and we found the default_node,
|
63
|
+
# that should bring us back to regularly scheduled programming..
|
64
|
+
lasgns = default_node[1..-1]
|
65
|
+
lasgns.each do |asgn|
|
66
|
+
args.assoc(asgn[1]) << eval(R2R.new.process(asgn[2]))
|
67
|
+
end
|
68
|
+
[args, (default_node[1..-1].map { |asgn| asgn[1] })]
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
# Used in mapping controller arguments to the params hash.
|
74
|
+
# NOTE: You must have the 'ruby2ruby' gem installed for this to work.
|
75
|
+
#
|
76
|
+
# ==== Examples
|
77
|
+
# # In PostsController
|
78
|
+
# def show(id) #=> id is the same as params[:id]
|
79
|
+
module GetArgs
|
80
|
+
|
81
|
+
# ==== Returns
|
82
|
+
# Array:: Method arguments and their default values.
|
83
|
+
#
|
84
|
+
# ==== Examples
|
85
|
+
# class Example
|
86
|
+
# def hello(one,two="two",three)
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# def goodbye
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# Example.instance_method(:hello).get_args
|
94
|
+
# #=> [[:one], [:two, "two"], [:three, "three"]]
|
95
|
+
# Example.instance_method(:goodbye).get_args #=> nil
|
96
|
+
def get_args
|
97
|
+
klass, meth = self.to_s.split(/ /).to_a[1][0..-2].split("#")
|
98
|
+
# Remove stupidity for #<Method: Class(Object)#foo>
|
99
|
+
klass = $` if klass =~ /\(/
|
100
|
+
ParseTreeArray.translate(Object.full_const_get(klass), meth).get_args
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require "methopara"
|
3
|
+
rescue
|
4
|
+
puts "make sure you have methora http://github.com/genki/methopara installed if you want to use action args on Ruby 1.9"
|
5
|
+
end
|
6
|
+
|
7
|
+
module GetArgs
|
8
|
+
def get_args
|
9
|
+
unless respond_to?(:parameters)
|
10
|
+
raise NotImplementedError, "Ruby #{RUBY_VERSION} doesn't support #{self.class}#parameters"
|
11
|
+
end
|
12
|
+
|
13
|
+
required = []
|
14
|
+
optional = []
|
15
|
+
|
16
|
+
parameters.each do |(type, name)|
|
17
|
+
if type == :opt
|
18
|
+
required << [name, nil]
|
19
|
+
optional << name
|
20
|
+
else
|
21
|
+
required << [name]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
return [required, optional]
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails-action-args}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andy Delcambre"]
|
12
|
+
s.date = %q{2010-04-03}
|
13
|
+
s.description = %q{Rails Action Args allows you to accept the parameters to you action as arguments to the action method.}
|
14
|
+
s.email = %q{adelcambre@engineyard.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION.yml",
|
27
|
+
"lib/rails_action_args.rb",
|
28
|
+
"lib/rails_action_args/abstract_controller.rb",
|
29
|
+
"lib/rails_action_args/get_args.rb",
|
30
|
+
"lib/rails_action_args/jruby_args.rb",
|
31
|
+
"lib/rails_action_args/mri_args.rb",
|
32
|
+
"lib/rails_action_args/vm_args.rb",
|
33
|
+
"rails-action-args.gemspec",
|
34
|
+
"spec/controllers/action_args_controller.rb",
|
35
|
+
"spec/rails_action_args_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/adelcambre/rails-action-args}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.6}
|
42
|
+
s.summary = %q{A port of merb-action-args to Rails 3}
|
43
|
+
s.test_files = [
|
44
|
+
"spec/controllers/action_args_controller.rb",
|
45
|
+
"spec/rails_action_args_spec.rb",
|
46
|
+
"spec/spec_helper.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
54
|
+
s.add_runtime_dependency(%q<actionpack>, [">= 0"])
|
55
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<actionpack>, [">= 0"])
|
58
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<actionpack>, [">= 0"])
|
62
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
include ActionArgs
|
3
|
+
end
|
4
|
+
|
5
|
+
module ExtraActions
|
6
|
+
def funky_inherited_method(foo, bar)
|
7
|
+
render :text => "#{foo} #{bar}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Awesome
|
12
|
+
class ActionArgsController < ApplicationController
|
13
|
+
def index(foo)
|
14
|
+
render :text => foo.to_s
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ActionArgsController < ApplicationController
|
20
|
+
include ExtraActions
|
21
|
+
|
22
|
+
def nada
|
23
|
+
render :text => "NADA"
|
24
|
+
end
|
25
|
+
|
26
|
+
def index(foo)
|
27
|
+
render :text => foo
|
28
|
+
end
|
29
|
+
|
30
|
+
def multi(foo, bar)
|
31
|
+
render :text => "#{foo} #{bar}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def defaults(foo, bar = "bar")
|
35
|
+
render :text => "#{foo} #{bar}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def defaults_mixed(foo, bar ="bar", baz = "baz")
|
39
|
+
render :text => "#{foo} #{bar} #{baz}"
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method :dynamic_define_method do
|
43
|
+
render :text => "mos def"
|
44
|
+
end
|
45
|
+
|
46
|
+
def with_default_nil(foo, bar = nil)
|
47
|
+
render :text => "#{foo} #{bar}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def with_default_array(foo, bar = [])
|
51
|
+
render :text => "#{foo} #{bar.inspect}"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class MyApp < Rails::Application
|
57
|
+
config.session_store :disabled
|
58
|
+
|
59
|
+
routes.draw do
|
60
|
+
match "/awesome/index" => "Awesome::ActionArgs#index"
|
61
|
+
|
62
|
+
controller :action_args do
|
63
|
+
match "/action_args/:action"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
2
|
+
|
3
|
+
describe "RailsActionArgs" do
|
4
|
+
def app
|
5
|
+
MyApp
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should be able to handle a nested class" do
|
9
|
+
request("/awesome/index?foo=bar").body.should == "bar"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be able to handle no arguments" do
|
13
|
+
request("/action_args/nada").body.should == "NADA"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be able to accept Action Arguments" do
|
17
|
+
request("/action_args/index?foo=bar").body.should == "bar"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be able to accept multiple Action Arguments" do
|
21
|
+
request("/action_args/multi?foo=bar&bar=baz").body.should == "bar baz"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be able to handle defaults in Action Arguments" do
|
25
|
+
request("/action_args/defaults?foo=bar").body.should == "bar bar"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be able to handle out of order defaults" do
|
29
|
+
request("/action_args/defaults_mixed?foo=bar&baz=bar").body.should == "bar bar bar"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should throw a 400 Bad Request if the arguments are not provided" do
|
33
|
+
request("/action_args/index").status.should == 400
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should treat define_method actions as equal" do
|
37
|
+
request("/action_args/dynamic_define_method").body.should == "mos def"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be able to inherit actions for use with Action Arguments" do
|
41
|
+
request("/action_args/funky_inherited_method?foo=bar&bar=baz").body.should == "bar baz"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be able to handle nil defaults" do
|
45
|
+
request("/action_args/with_default_nil?foo=bar").body.should == "bar "
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be able to handle [] defaults" do
|
49
|
+
request("/action_args/with_default_array?foo=bar").body.should == "bar []"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should print out the missing parameters if all are required" do
|
53
|
+
response = request("/action_args/multi")
|
54
|
+
response.status.should == 400
|
55
|
+
response.body.should include("were missing foo, bar")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should only print out missing parameters" do
|
59
|
+
response = request("/action_args/multi?foo=Hello")
|
60
|
+
response.status.should == 400
|
61
|
+
response.body.should include("were missing bar")
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.setup
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
require 'rails_action_args'
|
8
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "controllers", "action_args_controller"))
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
config.include(Rack::Test::Methods)
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-action-args
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Andy Delcambre
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-03 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: actionpack
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: activesupport
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
description: Rails Action Args allows you to accept the parameters to you action as arguments to the action method.
|
45
|
+
email: adelcambre@engineyard.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE
|
52
|
+
- README.markdown
|
53
|
+
files:
|
54
|
+
- .document
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.markdown
|
59
|
+
- Rakefile
|
60
|
+
- VERSION.yml
|
61
|
+
- lib/rails_action_args.rb
|
62
|
+
- lib/rails_action_args/abstract_controller.rb
|
63
|
+
- lib/rails_action_args/get_args.rb
|
64
|
+
- lib/rails_action_args/jruby_args.rb
|
65
|
+
- lib/rails_action_args/mri_args.rb
|
66
|
+
- lib/rails_action_args/vm_args.rb
|
67
|
+
- rails-action-args.gemspec
|
68
|
+
- spec/controllers/action_args_controller.rb
|
69
|
+
- spec/rails_action_args_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/adelcambre/rails-action-args
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.3.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: A port of merb-action-args to Rails 3
|
101
|
+
test_files:
|
102
|
+
- spec/controllers/action_args_controller.rb
|
103
|
+
- spec/rails_action_args_spec.rb
|
104
|
+
- spec/spec_helper.rb
|