Method: SingleTableGlobalize3::ActiveRecord::ClassMethods#method_missing

Defined in:
lib/single_table_globalize3/active_record/class_methods.rb

#method_missing(method_id, *arguments, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 30

def method_missing(method_id, *arguments, &block)
  match, attribute_names, translated_attributes, untranslated_attributes = supported_on_missing?(method_id)
  return super unless match

  scope = scoped

  translated_attributes.each do |attr|
    scope = scope.with_translated_attribute(attr, arguments[attribute_names.index(attr)])
  end

  untranslated_attributes.each do |unt|
    index = attribute_names.index(unt)
    raise StandarError unless index
    scope = scope.send(:"scoped_by_#{unt}", arguments[index])
  end

  if defined?(::ActiveRecord::DynamicFinderMatch) && match.is_a?(::ActiveRecord::DynamicFinderMatch)
    if match.instantiator? and scope.blank?
      return scope.find_or_instantiator_by_attributes match, attribute_names, *arguments, &block
    end
    match_finder_method = match.finder.to_s
    match_finder_method << "!" if match.bang? && ::ActiveRecord::VERSION::STRING >= "3.1.0"
    return scope.send(match_finder_method).tap do |found|
      found.is_a?(Array) ? found.map { |f| f.translations.reload } : found.translations.reload unless found.nil?
    end
  end
  return scope
end