Module: SimpleModelTranslations::ClassMethods

Defined in:
lib/simple_model_translations/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#translated_column_name(name) ⇒ Object



26
27
28
# File 'lib/simple_model_translations/class_methods.rb', line 26

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

#translation_classObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_model_translations/class_methods.rb', line 10

def translation_class
  @translation_class ||=
    begin
      Object.const_get(translation_class_name)
    rescue NameError => e
      klass = Object.const_set(translation_class_name, Class.new(ActiveRecord::Base))
      klass.translation_for(name.underscore.to_sym)
      klass.set_table_name(translation_class_name.to_s.underscore.pluralize)
      klass
    end
end

#translation_class_nameObject



22
23
24
# File 'lib/simple_model_translations/class_methods.rb', line 22

def translation_class_name
  @translation_class_name ||= (translation_options[:class_name] || "#{self.name}Translation").to_sym
end

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



30
31
32
33
34
35
# File 'lib/simple_model_translations/class_methods.rb', line 30

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

#with_translations(*args) ⇒ Object Also known as: with_translation



3
4
5
6
7
# File 'lib/simple_model_translations/class_methods.rb', line 3

def with_translations(*args)
  current_scope = joins(:translations)
  current_scope = current_scope.where("#{translated_column_name(:locale)} IN (?)", args) unless args.empty?
  current_scope
end