Class: Wallaby::ModelDecorator

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

Overview

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

See Also:

  • for more information on how to customize metadata

Direct Known Subclasses

Custom::ModelDecorator

Constant Summary collapse

/\A(?<method_prefix>[a-zA-Z]\w*_)(?<method_suffix>fields=?|field_names=?|metadata_of|label_of|type_of)\Z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fieldable

#label_of, #metadata_of, #prefix_field_names, #prefix_field_names=, #prefix_fields, #prefix_fields=, #type_of

Constructor Details

#initialize(model_class) ⇒ ModelDecorator

Initialize with model class

Parameters:

  • model_class (Class)


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

def initialize(model_class)
  @model_class = model_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object

Delegate missing methods to Fieldable if possible



121
122
123
124
125
126
127
# File 'lib/interfaces/wallaby/model_decorator.rb', line 121

def method_missing(method_id, *args, &block)
  method_name = method_id.to_s
  return super unless method_name.match?(MISSING_METHODS_RELATED_TO_FIELDS)

  matched = MISSING_METHODS_RELATED_TO_FIELDS.match(method_name)
  try("prefix_#{matched[:method_suffix]}", *args, matched[:method_prefix], &block)
end

Instance Attribute Details

#all_fieldsObject (readonly)

See Also:



24
25
26
# File 'lib/interfaces/wallaby/model_decorator.rb', line 24

def all_fields
  AllFields.new self
end

#fieldsActiveSupport::HashWithIndifferentAccess

Note:

to be implemented in subclasses.

Origin fields metadata.

Initially, #index_fields, #show_fields and #form_fields are copies of it.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

Raises:



34
35
36
# File 'lib/interfaces/wallaby/model_decorator.rb', line 34

def fields
  raise NotImplemented
end

#filtersActiveSupport::HashWithIndifferentAccess (readonly)

Note:

to be implemented in subclasses.

Filter metadata for index page.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)


78
79
80
# File 'lib/interfaces/wallaby/model_decorator.rb', line 78

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

#form_field_namesArray<String, Symbol> (readonly)

A list of field names for form (‘new`/`edit`) page

Returns:

  • (Array<String, Symbol>)


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

def form_field_names
  @form_field_names ||= form_fields.keys - [primary_key.to_s]
end

#form_fieldsActiveSupport::HashWithIndifferentAccess (readonly)

Note:

to be implemented in subclasses.

Fields metadata for form (‘new`/`edit`) page.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

Raises:



63
64
65
# File 'lib/interfaces/wallaby/model_decorator.rb', line 63

def form_fields
  raise NotImplemented
end

#index_fieldsActiveSupport::HashWithIndifferentAccess (readonly)

Note:

to be implemented in subclasses.

Fields metadata for ‘index` page.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

Raises:



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

def index_fields
  raise NotImplemented
end

#model_classClass

Returns:

  • (Class)


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

def model_class
  @model_class
end

#primary_keyString

Note:

to be implemented in subclasses.

Returns primary key name.

Returns:

  • (String)

    primary key name

Raises:



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

def primary_key
  raise NotImplemented
end

#show_fieldsActiveSupport::HashWithIndifferentAccess (readonly)

Note:

to be implemented in subclasses.

Fields metadata for ‘show` page.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

Raises:



55
56
57
# File 'lib/interfaces/wallaby/model_decorator.rb', line 55

def show_fields
  raise NotImplemented
end

Instance Method Details

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

Note:

to be implemented in subclasses.

Validation error for given resource

Parameters:

  • _resource (Object)

Returns:

  • (ActiveModel::Errors, Hash)

Raises:



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

def form_active_errors(_resource)
  raise NotImplemented
end

#guess_title(_resource) ⇒ String

Note:

to be implemented in subclasses.

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)

Returns:

  • (String)

    title of current resource

Raises:



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

def guess_title(_resource)
  raise NotImplemented
end

#resources_nameString

Returns:

  • (String)

See Also:



116
117
118
# File 'lib/interfaces/wallaby/model_decorator.rb', line 116

def resources_name
  Map.resources_name_map model_class
end

#respond_to_missing?(method_id, _include_private) ⇒ Boolean

Check if method looks like: ‘_fields`, `_field_names` that can be processed by Fieldable

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/interfaces/wallaby/model_decorator.rb', line 130

def respond_to_missing?(method_id, _include_private)
  method_name = method_id.to_s
  method_name.match?(MISSING_METHODS_RELATED_TO_FIELDS) || super
end