Module: Gaigo::Naming::HumanAttributeName::ClassMethods

Defined in:
lib/gaigo/models/naming/human_attribute_name.rb

Instance Method Summary collapse

Instance Method Details

#try_human_attribute_name(attribute, options = {}, &block) ⇒ Object



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
# File 'lib/gaigo/models/naming/human_attribute_name.rb', line 12

def try_human_attribute_name(attribute, options = {}, &block)
  options   = { :count => 1 }.merge!(options)
  parts     = attribute.to_s.split(".")
  attribute = parts.pop
  namespace = parts.join("/") unless parts.empty?
  attributes_scope = "#{self.i18n_scope}.attributes"

  if namespace
    defaults = lookup_ancestors.map do |klass|
      :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
    end
    defaults << :"#{attributes_scope}.#{namespace}.#{attribute}"
  else
    defaults = lookup_ancestors.map do |klass|
      :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}"
    end
  end

  if block_given?   #
    yield defaults  #
  end               #

  defaults << :"attributes.#{attribute}"
  defaults << options.delete(:default) if options[:default]

  options[:default] = defaults

  I18n.try_translate(defaults.shift, options) do |args, options|
    options[:default] << attribute.humanize
  end
end