0
@@ -1354,8 +1354,8 @@ module ActiveRecord #:nodoc:
0
def respond_to?(method_id, include_private = false)
0
- if match = matches_dynamic_finder?(method_id) || matches_dynamic_finder_with_initialize_or_create?(method_id)
0
- return true if all_attributes_exists?(extract_attribute_names_from_match(match))
0
+ if match = DynamicFinderMatch.match(method_id)
0
+ return true if all_attributes_exists?(match.attribute_names)
0
@@ -1674,88 +1674,65 @@ module ActiveRecord #:nodoc:
0
# Each dynamic finder or initializer/creator is also defined in the class after it is first invoked, so that future
0
# attempts to use it do not run through method_missing.
0
def method_missing(method_id, *arguments)
0
- if match = matches_dynamic_finder?(method_id)
0
- finder = determine_finder(match)
0
- attribute_names = extract_attribute_names_from_match(match)
0
+ if match = DynamicFinderMatch.match(method_id)
0
+ attribute_names = match.attribute_names
0
super unless all_attributes_exists?(attribute_names)
0
- def self.#{method_id}(*args)
0
- options = args.extract_options!
0
- attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
0
- finder_options = { :conditions => attributes }
0
- validate_find_options(options)
0
- set_readonly_option!(options)
0
- if options[:conditions]
0
- with_scope(:find => finder_options) do
0
- ActiveSupport::Deprecation.silence { send(:#{finder}, options) }
0
+ def self.#{method_id}(*args)
0
+ options = args.extract_options!
0
+ attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
0
+ finder_options = { :conditions => attributes }
0
+ validate_find_options(options)
0
+ set_readonly_option!(options)
0
+ if options[:conditions]
0
+ with_scope(:find => finder_options) do
0
+ ActiveSupport::Deprecation.silence { send(:#{finder}, options) }
0
+ ActiveSupport::Deprecation.silence { send(:#{finder}, options.merge(finder_options)) }
0
- ActiveSupport::Deprecation.silence { send(:#{finder}, options.merge(finder_options)) }
0
- send(method_id, *arguments)
0
- elsif match = matches_dynamic_finder_with_initialize_or_create?(method_id)
0
- instantiator = determine_instantiator(match)
0
- attribute_names = extract_attribute_names_from_match(match)
0
- super unless all_attributes_exists?(attribute_names)
0
- def self.#{method_id}(*args)
0
- guard_protected_attributes = false
0
- if args[0].is_a?(Hash)
0
- guard_protected_attributes = true
0
- attributes = args[0].with_indifferent_access
0
- find_attributes = attributes.slice(*[:#{attribute_names.join(',:')}])
0
- find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
0
+ send(method_id, *arguments)
0
+ elsif match.instantiator?
0
+ instantiator = match.instantiator
0
+ def self.#{method_id}(*args)
0
+ guard_protected_attributes = false
0
+ if args[0].is_a?(Hash)
0
+ guard_protected_attributes = true
0
+ attributes = args[0].with_indifferent_access
0
+ find_attributes = attributes.slice(*[:#{attribute_names.join(',:')}])
0
+ find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
0
- options = { :conditions => find_attributes }
0
- set_readonly_option!(options)
0
+ options = { :conditions => find_attributes }
0
+ set_readonly_option!(options)
0
-
record = find_initial(options)
0
+
record = find_initial(options)
0
- record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) }
0
- #{'yield(record) if block_given?'}
0
- #{'record.save' if instantiator == :create}
0
+ record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) }
0
+ #{'yield(record) if block_given?'}
0
+ #{'record.save' if instantiator == :create}
0
- send(method_id, *arguments)
0
+ send(method_id, *arguments)
0
- def matches_dynamic_finder?(method_id)
0
- /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
0
- def matches_dynamic_finder_with_initialize_or_create?(method_id)
0
- /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s)
0
- def determine_finder(match)
0
- match.captures.first == 'all_by' ? :find_every : :find_initial
0
- def determine_instantiator(match)
0
- match.captures.first == 'initialize' ? :new : :create
0
- def extract_attribute_names_from_match(match)
0
- match.captures.last.split('_and_')
0
def construct_attributes_from_arguments(attribute_names, arguments)
0
attribute_names.each_with_index { |name, idx| attributes[name] = arguments[idx] }