Module: Globalize::ActiveRecord::ClassMethods

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/globalize/active_record/class_methods.rb', line 58

def method_missing(method, *args)
  if method.to_s =~ /^find_(first_|)by_(\w+)$/ && translated_attribute_names.include?($2.to_sym)
    result = with_translated_attribute($2, args.first)
    $1 == 'first_' ? result.first : result
  else
    super
  end
end

Instance Method Details

#required_attributesObject



29
30
31
# File 'lib/globalize/active_record/class_methods.rb', line 29

def required_attributes
  validators.map { |v| v.attributes if v.is_a?(ActiveModel::Validations::PresenceValidator) }.flatten
end

#required_translated_attributesObject



33
34
35
# File 'lib/globalize/active_record/class_methods.rb', line 33

def required_translated_attributes
  translated_attribute_names & required_attributes
end

#respond_to?(method, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/globalize/active_record/class_methods.rb', line 54

def respond_to?(method, *args, &block)
  method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym) || super
end

#translated_column_name(name) ⇒ Object



50
51
52
# File 'lib/globalize/active_record/class_methods.rb', line 50

def translated_column_name(name)
  "#{translation_class.table_name}.#{name}"
end

#translation_classObject



37
38
39
40
41
42
43
44
# File 'lib/globalize/active_record/class_methods.rb', line 37

def translation_class
  klass = const_get(:Translation) rescue const_set(:Translation, Class.new(Translation))
  if klass.table_name == 'translations'
    klass.set_table_name(translation_options[:table_name])
    klass.belongs_to name.underscore.gsub('/', '_')
  end
  klass
end

#translations_table_nameObject



46
47
48
# File 'lib/globalize/active_record/class_methods.rb', line 46

def translations_table_name
  translation_class.table_name
end

#with_locales(*locales) ⇒ Object



6
7
8
# File 'lib/globalize/active_record/class_methods.rb', line 6

def with_locales(*locales)
  scoped & translation_class.with_locales(*locales)
end

#with_required_attributesObject



15
16
17
18
19
# File 'lib/globalize/active_record/class_methods.rb', line 15

def with_required_attributes
  required_translated_attributes.inject(scoped) do |scope, name|
    scope.where("#{translated_column_name(name)} IS NOT NULL")
  end
end

#with_translated_attribute(name, value, locales = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/globalize/active_record/class_methods.rb', line 21

def with_translated_attribute(name, value, locales = nil)
  locales ||= Globalize.fallbacks
  with_translations.where(
    translated_column_name(name)    => value,
    translated_column_name(:locale) => locales.map(&:to_s)
  )
end

#with_translations(*locales) ⇒ Object



10
11
12
13
# File 'lib/globalize/active_record/class_methods.rb', line 10

def with_translations(*locales)
  locales = translated_locales if locales.empty?
  includes(:translations).with_locales(locales).with_required_attributes
end