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

Instance Method Details

#label_of(field_name, prefix = '') ⇒ String Also known as: prefix_label_of

Returns label for the given field.

Parameters:

  • field_name (String, Symbol)

    field name

  • prefix (String) (defaults to: '')

Returns:

  • (String)

    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.

Parameters:

  • field_name (String, Symbol)

    field name

  • prefix (String) (defaults to: '')

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    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.

Parameters:

  • prefix (String)

Returns:

  • (Array<String>)

    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

Parameters:

  • field_names (Array<String>)
  • prefix (String)

Returns:

  • (Array<String>)

    a list of field names



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.

Parameters:

  • prefix (String)

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    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

Parameters:

  • fields (Hash)

    fields metadata

  • prefix (String)

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



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.

Parameters:

  • field_name (String, Symbol)

    field name

  • prefix (String) (defaults to: '')

Returns:

  • (String, Symbol)

    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