Module: ActiveRecordTranslatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/version.rb,
lib/activerecord_translatable.rb,
lib/activerecord_translatable/extension.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =

:nodoc:

"0.1.0"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Define accessores



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/activerecord_translatable/extension.rb', line 45

def method_missing(method_name, *arguments, &block)
  translatable.each do |attribute|
    attribute = attribute.to_s
    if method_name.to_s =~ /^#{attribute}_(.{2})=$/
      return set_translation(attribute, arguments.first, $1.to_sym)
    elsif method_name.to_s =~ /^#{attribute}=$/
      return set_translation(attribute, arguments.first)
    elsif method_name.to_s =~ /^#{attribute}_(.{2})$/
      return translation(attribute, $1.to_sym)
    elsif method_name.to_s =~ /^#{attribute}$/
      return translation(attribute)
    end
  end
  super
end

Instance Method Details

#available_localesObject

Locales available for this model a locale is available if at least one attribute is present in given locale



38
39
40
# File 'lib/activerecord_translatable/extension.rb', line 38

def available_locales
  self.locales.map { |locale| locale.to_sym }
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/activerecord_translatable/extension.rb', line 61

def respond_to_missing?(method_name, include_private = false)
  translatable.each do |attribute|
    attribute = attribute.to_s
    if method_name.to_s =~ /^#{attribute}_(.{2})=$/
      return true
    elsif method_name.to_s =~ /^#{attribute}=$/
      return true
    elsif method_name.to_s =~ /^#{attribute}_(.{2})$/
      return true
    elsif method_name.to_s =~ /^#{attribute}$/
      return true
    end
  end
  false
end

#translatableObject

Translatable attributes for this model



30
31
32
# File 'lib/activerecord_translatable/extension.rb', line 30

def translatable
  self._translatable[base_name]
end