Module: Globalize::ActiveRecord::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#required_attributesObject



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

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

#required_translated_attributesObject



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

def required_translated_attributes
  translated_attribute_names & required_attributes
end

#translated?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#translated_column_name(name) ⇒ Object



56
57
58
# File 'lib/globalize/active_record/class_methods.rb', line 56

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

#translation_classObject



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

def translation_class
  @translation_class ||= begin
    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

    klass.belongs_to :globalized_model, :class_name => self.name, :foreign_key => translation_options[:foreign_key], :touch => true
    klass
  end
end

#translations_table_nameObject



52
53
54
# File 'lib/globalize/active_record/class_methods.rb', line 52

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)
  all.merge 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(all) do |scope, name|
    scope.where("#{translated_column_name(name)} IS NOT NULL")
  end
end

#with_translated_attribute(name, value, locales = Globalize.fallbacks) ⇒ Object



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

def with_translated_attribute(name, value, 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?
  preload(:translations).joins(:translations).readonly(false).with_locales(locales).with_required_attributes
end