Module: SingleTableGlobalize3::ActiveRecord::WithTranslations

Included in:
ClassMethods
Defined in:
lib/single_table_globalize3/active_record/with_translations.rb

Instance Method Summary collapse

Instance Method Details

#required_attributesObject



29
30
31
# File 'lib/single_table_globalize3/active_record/with_translations.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/single_table_globalize3/active_record/with_translations.rb', line 33

def required_translated_attributes
  translated_attribute_names & required_attributes
end

#translated?(name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 25

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

#translated_column_name(name, index = nil) ⇒ Object



45
46
47
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 45

def translated_column_name(name, index = nil)
  "#{translations_table_name(index)}.#{name}"
end

#translation_classObject



37
38
39
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 37

def translation_class
  SingleTableGlobalize3::ActiveRecord::Translation
end

#translations_table_name(index = nil) ⇒ Object



41
42
43
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 41

def translations_table_name(index = nil)
  "#{translation_class.table_name}#{index}"
end

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



16
17
18
19
20
21
22
23
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 16

def with_translated_attribute(name, value, locales = nil)
  locales ||= SingleTableGlobalize3.fallbacks
  self.join_index = self.join_index + 1
  with_translations(locales, self.join_index).where(
    translated_column_name('attribute_name', self.join_index) => name.to_s,
    translated_column_name('value', self.join_index) => Array(value).map(&:to_s)
  )
end

#with_translations(*locales) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/single_table_globalize3/active_record/with_translations.rb', line 6

def with_translations(*locales)
  index = locales.pop if locales.last.is_a?(Fixnum)
  locales = locales.concat(SingleTableGlobalize3.fallbacks).flatten.map(&:to_s)
  alias_table_name = translations_table_name(index)

  joins("LEFT OUTER JOIN #{translation_class.table_name} #{alias_table_name} ON #{alias_table_name}.translatable_id = #{table_name}.id").
  select("distinct #{table_name}.*").
  where(translated_column_name('locale', index) => locales)
end