Class: AzaharaSchema::AttributeName
- Inherits:
-
Object
- Object
- AzaharaSchema::AttributeName
- Defined in:
- lib/azahara_schema/attribute_name.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Instance Method Summary collapse
- #human(**options) ⇒ Object
-
#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.
- #i18n_fallback_keys ⇒ Object
- #i18n_key ⇒ Object
- #i18n_list_fallback_keys(value) ⇒ Object
- #i18n_list_fallback_prefixes ⇒ Object
- #i18n_scope ⇒ Object
- #i18n_scoped_key ⇒ Object
- #i18n_scoped_list_key(value, prefix = self.i18n_scoped_list_prefix) ⇒ Object
- #i18n_scoped_list_prefix ⇒ Object
-
#initialize(attribute) ⇒ AttributeName
constructor
A new instance of AttributeName.
- #model_i18n_key ⇒ Object
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
#attribute ⇒ Object (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(**) I18n.t(i18n_scoped_key, **(.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, **) return '' unless value.present? I18n.t(i18n_scoped_list_key(value.to_s), **.merge(default: i18n_list_fallback_keys(value)+[value.to_s])) end |
#i18n_fallback_keys ⇒ Object
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_key ⇒ Object
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_prefixes ⇒ Object
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_scope ⇒ Object
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_key ⇒ Object
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_prefix ⇒ Object
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_key ⇒ Object
21 22 23 |
# File 'lib/azahara_schema/attribute_name.rb', line 21 def model_i18n_key attribute.model.model_name.i18n_key end |