Module: Transilator::ActiveRecordExtensions::ClassMethods

Defined in:
lib/transilator/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#transilator(*args) ⇒ Object



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/transilator/active_record_extensions.rb', line 6

def transilator(*args)

  args.each do |attribute|

    # read the attribute from the hstore attribute and return it.
    #
    # returns the attribute or an empty string if the attribute is nil
    define_method attribute do
      locale = I18n.locale.to_s
      attribute_value_hash = (self[attribute] || {})
      value = (attribute_value_hash)[locale] || ""
      # we have a value, so let's abort.
      return value unless value.empty?
      # now implement our fallback mechanism
      fallbacks_for_locale  = Transilator.config.get_fallbacks_for_locale(locale)
      return value if fallbacks_for_locale.empty?
      # now do the magic for figuring out the fallback
      fallback_locale_to_use = fallbacks_for_locale.find {|f| (attribute_value_hash)[f] }
      # no fallback that can be used found.
      return "" unless fallback_locale_to_use
      # return our value or an empty string
      attribute_value_hash[fallback_locale_to_use] || ""
    end

    # set the attribute on the model on the hstore hash
    #
    # returns the new value
    define_method "#{attribute}=" do |value|
      if value.is_a?(Hash)
        write_attribute attribute, value
      elsif self[attribute].present?
        write_attribute attribute, self[attribute].merge({I18n.locale => value })
        value
      else
        write_attribute attribute, {I18n.locale => value }
        value
      end
    end

    # returns the raw hstore directly
    define_method "#{attribute}_before_type_cast" do
      self[attribute] || {}
    end

    module_eval do
      serialize "#{attribute}", ActiveRecord::Coders::Hstore
    end if defined?(ActiveRecord::Coders::Hstore)


    # for each locale add a getter and setter method in the type of
    # title_de or title_en
    I18n.available_locales.map(&:to_s).each do |locale|

      define_method "#{attribute}_#{locale}" do
        (self[attribute] || {})[locale] || ""
      end

      define_method "#{attribute}_#{locale}=" do |value|
        if self[attribute].present?
          write_attribute attribute, self[attribute].merge({locale => value })
        else
          write_attribute attribute, {locale => value }
        end
      end

    end
  end
end