Module: Traco::ClassMethods

Defined in:
lib/traco/class_methods.rb

Constant Summary collapse

LOCALE_RE =
/[a-zA-Z]{2}(?:_[a-zA-Z]{2})?/

Instance Method Summary collapse

Instance Method Details

#human_attribute_name(attribute, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/traco/class_methods.rb', line 21

def human_attribute_name(attribute, options = {})
  default = super(attribute, options.merge(default: ""))

  if default.blank? && attribute.to_s.match(/\A(\w+?)_(#{LOCALE_RE})\z/)
    column, locale = $1, $2.to_sym
    if translates?(column)
      return "#{super(column, options)} (#{locale_name(locale)})"
    end
  end

  super
end

#locale_columns(*attributes) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/traco/class_methods.rb', line 13

def locale_columns(*attributes)
  attributes.inject([]) { |memo, attribute|
    memo += locales_for_attribute(attribute).map { |locale|
      :"#{attribute}_#{locale}"
    }
  }
end

#locales_for_attribute(attribute) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/traco/class_methods.rb', line 5

def locales_for_attribute(attribute)
  re = /\A#{attribute}_(#{LOCALE_RE})\z/

  column_names.
    grep(re) { $1.to_sym }.
    sort_by(&locale_sort_value)
end