Class: Wallaby::Custom::ModelDecorator

Inherits:
ModelDecorator show all
Defined in:
lib/adaptors/wallaby/custom/model_decorator.rb

Overview

Wallaby::Custom mode decorator that only pulls out all the attributes from setter/getter pair methods.

Constant Summary

Constants inherited from ModelDecorator

ModelDecorator::MISSING_METHODS_RELATED_TO_FIELDS

Instance Attribute Summary

Attributes inherited from ModelDecorator

#all_fields, #filters, #form_field_names, #model_class

Instance Method Summary collapse

Methods inherited from ModelDecorator

#initialize, #method_missing, #resources_name, #respond_to_missing?

Methods included from Fieldable

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

Constructor Details

This class inherits a constructor from Wallaby::ModelDecorator

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wallaby::ModelDecorator

Instance Method Details

#fieldsActiveSupport::HashWithIndifferentAccess

Retrieve the attributes from the setter/getter pair methods, e.g. ‘name=` and `name`

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



9
10
11
12
13
14
15
16
17
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 9

def fields
  @fields ||=
    ::ActiveSupport::HashWithIndifferentAccess.new.tap do |hash|
      methods = model_class.public_instance_methods.map(&:to_s)
      methods
        .grep(/(\A[^!=]|[^!=]\Z)/).select { |method_id| methods.include? "#{method_id}=" }
        .each { |attribute| hash[attribute] = { label: attribute.humanize, type: 'string' } }
    end.freeze
end

#form_active_errors(resource) ⇒ ActiveModel::Errors

It returns an ActiveModel::Errors instance. However, this instance does not contain any errors. You might want to override this method in the custom resource decorator and get the errors out from the given resource.

Parameters:

  • resource (Object)

Returns:

  • (ActiveModel::Errors)


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

def form_active_errors(resource)
  @form_active_errors ||= ActiveModel::Errors.new resource
end

#form_fieldsActiveSupport::HashWithIndifferentAccess

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

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



33
34
35
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 33

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

#guess_title(resource) ⇒ String?

Parameters:

  • resource (Object)

Returns:

  • (String, nil)


53
54
55
56
57
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 53

def guess_title(resource)
  FieldUtils
    .first_field_by({ name: /name|title|subject/ }, fields)
    .try { |field_name| resource.try field_name }
end

#index_fieldsActiveSupport::HashWithIndifferentAccess

A copy of #fields for index page

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



21
22
23
# File 'lib/adaptors/wallaby/custom/model_decorator.rb', line 21

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

#primary_keyString, Symbole

Returns default to ‘:id`.

Returns:

  • (String, Symbole)

    default to ‘:id`



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

def primary_key
  @primary_key ||= fields.key?(:id) ? :id : fields.keys.first.try(:to_sym)
end

#show_fieldsActiveSupport::HashWithIndifferentAccess

A copy of #fields for show page

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    metadata



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

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