Class: AzaharaSchema::AttributeName

Inherits:
Object
  • Object
show all
Defined in:
lib/azahara_schema/attribute_name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AttributeName

Returns a new instance of AttributeName.



6
7
8
# File 'lib/azahara_schema/attribute_name.rb', line 6

def initialize(attribute)
  @attribute = attribute
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



4
5
6
# File 'lib/azahara_schema/attribute_name.rb', line 4

def attribute
  @attribute
end

Instance Method Details

#human(**options) ⇒ Object



10
11
12
# File 'lib/azahara_schema/attribute_name.rb', line 10

def human(**options)
  I18n.t(i18n_scoped_key, **(options.merge(default: i18n_fallback_keys)))
end

#human_list_value(value, **options) ⇒ Object

translates values to list_values TODO: not needed to do it for every value - for example districts are not translatable



16
17
18
19
# File 'lib/azahara_schema/attribute_name.rb', line 16

def human_list_value(value, **options)
  return '' unless value.present?
  I18n.t(i18n_scoped_list_key(value.to_s), **options.merge(default: i18n_list_fallback_keys(value)+[value.to_s]))
end

#i18n_fallback_keysObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/azahara_schema/attribute_name.rb', line 45

def i18n_fallback_keys
  if attribute.respond_to?(:attribute)
    parent_attr_name = attribute.attribute.attribute_name
    keys = [ parent_attr_name.i18n_scoped_key.to_sym ]
    keys.concat( parent_attr_name.i18n_fallback_keys )
    keys
  else
    []
  end
end

#i18n_keyObject



25
26
27
# File 'lib/azahara_schema/attribute_name.rb', line 25

def i18n_key
  attribute.name
end

#i18n_list_fallback_keys(value) ⇒ Object



67
68
69
# File 'lib/azahara_schema/attribute_name.rb', line 67

def i18n_list_fallback_keys(value)
  i18n_list_fallback_prefixes.collect{|pref| i18n_scoped_list_key(value, pref) }
end

#i18n_list_fallback_prefixesObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/azahara_schema/attribute_name.rb', line 56

def i18n_list_fallback_prefixes
  if attribute.respond_to?(:attribute)
    parent_attr_name = attribute.attribute.attribute_name
    prefixes = [ parent_attr_name.i18n_scoped_list_prefix ]
    prefixes.concat( parent_attr_name.i18n_list_fallback_prefixes )
    prefixes
  else
    []
  end
end

#i18n_scopeObject



29
30
31
# File 'lib/azahara_schema/attribute_name.rb', line 29

def i18n_scope
  'activerecord.attributes.' + model_i18n_key.to_s
end

#i18n_scoped_keyObject



33
34
35
# File 'lib/azahara_schema/attribute_name.rb', line 33

def i18n_scoped_key
  (i18n_scope + '.' + i18n_key.to_s).to_sym
end

#i18n_scoped_list_key(value, prefix = self.i18n_scoped_list_prefix) ⇒ Object



41
42
43
# File 'lib/azahara_schema/attribute_name.rb', line 41

def i18n_scoped_list_key(value, prefix=self.i18n_scoped_list_prefix)
  (prefix + '.' + value.to_s).to_sym
end

#i18n_scoped_list_prefixObject



37
38
39
# File 'lib/azahara_schema/attribute_name.rb', line 37

def i18n_scoped_list_prefix
  i18n_scope + '.' + i18n_key.to_s.pluralize
end

#model_i18n_keyObject



21
22
23
# File 'lib/azahara_schema/attribute_name.rb', line 21

def model_i18n_key
  attribute.model.model_name.i18n_key
end