Module: Wallaby::Fieldable
- Included in:
- ModelDecorator
- Defined in:
- lib/concerns/wallaby/fieldable.rb
Overview
Field helper method collection that takes care of ‘fields`, `field_names` related methods for model decorator
Instance Method Summary collapse
-
#label_of(field_name, prefix = '') ⇒ String
(also: #prefix_label_of)
Label for the given field.
-
#metadata_of(field_name, prefix = '') ⇒ ActiveSupport::HashWithIndifferentAccess
(also: #prefix_metadata_of)
Metadata.
-
#prefix_field_names(prefix) ⇒ Array<String>
A list of field names.
-
#prefix_field_names=(field_names, prefix) ⇒ Array<String>
Set field names for the given prefix.
-
#prefix_fields(prefix) ⇒ ActiveSupport::HashWithIndifferentAccess
Metadata.
-
#prefix_fields=(fields, prefix) ⇒ ActiveSupport::HashWithIndifferentAccess
Set metadata for the given prefix.
-
#type_of(field_name, prefix = '') ⇒ String, Symbol
(also: #prefix_type_of)
Type for the given field.
Instance Method Details
#label_of(field_name, prefix = '') ⇒ String Also known as: prefix_label_of
Returns label for the given field.
18 19 20 |
# File 'lib/concerns/wallaby/fieldable.rb', line 18 def label_of(field_name, prefix = '') (field_name, prefix)[:label] || field_name.to_s.humanize end |
#metadata_of(field_name, prefix = '') ⇒ ActiveSupport::HashWithIndifferentAccess Also known as: prefix_metadata_of
Returns metadata.
10 11 12 |
# File 'lib/concerns/wallaby/fieldable.rb', line 10 def (field_name, prefix = '') prefix_fields(prefix)[field_name] || {} end |
#prefix_field_names(prefix) ⇒ Array<String>
Returns a list of field names.
49 50 51 52 53 54 55 56 |
# File 'lib/concerns/wallaby/fieldable.rb', line 49 def prefix_field_names(prefix) variable = "@#{prefix}field_names" instance_variable_get(variable) || \ instance_variable_set(variable, begin fields = prefix_fields(prefix).reject { |_k, | [:hidden] } reposition(fields.keys, primary_key) end) end |
#prefix_field_names=(field_names, prefix) ⇒ Array<String>
Set field names for the given prefix
62 63 64 65 |
# File 'lib/concerns/wallaby/fieldable.rb', line 62 def prefix_field_names=(field_names, prefix) variable = "@#{prefix}field_names" instance_variable_set(variable, Array.wrap(field_names).flatten) end |
#prefix_fields(prefix) ⇒ ActiveSupport::HashWithIndifferentAccess
Returns metadata.
33 34 35 36 |
# File 'lib/concerns/wallaby/fieldable.rb', line 33 def prefix_fields(prefix) variable = "@#{prefix}fields" instance_variable_get(variable) || instance_variable_set(variable, Utils.clone(fields)) end |
#prefix_fields=(fields, prefix) ⇒ ActiveSupport::HashWithIndifferentAccess
Set metadata for the given prefix
42 43 44 45 |
# File 'lib/concerns/wallaby/fieldable.rb', line 42 def prefix_fields=(fields, prefix) variable = "@#{prefix}fields" instance_variable_set(variable, FieldsRegulator.new(fields).execute) end |
#type_of(field_name, prefix = '') ⇒ String, Symbol Also known as: prefix_type_of
Returns type for the given field.
26 27 28 |
# File 'lib/concerns/wallaby/fieldable.rb', line 26 def type_of(field_name, prefix = '') ensure_type_is_present field_name, (field_name, prefix)[:type], prefix end |