Class: Wallaby::ModelDecorator

Inherits:
Object
  • Object
show all
Includes:
FieldHelpers
Defined in:
lib/interfaces/wallaby/model_decorator.rb,
lib/interfaces/wallaby/model_decorator/field_helpers.rb

Overview

Model Decorator interface, designed to maintain metadata for all the fields coming from data source (database/api)

Direct Known Subclasses

ActiveRecord::ModelDecorator

Defined Under Namespace

Modules: FieldHelpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldHelpers

#form_label_of, #form_metadata_of, #form_type_of, #index_label_of, #index_metadata_of, #index_type_of, #label_of, #metadata_of, #show_label_of, #show_metadata_of, #show_type_of, #type_of

Constructor Details

#initialize(model_class) ⇒ ModelDecorator

Initialize with model class

Parameters:

  • model_class (Class)

    model class



15
16
17
# File 'lib/interfaces/wallaby/model_decorator.rb', line 15

def initialize(model_class)
  @model_class = model_class
end

Instance Attribute Details

#field_namesArray<String>, Array<Symbol>

Returns a list of field names.

Returns:

  • (Array<String>, Array<Symbol>)

    a list of field names



31
32
33
# File 'lib/interfaces/wallaby/model_decorator.rb', line 31

def field_names
  @field_names ||= reposition fields.keys, primary_key
end

#form_field_namesArray<String>, Array<Symbol>

Returns a list field names displayed on form (new/edit) page.

Returns:

  • (Array<String>, Array<Symbol>)

    a list field names displayed on form (new/edit) page



82
83
84
# File 'lib/interfaces/wallaby/model_decorator.rb', line 82

def form_field_names
  @form_field_names ||= reposition form_fields.keys, primary_key
end

#index_field_namesArray<String>, Array<Symbol>

Returns a list of field names displayed on index page.

Returns:

  • (Array<String>, Array<Symbol>)

    a list of field names displayed on index page



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

def index_field_names
  @index_field_names ||= reposition index_fields.keys, primary_key
end

#model_classObject (readonly)

Returns the value of attribute model_class.



9
10
11
# File 'lib/interfaces/wallaby/model_decorator.rb', line 9

def model_class
  @model_class
end

#show_field_namesArray<String>, Array<Symbol>

Returns a list of field names displayed on show page.

Returns:

  • (Array<String>, Array<Symbol>)

    a list of field names displayed on show page



65
66
67
# File 'lib/interfaces/wallaby/model_decorator.rb', line 65

def show_field_names
  @show_field_names ||= reposition show_fields.keys, primary_key
end

Instance Method Details

#fieldsHash

Returns origin metadata for fields.

Returns:

  • (Hash)

    origin metadata for fields

Raises:



20
21
22
# File 'lib/interfaces/wallaby/model_decorator.rb', line 20

def fields
  raise NotImplemented
end

#fields=(fields) ⇒ Object

Set metadata as origin

Parameters:

  • fields (Hash)

    metadata



26
27
28
# File 'lib/interfaces/wallaby/model_decorator.rb', line 26

def fields=(fields)
  @fields = ::ActiveSupport::HashWithIndifferentAccess.new fields
end

#filtersHash

Returns metadata for custom filters used on index page.

Returns:

  • (Hash)

    metadata for custom filters used on index page



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

def filters
  @filters ||= ::ActiveSupport::HashWithIndifferentAccess.new
end

#form_active_errors(_resource) ⇒ Hash

Returns validation errors for current resource.

Returns:

  • (Hash)

    validation errors for current resource

Raises:



92
93
94
# File 'lib/interfaces/wallaby/model_decorator.rb', line 92

def form_active_errors(_resource)
  raise NotImplemented
end

#form_fieldsHash

Returns metadata for fields used on form (new/edit) page.

Returns:

  • (Hash)

    metadata for fields used on form (new/edit) page

Raises:



70
71
72
# File 'lib/interfaces/wallaby/model_decorator.rb', line 70

def form_fields
  raise NotImplemented
end

#form_fields=(fields) ⇒ Object

Set metadata for form (new/edit) page

Parameters:

  • fields (Hash)

    metadata



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

def form_fields=(fields)
  @form_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields
end

#guess_title(_resource) ⇒ String

To guess the title for a resource.

This title will be used on the following places:

  • page title on show page

  • in the response for autocomplete association field

  • title of show link for a resource

Parameters:

  • _resource (Object)

    resource

Returns:

  • (String)

    title of current resource

Raises:



109
110
111
# File 'lib/interfaces/wallaby/model_decorator.rb', line 109

def guess_title(_resource)
  raise NotImplemented
end

#index_fieldsHash

Returns metadata for fields used on index page.

Returns:

  • (Hash)

    metadata for fields used on index page

Raises:



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

def index_fields
  raise NotImplemented
end

#index_fields=(fields) ⇒ Object

Set metadata for index page

Parameters:

  • fields (Hash)

    metadata



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

def index_fields=(fields)
  @index_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields
end

#primary_keyString

Returns primary key.

Returns:

  • (String)

    primary key

Raises:



97
98
99
# File 'lib/interfaces/wallaby/model_decorator.rb', line 97

def primary_key
  raise NotImplemented
end

#resources_nameString

Returns resources name coverted from model class.

Returns:

  • (String)

    resources name coverted from model class



114
115
116
# File 'lib/interfaces/wallaby/model_decorator.rb', line 114

def resources_name
  Map.resources_name_map @model_class
end

#show_fieldsHash

Returns metadata for fields used on show page.

Returns:

  • (Hash)

    metadata for fields used on show page

Raises:



53
54
55
# File 'lib/interfaces/wallaby/model_decorator.rb', line 53

def show_fields
  raise NotImplemented
end

#show_fields=(fields) ⇒ Object

Set metadata for show page

Parameters:

  • fields (Hash)

    metadata



59
60
61
# File 'lib/interfaces/wallaby/model_decorator.rb', line 59

def show_fields=(fields)
  @show_fields = ::ActiveSupport::HashWithIndifferentAccess.new fields
end