Module: MatViewsI18n
- Extended by:
- ActiveSupport::Concern
- Included in:
- MatViews::ApplicationRecord
- Defined in:
- app/models/concerns/mat_views_i18n.rb
Overview
Methods are added as **class methods** to the including model.
MatViewsI18n
Concern that adds convenient class-level helpers for model I18n:
-
Humanized attribute names
-
Humanized enum values
-
Select-friendly enum option arrays
-
Placeholders and hints for forms
These helpers rely on Rails’ standard i18n model keys using the model’s ‘model_name.i18n_key` (e.g. `MatViews::MatViewDefinition` → `mat_views/mat_view_definition`).
## Expected i18n structure (examples)
“‘yml en-US:
activerecord:
attributes:
mat_views/mat_view_definition:
name: "View name"
sql: "SQL"
enums:
mat_views/mat_view_definition:
refresh_strategy:
regular: "Regular"
concurrent: "Concurrent"
swap: "Swap"
placeholders:
mat_views/mat_view_definition:
name: "e.g. monthly_sales_mv"
hints:
mat_views/mat_view_definition:
sql: "Use a SELECT statement; no trailing semicolon."
“‘
## Usage “‘ruby MatViews::MatViewDefinition.human_name(:name) # => “View name” MatViews::MatViewDefinition.human_enum_name(:refresh_strategy, :regular) # => “Regular” MatViews::MatViewDefinition.human_enum_options(:refresh_strategy) # => [[“Regular”,“regular”], [“Concurrent”,“concurrent”], [“Swap”,“swap”]] MatViews::MatViewDefinition.placeholder_for(:name) # => “e.g. monthly_sales_mv” MatViews::MatViewDefinition.hint_for(:sql) # => “Use a SELECT statement…” “`
Class Method Summary collapse
-
.hint_for(attribute) ⇒ String
Form hint/help text for the given attribute.
-
.human_enum_name(enum_name, enum_value) ⇒ String
Humanized (translated) enum value label.
-
.human_enum_options(enum_name) ⇒ Array<Array(String, String)>
Options array suitable for Rails ‘options_for_select`.
-
.human_name(attribute) ⇒ String
Humanized (translated) attribute label for this model.
-
.placeholder_for(attribute) ⇒ String
Form placeholder for the given attribute.
Class Method Details
.hint_for(attribute) ⇒ String
Form hint/help text for the given attribute. Returns empty string if not defined.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/concerns/mat_views_i18n.rb', line 85 module MatViewsI18n extend ActiveSupport::Concern class_methods do # @return [String] def human_name(attribute) I18n.t( "activerecord.attributes.#{model_name.i18n_key}.#{attribute}", default: attribute.to_s.humanize ) end # human_enum_name(:refresh_strategy, :regular) → "Regular" # # @param enum_name [Symbol, String] # @param enum_value [Symbol, String, Integer] # @return [String] def human_enum_name(enum_name, enum_value) key = enum_value.to_s I18n.t( "activerecord.enums.#{model_name.i18n_key}.#{enum_name}.#{key}", default: key.humanize ) end # human_enum_options(:refresh_strategy) # → [["Regular","regular"], ["Concurrent","concurrent"], ["Swap","swap"]] # # @param enum_name [Symbol, String] # @return [Array<Array(String, String)>] def (enum_name) public_send(enum_name.to_s.pluralize).keys.map do |val| [human_enum_name(enum_name, val), val] end end # @param attribute [Symbol, String] # @return [String] def placeholder_for(attribute) I18n.t( "activerecord.placeholders.#{model_name.i18n_key}.#{attribute}", default: '' ) end # @param attribute [Symbol, String] # @return [String] def hint_for(attribute) I18n.t( "activerecord.hints.#{model_name.i18n_key}.#{attribute}", default: '' ) end end end |
.human_enum_name(enum_name, enum_value) ⇒ String
Humanized (translated) enum value label. Falls back to ‘enum_value.to_s.humanize` when missing.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/concerns/mat_views_i18n.rb', line 85 module MatViewsI18n extend ActiveSupport::Concern class_methods do # @return [String] def human_name(attribute) I18n.t( "activerecord.attributes.#{model_name.i18n_key}.#{attribute}", default: attribute.to_s.humanize ) end # human_enum_name(:refresh_strategy, :regular) → "Regular" # # @param enum_name [Symbol, String] # @param enum_value [Symbol, String, Integer] # @return [String] def human_enum_name(enum_name, enum_value) key = enum_value.to_s I18n.t( "activerecord.enums.#{model_name.i18n_key}.#{enum_name}.#{key}", default: key.humanize ) end # human_enum_options(:refresh_strategy) # → [["Regular","regular"], ["Concurrent","concurrent"], ["Swap","swap"]] # # @param enum_name [Symbol, String] # @return [Array<Array(String, String)>] def (enum_name) public_send(enum_name.to_s.pluralize).keys.map do |val| [human_enum_name(enum_name, val), val] end end # @param attribute [Symbol, String] # @return [String] def placeholder_for(attribute) I18n.t( "activerecord.placeholders.#{model_name.i18n_key}.#{attribute}", default: '' ) end # @param attribute [Symbol, String] # @return [String] def hint_for(attribute) I18n.t( "activerecord.hints.#{model_name.i18n_key}.#{attribute}", default: '' ) end end end |
.human_enum_options(enum_name) ⇒ Array<Array(String, String)>
Options array suitable for Rails ‘options_for_select`.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/concerns/mat_views_i18n.rb', line 85 module MatViewsI18n extend ActiveSupport::Concern class_methods do # @return [String] def human_name(attribute) I18n.t( "activerecord.attributes.#{model_name.i18n_key}.#{attribute}", default: attribute.to_s.humanize ) end # human_enum_name(:refresh_strategy, :regular) → "Regular" # # @param enum_name [Symbol, String] # @param enum_value [Symbol, String, Integer] # @return [String] def human_enum_name(enum_name, enum_value) key = enum_value.to_s I18n.t( "activerecord.enums.#{model_name.i18n_key}.#{enum_name}.#{key}", default: key.humanize ) end # human_enum_options(:refresh_strategy) # → [["Regular","regular"], ["Concurrent","concurrent"], ["Swap","swap"]] # # @param enum_name [Symbol, String] # @return [Array<Array(String, String)>] def (enum_name) public_send(enum_name.to_s.pluralize).keys.map do |val| [human_enum_name(enum_name, val), val] end end # @param attribute [Symbol, String] # @return [String] def placeholder_for(attribute) I18n.t( "activerecord.placeholders.#{model_name.i18n_key}.#{attribute}", default: '' ) end # @param attribute [Symbol, String] # @return [String] def hint_for(attribute) I18n.t( "activerecord.hints.#{model_name.i18n_key}.#{attribute}", default: '' ) end end end |
.human_name(attribute) ⇒ String
Humanized (translated) attribute label for this model. Falls back to ‘attribute.to_s.humanize` when missing.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/concerns/mat_views_i18n.rb', line 85 module MatViewsI18n extend ActiveSupport::Concern class_methods do # @return [String] def human_name(attribute) I18n.t( "activerecord.attributes.#{model_name.i18n_key}.#{attribute}", default: attribute.to_s.humanize ) end # human_enum_name(:refresh_strategy, :regular) → "Regular" # # @param enum_name [Symbol, String] # @param enum_value [Symbol, String, Integer] # @return [String] def human_enum_name(enum_name, enum_value) key = enum_value.to_s I18n.t( "activerecord.enums.#{model_name.i18n_key}.#{enum_name}.#{key}", default: key.humanize ) end # human_enum_options(:refresh_strategy) # → [["Regular","regular"], ["Concurrent","concurrent"], ["Swap","swap"]] # # @param enum_name [Symbol, String] # @return [Array<Array(String, String)>] def (enum_name) public_send(enum_name.to_s.pluralize).keys.map do |val| [human_enum_name(enum_name, val), val] end end # @param attribute [Symbol, String] # @return [String] def placeholder_for(attribute) I18n.t( "activerecord.placeholders.#{model_name.i18n_key}.#{attribute}", default: '' ) end # @param attribute [Symbol, String] # @return [String] def hint_for(attribute) I18n.t( "activerecord.hints.#{model_name.i18n_key}.#{attribute}", default: '' ) end end end |
.placeholder_for(attribute) ⇒ String
Form placeholder for the given attribute. Returns empty string if not defined.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/concerns/mat_views_i18n.rb', line 85 module MatViewsI18n extend ActiveSupport::Concern class_methods do # @return [String] def human_name(attribute) I18n.t( "activerecord.attributes.#{model_name.i18n_key}.#{attribute}", default: attribute.to_s.humanize ) end # human_enum_name(:refresh_strategy, :regular) → "Regular" # # @param enum_name [Symbol, String] # @param enum_value [Symbol, String, Integer] # @return [String] def human_enum_name(enum_name, enum_value) key = enum_value.to_s I18n.t( "activerecord.enums.#{model_name.i18n_key}.#{enum_name}.#{key}", default: key.humanize ) end # human_enum_options(:refresh_strategy) # → [["Regular","regular"], ["Concurrent","concurrent"], ["Swap","swap"]] # # @param enum_name [Symbol, String] # @return [Array<Array(String, String)>] def (enum_name) public_send(enum_name.to_s.pluralize).keys.map do |val| [human_enum_name(enum_name, val), val] end end # @param attribute [Symbol, String] # @return [String] def placeholder_for(attribute) I18n.t( "activerecord.placeholders.#{model_name.i18n_key}.#{attribute}", default: '' ) end # @param attribute [Symbol, String] # @return [String] def hint_for(attribute) I18n.t( "activerecord.hints.#{model_name.i18n_key}.#{attribute}", default: '' ) end end end |