Class: Wallaby::ModelDecorator

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

Overview

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

Direct Known Subclasses

ActiveRecord::ModelDecorator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ ModelDecorator

Returns a new instance of ModelDecorator.



11
12
13
# File 'lib/interfaces/wallaby/model_decorator.rb', line 11

def initialize(model_class)
  @model_class = model_class
end

Instance Attribute Details

#field_namesArray

Returns field names.

Returns:

  • (Array)

    field names



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

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

#form_field_namesArray

Returns field names of form page.

Returns:

  • (Array)

    field names of form page



123
124
125
# File 'lib/interfaces/wallaby/model_decorator.rb', line 123

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

#index_field_namesArray

Returns field names of index page.

Returns:

  • (Array)

    field names of index page



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

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

#model_classObject (readonly)

Returns the value of attribute model_class.



7
8
9
# File 'lib/interfaces/wallaby/model_decorator.rb', line 7

def model_class
  @model_class
end

#show_field_namesArray

Returns field names of show page.

Returns:

  • (Array)

    field names of show page



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

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

Instance Method Details

#fieldsHash

Returns metadata information for all fields that index/show/form will copy.

Returns:

  • (Hash)

    metadata information for all fields that index/show/form will copy

Raises:



17
18
19
# File 'lib/interfaces/wallaby/model_decorator.rb', line 17

def fields
  raise NotImplemented
end

#fields=(fields) ⇒ Object

Parameters:

  • fields (Hash)

    metadata information for all fields



22
23
24
# File 'lib/interfaces/wallaby/model_decorator.rb', line 22

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

#filtersHash

Returns custom filters.

Returns:

  • (Hash)

    custom filters



143
144
145
# File 'lib/interfaces/wallaby/model_decorator.rb', line 143

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

#form_active_errors(_resource) ⇒ Hash

Returns errors.

Returns:

  • (Hash)

    errors

Raises:



148
149
150
# File 'lib/interfaces/wallaby/model_decorator.rb', line 148

def form_active_errors(_resource)
  raise NotImplemented
end

#form_fieldsHash

Returns metadata information for all fields that would be used on form page.

Returns:

  • (Hash)

    metadata information for all fields that would be used on form page

Raises:



112
113
114
# File 'lib/interfaces/wallaby/model_decorator.rb', line 112

def form_fields
  raise NotImplemented
end

#form_fields=(fields) ⇒ Object

Parameters:

  • fields (Hash)

    metadata information for all fields that would be used on form page



118
119
120
# File 'lib/interfaces/wallaby/model_decorator.rb', line 118

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

#form_label_of(field_name) ⇒ String

Returns form label for a given field.

Returns:

  • (String)

    form label for a given field



133
134
135
# File 'lib/interfaces/wallaby/model_decorator.rb', line 133

def form_label_of(field_name)
  (field_name)[:label]
end

#form_metadata_of(field_name) ⇒ Hash

Returns form metadata information for a given field.

Returns:

  • (Hash)

    form metadata information for a given field



128
129
130
# File 'lib/interfaces/wallaby/model_decorator.rb', line 128

def (field_name)
  form_fields[field_name] || {}
end

#form_type_of(field_name) ⇒ String, Symbol

Returns form type for a given field.

Returns:

  • (String, Symbol)

    form type for a given field



138
139
140
# File 'lib/interfaces/wallaby/model_decorator.rb', line 138

def form_type_of(field_name)
  validate_presence_of (field_name)[:type]
end

#guess_title(_resource) ⇒ String

Returns title of given resource.

Parameters:

  • _resource (Object)

    resource

Returns:

  • (String)

    title of given resource

Raises:



159
160
161
# File 'lib/interfaces/wallaby/model_decorator.rb', line 159

def guess_title(_resource)
  raise NotImplemented
end

#index_fieldsHash

Returns metadata information for all fields that would be used on index page.

Returns:

  • (Hash)

    metadata information for all fields that would be used on index page

Raises:



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

def index_fields
  raise NotImplemented
end

#index_fields=(fields) ⇒ Object

Parameters:

  • fields (Hash)

    metadata information for all fields that would be used on index page



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

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

#index_label_of(field_name) ⇒ String

Returns index label for a given field.

Returns:

  • (String)

    index label for a given field



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

def index_label_of(field_name)
  (field_name)[:label]
end

#index_metadata_of(field_name) ⇒ Hash

Returns index metadata information for a given field.

Returns:

  • (Hash)

    index metadata information for a given field



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

def (field_name)
  index_fields[field_name] || {}
end

#index_type_of(field_name) ⇒ String, Symbol

Returns index type for a given field.

Returns:

  • (String, Symbol)

    index type for a given field



74
75
76
# File 'lib/interfaces/wallaby/model_decorator.rb', line 74

def index_type_of(field_name)
  validate_presence_of (field_name)[:type]
end

#label_of(field_name) ⇒ String

Returns label for a given field.

Returns:

  • (String)

    label for a given field



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

def label_of(field_name)
  (field_name)[:label]
end

#metadata_of(field_name) ⇒ Hash

Returns metadata information for a given field.

Returns:

  • (Hash)

    metadata information for a given field



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

def (field_name)
  fields[field_name] || {}
end

#primary_keyString

Returns primary key.

Returns:

  • (String)

    primary key

Raises:



153
154
155
# File 'lib/interfaces/wallaby/model_decorator.rb', line 153

def primary_key
  raise NotImplemented
end

#resources_nameString

Returns resources name mapped from model class.

Returns:

  • (String)

    resources name mapped from model class



164
165
166
# File 'lib/interfaces/wallaby/model_decorator.rb', line 164

def resources_name
  Map.resources_name_map @model_class
end

#show_fieldsHash

Returns metadata information for all fields that would be used on show page.

Returns:

  • (Hash)

    metadata information for all fields that would be used on show page

Raises:



80
81
82
# File 'lib/interfaces/wallaby/model_decorator.rb', line 80

def show_fields
  raise NotImplemented
end

#show_fields=(fields) ⇒ Object

Parameters:

  • fields (Hash)

    metadata information for all fields that would be used on show page



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

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

#show_label_of(field_name) ⇒ String

Returns show label for a given field.

Returns:

  • (String)

    show label for a given field



101
102
103
# File 'lib/interfaces/wallaby/model_decorator.rb', line 101

def show_label_of(field_name)
  (field_name)[:label]
end

#show_metadata_of(field_name) ⇒ Hash

Returns show metadata information for a given field.

Returns:

  • (Hash)

    show metadata information for a given field



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

def (field_name)
  show_fields[field_name] || {}
end

#show_type_of(field_name) ⇒ String, Symbol

Returns show type for a given field.

Returns:

  • (String, Symbol)

    show type for a given field



106
107
108
# File 'lib/interfaces/wallaby/model_decorator.rb', line 106

def show_type_of(field_name)
  validate_presence_of (field_name)[:type]
end

#type_of(field_name) ⇒ String, Symbol

Returns type for a given field.

Returns:

  • (String, Symbol)

    type for a given field



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

def type_of(field_name)
  validate_presence_of (field_name)[:type]
end