forked from adelcambre/rails-action-args
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it a module rather than monkey patching directly
- Loading branch information
Andy Delcambre
committed
Aug 23, 2009
1 parent
f11b8ab
commit 2e6efcc
Showing
4 changed files
with
62 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,53 @@ | ||
class AbstractController::Base | ||
|
||
class << self | ||
attr_accessor :action_argument_list | ||
alias_method :old_inherited, :inherited | ||
module ActionArgs | ||
|
||
# Stores the argument lists for all methods for this class. | ||
# | ||
# ==== Parameters | ||
# klass<Class>:: | ||
# The controller that is being inherited from Merb::AbstractController. | ||
def inherited(klass) | ||
klass.action_argument_list = Hash.new do |h,k| | ||
args = klass.instance_method(k).get_args | ||
arguments = args[0] | ||
defaults = [] | ||
arguments.each {|a| defaults << a[0] if a.size == 2} if arguments | ||
h[k] = [arguments || [], defaults] | ||
def self.included(base) | ||
|
||
base.class_eval do | ||
class << self | ||
attr_accessor :action_argument_list | ||
alias_method :old_inherited, :inherited | ||
|
||
# Stores the argument lists for all methods for this class. | ||
# | ||
# ==== Parameters | ||
# klass<Class>:: | ||
# The controller that is being inherited from Merb::AbstractController. | ||
def inherited(klass) | ||
klass.action_argument_list = Hash.new do |hash,action| | ||
args = klass.instance_method(action).get_args | ||
arguments = args[0] | ||
defaults = [] | ||
arguments.each {|a| defaults << a[0] if a.size == 2} if arguments | ||
hash[action] = [arguments || [], defaults] | ||
end | ||
old_inherited(klass) | ||
end | ||
end | ||
old_inherited(klass) | ||
end | ||
end | ||
|
||
alias :old_send_action :send_action | ||
# Calls an action and maps the params hash to the action parameters. | ||
# | ||
# ==== Parameters | ||
# action<Symbol>:: The action to call | ||
# | ||
# ==== Raises | ||
# BadRequest:: The params hash doesn't have a required parameter. | ||
def send_action(action) | ||
arguments, defaults = self.class.action_argument_list[action.to_s] | ||
|
||
# Calls an action and maps the params hash to the action parameters. | ||
# | ||
# ==== Parameters | ||
# action<Symbol>:: The action to call | ||
# | ||
# ==== Raises | ||
# BadRequest:: The params hash doesn't have a required parameter. | ||
def send_action(action) | ||
arguments, defaults = self.class.action_argument_list[action] | ||
|
||
args = arguments.map do |arg, default| | ||
p = params.key?(arg.to_sym) | ||
unless p || (defaults && defaults.include?(arg)) | ||
missing = arguments.reject {|arg| params.key?(arg[0].to_sym || arg[1])} | ||
raise BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}" | ||
args = arguments.map do |arg, default| | ||
p = params.key?(arg.to_sym) | ||
unless p || (defaults && defaults.include?(arg)) | ||
missing = arguments.reject {|arg| params.key?(arg[0].to_sym || arg[1])} | ||
raise BadRequest, "Your parameters (#{params.inspect}) were missing #{missing.join(", ")}" | ||
end | ||
p ? params[arg.to_sym] : default | ||
end | ||
old_send_action(action, *args) | ||
end | ||
p ? params[arg.to_sym] : default | ||
end | ||
__send__(action, *args) | ||
end | ||
|
||
end | ||
|
||
AbstractController::Base.send(:include, ActionArgs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters