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



66
67
68
69
70
71
72
73
# File 'lib/globalize/active_record/class_methods.rb', line 66

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

Instance Method Details

#required_attributesObject



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

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

#required_translated_attributesObject



37
38
39
# File 'lib/globalize/active_record/class_methods.rb', line 37

def required_translated_attributes
  translated_attribute_names & required_attributes
end

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/globalize/active_record/class_methods.rb', line 62

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

#translated?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def translated?(name)
  translated_attribute_names.include?(name.to_sym)
end

#translated_column_name(name) ⇒ Object



58
59
60
# File 'lib/globalize/active_record/class_methods.rb', line 58

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

#translation_classObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/globalize/active_record/class_methods.rb', line 41

def translation_class
  klass = self.const_get(:Translation) rescue nil
  if(klass.nil? || (klass.class_name != (self.class_name + "Translation")))
    klass = self.const_set(:Translation, Class.new(Globalize::ActiveRecord::Translation))
  end
  
  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



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

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) => Array(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