Module: Utf8Translatable::Base::ClassMethods

Defined in:
lib/utf8_translatable/base.rb

Overview

Defines Methods to extend ActiveRecord::Base

Instance Method Summary collapse

Instance Method Details

#ensures_translated_and_utf8Object

Defines ‘_#column_name’ prefixed methods for each column of the table that returns the corresponding attribute value translated in the correct locale and utf8 safe encoded

  • Warning Documentation to be detailed

  • NB: Is defined in lib/model_helpers/ut8_translatable.rb, which is included in initializers/active_record_extensions.rb

  • Examples

    1. Composer has columns firstname and lastname.

    Calling @composer._firstname will return the firstname ut8 encoded

    1. Work has columns title_en and title_fr

    Calling @work._title will return @work.title_#I18n.locale utf8 encoded



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
74
75
76
77
78
# File 'lib/utf8_translatable/base.rb', line 34

def ensures_translated_and_utf8
  if ActiveRecord::Base.connection.table_exists? table_name
    columns.map{|c| {:name => c.name.to_s, :type => c.type.to_s}}.each do |col|
      col_type, col_name = col[:type].try(:to_s), col[:name].try(:to_s)

      if col_type == 'string' || col_type == 'text'
        if is_translatable_attr?(col_type, col_name)
          _attr = col_name.gsub("_#{I18n.default_locale}", '')

          define_method _universal_method(col_name) do
            if send("#{_attr}_#{I18n.locale}").nil? then nil else send("#{_attr}_#{I18n.locale}").force_encoding(Encoding::UTF_8) end
          end

        else
          # ensure encoded in utf-8
          _attr = col_name
          _utf8_method = "_#{col_name}".to_sym

          define_method _utf8_method do
            if send(_attr).nil? then nil else send(_attr).force_encoding(Encoding::UTF_8) end
          end

        end
      end
    end
  end

  define_singleton_method :is_utf8_translatable? do
    true
  end

  # if a model calls a _name method (assuming the model is utf8_translatable)
  # then running the migrations (before the name_en and name_fr actually exist)
  # won't work until the columns name_en and name_fr have actually been created
  # this method should prevent this
  #
  def utf8_translatable_alias(arg1, arg2)
    if ActiveRecord::Base.connection.table_exists? table_name
      send(:alias_method, arg1, arg2)
    else
      nil
    end    
  end

end

#is_utf8_translatable?Boolean

default state

Returns:

  • (Boolean)


13
14
15
# File 'lib/utf8_translatable/base.rb', line 13

def is_utf8_translatable?
  false
end

#utf8_translatable_alias(arg1, arg2) ⇒ Object

if a model calls a _name method (assuming the model is utf8_translatable) then running the migrations (before the name_en and name_fr actually exist) won’t work until the columns name_en and name_fr have actually been created this method should prevent this



70
71
72
73
74
75
76
# File 'lib/utf8_translatable/base.rb', line 70

def utf8_translatable_alias(arg1, arg2)
  if ActiveRecord::Base.connection.table_exists? table_name
    send(:alias_method, arg1, arg2)
  else
    nil
  end    
end