Module: HasLocalizationTable::ActiveRecord::FinderMethods

Defined in:
lib/has_localization_table/active_record/finder_methods.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/has_localization_table/active_record/finder_methods.rb', line 4

def method_missing(name, *args, &block)
  if name.to_s =~ /\Afind_by_([a-z0-9_]+(_and_[a-z0-9_]+)*)\Z/
    attributes = $1.split("_and_").map(&:to_sym)
    if (attributes & localized_attributes).size == attributes.size
      raise ArgumentError, "expected #{attributes.size} #{"argument".pluralize(attributes.size)}: #{attributes.join(", ")}" unless args.size == attributes.size
      args = attributes.zip(args).inject({}) { |memo, (key, val)| memo[key] = val; memo }
      return find_by_localized_attributes(args)
    end
  end
  
  super
end

Instance Method Details

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/has_localization_table/active_record/finder_methods.rb', line 17

def respond_to?(*args)
  if args.first.to_s =~ /\Afind_by_([a-z0-9_]+(_and_[a-z0-9_]+)*)\Z/
    attributes = $1.split("_and_").map(&:to_sym)
    return true if (attributes & localized_attributes).size == attributes.size
  end
  
  super
end