Class: Wallaby::Her::ModelDecorator

Inherits:
ModelDecorator
  • Object
show all
Defined in:
lib/adapters/wallaby/her/model_decorator.rb

Overview

Modal decorator for Her

Constant Summary collapse

ATTRIBUTE_SUFFIX =

Generally, in Her, attribute methods are generated with this suffix

'_will_change!'

Instance Method Summary collapse

Instance Method Details

#fieldsActiveSupport::HashWithIndifferentAccess

Origin metadata directly coming from Her.

It needs to be frozen so that we can keep the metadata integrity

Examples:

sample fields:

model_decorator.fields
# =>
{
  # general field
  id: { type: 'integer', label: 'Id' },
  # association field
  category: {
    'type' => 'belongs_to',
    'label' => 'Category',
    'is_association' => true
  }
}

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



27
28
29
30
31
32
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 27

def fields
  @fields ||= ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash|
    hash.merge! general_fields
    hash.merge! association_fields
  end.freeze
end

#form_active_errors(resource) ⇒ ActiveModel::Errors, Hash

Returns errors for resource.

Returns:

  • (ActiveModel::Errors, Hash)

    errors for resource

See Also:



70
71
72
73
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 70

def form_active_errors(resource)
  resource.errors.presence \
    || resource.instance_variable_get(:@response_errors)
end

#form_field_namesArray<String>

Returns a list of field names for form (new/edit) page.

Returns:

  • (Array<String>)

    a list of field names for form (new/edit) page



61
62
63
64
65
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 61

def form_field_names
  @form_field_names ||=
    Utils.clone(index_field_names)
         .delete_if { |field_name| field_name == primary_key.to_s }
end

#form_fieldsActiveSupport::HashWithIndifferentAccess

A copy of #fields for form (new/edit) page

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



48
49
50
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 48

def form_fields
  @form_fields ||= Utils.clone fields
end

#guess_title(resource) ⇒ String

To guess the title for resource.

It will go through the fields and try to find out the one that looks like a name or text to represent this resource. Otherwise, it will fall back to primary key.

Parameters:

  • resource (Object)

Returns:

  • (String)

    the title of given resource



87
88
89
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 87

def guess_title(resource)
  ModuleUtils.try_to resource, possible_title_field
end

#index_field_namesArray<String>

Returns a list of field names for index page.

Returns:

  • (Array<String>)

    a list of field names for index page



53
54
55
56
57
58
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 53

def index_field_names
  @index_field_names ||=
    index_fields.reject do |_field_name, |
      [:is_association]
    end.keys
end

#index_fieldsActiveSupport::HashWithIndifferentAccess

A copy of #fields for index page

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



36
37
38
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 36

def index_fields
  @index_fields ||= Utils.clone fields
end

#primary_keyString

Returns primary key for the resource.

Returns:

  • (String)

    primary key for the resource



76
77
78
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 76

def primary_key
  @primary_key ||= @model_class.primary_key
end

#show_fieldsActiveSupport::HashWithIndifferentAccess

A copy of #fields for show page

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



42
43
44
# File 'lib/adapters/wallaby/her/model_decorator.rb', line 42

def show_fields
  @show_fields ||= Utils.clone fields
end