Permalink
Browse files

Refactored in use of extract_options! (closes #9079) [josh]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7220 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 34b0811 commit edd68a587f412ccdf15613c663acbab341d45017 @dhh dhh committed Jul 24, 2007
@@ -222,7 +222,7 @@ def expire_action(options = {})
class ActionCacheFilter #:nodoc:
def initialize(*actions, &block)
- @options = actions.last.is_a?(Hash) ? actions.pop : {}
+ @options = actions.extract_options!
@actions = Set.new actions
end
@@ -596,7 +596,7 @@ def self.included(base) #:nodoc:
module ClassMethods #:nodoc:
def cache_sweeper(*sweepers)
return unless perform_caching
- configuration = sweepers.last.is_a?(Hash) ? sweepers.pop : {}
+ configuration = sweepers.extract_options!
sweepers.each do |sweeper|
ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
sweeper_instance = Object.const_get(Inflector.classify(sweeper)).instance
@@ -601,7 +601,7 @@ def class_for_filter(filter, filter_type) #:nodoc:
def extract_conditions(*filters, &block) #:nodoc:
filters.flatten!
- conditions = filters.last.is_a?(Hash) ? filters.pop : {}
+ conditions = filters.extract_options!
filters << block if block_given?
return filters, conditions
end
@@ -295,7 +295,7 @@ def initialize(entity, options)
# HTTP POST on <tt>new_message_url</tt> will raise a RoutingError exception. The default route in
# <tt>config/routes.rb</tt> overrides this and allows invalid HTTP methods for resource routes.
def resources(*entities, &block)
- options = entities.last.is_a?(Hash) ? entities.pop : { }
+ options = entities.extract_options!
entities.each { |entity| map_resource(entity, options.dup, &block) }
end
@@ -369,7 +369,7 @@ def resources(*entities, &block)
# edit_account edit_account_url, hash_for_edit_account_url,
# edit_account_path, hash_for_edit_account_path
def resource(*entities, &block)
- options = entities.last.is_a?(Hash) ? entities.pop : { }
+ options = entities.extract_options!
entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
end
@@ -118,7 +118,7 @@ def error_message_on(object, method, prepend_text = "", append_text = "", css_cl
# you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors
# instance yourself and set it up. View the source of this method to see how easy it is.
def error_messages_for(*params)
- options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
+ options = params.extract_options!.symbolize_keys
objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
count = objects.inject(0) {|sum, object| sum + object.errors.count }
unless count.zero?
@@ -172,7 +172,7 @@ def javascript_path(source)
# javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is false =>
# <script type="text/javascript" src="/javascripts/shop.js"></script>
def javascript_include_tag(*sources)
- options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
+ options = sources.extract_options!.stringify_keys
cache = options.delete("cache")
if ActionController::Base.perform_caching && cache
@@ -281,7 +281,7 @@ def stylesheet_path(source)
# stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is true =>
# <link href="/stylesheets/payment.css" media="screen" rel="Stylesheet" type="text/css" />
def stylesheet_link_tag(*sources)
- options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
+ options = sources.extract_options!.stringify_keys
cache = options.delete("cache")
if ActionController::Base.perform_caching && cache
@@ -158,7 +158,7 @@ module FormHelper
def form_for(record_or_name_or_array, *args, &proc)
raise ArgumentError, "Missing block" unless block_given?
- options = args.last.is_a?(Hash) ? args.pop : {}
+ options = args.extract_options!
case record_or_name_or_array
when String, Symbol
@@ -212,7 +212,7 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
# like FormOptionHelper#collection_select and DateHelper#datetime_select.
def fields_for(object_name, *args, &block)
raise ArgumentError, "Missing block" unless block_given?
- options = args.last.is_a?(Hash) ? args.pop : {}
+ options = args.extract_options!
object = args.first
builder = options[:builder] || ActionView::Base.default_form_builder
@@ -80,7 +80,7 @@ module JavaScriptHelper
# return false;">Show me more</a>
#
def link_to_function(name, *args, &block)
- html_options = args.last.is_a?(Hash) ? args.pop : {}
+ html_options = args.extract_options!
function = args[0] || ''
html_options.symbolize_keys!
@@ -111,7 +111,7 @@ def link_to_function(name, *args, &block)
# page[:details].visual_effect :toggle_slide
# end
def button_to_function(name, *args, &block)
- html_options = args.last.is_a?(Hash) ? args.pop : {}
+ html_options = args.extract_options!
function = args[0] || ''
html_options.symbolize_keys!
@@ -182,7 +182,7 @@ def form_remote_tag(options = {}, &block)
# Works like form_remote_tag, but uses form_for semantics.
def remote_form_for(record_or_name, *args, &proc)
- options = args.last.is_a?(Hash) ? args.pop : {}
+ options = args.extract_options!
case record_or_name
when String, Symbol
@@ -98,10 +98,6 @@ def sanitize_sql(sql)
@reflection.klass.send(:sanitize_sql, sql)
end
- def extract_options_from_args!(args)
- @owner.send(:extract_options_from_args!, args)
- end
-
def set_belongs_to_association_for(record)
if @reflection.options[:as]
record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
@@ -29,7 +29,7 @@ def find_first
end
def find(*args)
- options = Base.send(:extract_options_from_args!, args)
+ options = args.extract_options!
# If using a custom finder_sql, scan the entire collection.
if @reflection.options[:finder_sql]
@@ -38,7 +38,7 @@ def count(*args)
end
def find(*args)
- options = Base.send(:extract_options_from_args!, args)
+ options = args.extract_options!
# If using a custom finder_sql, scan the entire collection.
if @reflection.options[:finder_sql]
@@ -9,7 +9,7 @@ def initialize(owner, reflection)
end
def find(*args)
- options = Base.send(:extract_options_from_args!, args)
+ options = args.extract_options!
conditions = "#{@finder_sql}"
if sanitized_conditions = sanitize_sql(options[:conditions])
@@ -421,7 +421,7 @@ class << self # Class methods
# person.save!
# end
def find(*args)
- options = extract_options_from_args!(args)
+ options = args.extract_options!
validate_find_options(options)
set_readonly_option!(options)
@@ -1604,10 +1604,6 @@ def raise_if_bind_arity_mismatch(statement, expected, provided) #:nodoc:
end
end
- def extract_options_from_args!(args) #:nodoc:
- args.last.is_a?(Hash) ? args.pop : {}
- end
-
VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
:order, :select, :readonly, :group, :from, :lock ]
@@ -364,7 +364,7 @@ def column(name, type, options = {})
%w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
class_eval <<-EOV
def #{column_type}(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
+ options = args.extract_options!
column_names = args
column_names.each { |name| column(name, '#{column_type}', options) }
@@ -21,7 +21,7 @@ def #{sym}
end
def cattr_writer(*syms)
- options = syms.last.is_a?(Hash) ? syms.pop : {}
+ options = syms.extract_options!
syms.flatten.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
unless defined? @@#{sym}
@@ -23,7 +23,7 @@ def #{sym}
end
def class_inheritable_writer(*syms)
- options = syms.last.is_a?(Hash) ? syms.pop : {}
+ options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
def self.#{sym}=(obj)
@@ -40,7 +40,7 @@ def #{sym}=(obj)
end
def class_inheritable_array_writer(*syms)
- options = syms.last.is_a?(Hash) ? syms.pop : {}
+ options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
def self.#{sym}=(obj)
@@ -57,7 +57,7 @@ def #{sym}=(obj)
end
def class_inheritable_hash_writer(*syms)
- options = syms.last.is_a?(Hash) ? syms.pop : {}
+ options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
def self.#{sym}=(obj)
@@ -21,7 +21,7 @@ def #{sym}
end
def mattr_writer(*syms)
- options = syms.last.is_a?(Hash) ? syms.pop : {}
+ options = syms.extract_options!
syms.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
unless defined? @@#{sym}
@@ -85,7 +85,7 @@ def extract_callstack(callstack)
module ClassMethods #:nodoc:
# Declare that a method has been deprecated.
def deprecate(*method_names)
- options = method_names.last.is_a?(Hash) ? method_names.pop : {}
+ options = method_names.extract_options!
method_names = method_names + options.keys
method_names.each do |method_name|
alias_method_chain(method_name, :deprecation) do |target, punctuation|

0 comments on commit edd68a5

Please sign in to comment.