Module: SameTableTranslation

Defined in:
lib/same_table_translation.rb,
lib/same_table_translation/version.rb,
lib/generators/same_table_translation/translate_generator.rb

Defined Under Namespace

Classes: TranslateGenerator

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#translatable(*attributes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/same_table_translation.rb', line 4

def translatable(*attributes)
  locales = I18n.available_locales
  attributes.each do |attribute|
    locales.each do |locale|
      begin
        name = "#{attribute.to_s}_#{locale.to_s}"
        attr_accessible name.to_sym if self.attribute_names.include?(name)
      rescue
      end
    end
  end

  attributes.each do |attribute|
    define_method attribute.to_s do
      begin
        self.attributes["#{attribute.to_s}_#{I18n.locale.to_s}"]
      rescue
      end
    end

    define_method "#{attribute.to_s}=" do |value|
      begin
        self.attributes = { "#{attribute}_#{I18n.locale.to_s}" => value }
      rescue
      end
    end
  end

  scope :translated, lambda { |attribute|
    where(arel_table["#{attribute.to_s}_#{I18n.locale.to_s}".to_sym].not_eq(nil))
  }

  scope :untranslated, lambda { |attribute|
    where(arel_table["#{attribute.to_s}_#{I18n.locale.to_s}".to_sym].eq(nil))
  }
end