class Wallaby::Custom::ModelDecorator

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

Public Instance Methods

fields() click to toggle source

Assume that attributes come from the setter/getter, e.g. `name=`/`name` @return [ActiveSupport::HashWithIndifferentAccess] metadata

# 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(false).map(&:to_s)
      methods
        .grep(/[^=]$/).select { |method_id| methods.include? "#{method_id}=" }
        .each { |attribute| hash[attribute] = { label: attribute.humanize, type: 'string' } }
    end.freeze
end
form_active_errors(resource) click to toggle source

@param resource [Object] @return [ActiveModel::Errors]

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 54
def form_active_errors(resource)
  @form_active_errors ||= ActiveModel::Errors.new resource
end
form_field_names() click to toggle source

@return [Array<String>] a list of field names for form (new/edit) page

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 48
def form_field_names
  @form_field_names ||= form_fields.keys - [primary_key.to_s]
end
form_fields() click to toggle source

A copy of {#fields} for form (new/edit) page @return [ActiveSupport::HashWithIndifferentAccess] metadata

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 33
def form_fields
  @form_fields ||= Utils.clone fields
end
guess_title(resource) click to toggle source

@param resource [Object] @return [String, nil]

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 65
def guess_title(resource)
  FieldUtils
    .first_field_by({ name: /name|title|subject/ }, fields)
    .try { |field_name| resource.try field_name }
end
index_field_names() click to toggle source

@return [Array<String>] a list of field names for index page

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 38
def index_field_names
  @index_field_names ||= reposition index_fields.keys, primary_key
end
index_fields() click to toggle source

A copy of {#fields} for index page @return [ActiveSupport::HashWithIndifferentAccess] metadata

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 21
def index_fields
  @index_fields ||= Utils.clone fields
end
primary_key() click to toggle source

@return [String, Symbole] default to `:id`

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 59
def primary_key
  @primary_key ||= :id
end
show_field_names() click to toggle source

@return [Array<String>] a list of field names for show page

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 43
def show_field_names
  @show_field_names ||= reposition show_fields.keys, primary_key
end
show_fields() click to toggle source

A copy of {#fields} for show page @return [ActiveSupport::HashWithIndifferentAccess] metadata

# File lib/adaptors/wallaby/custom/model_decorator.rb, line 27
def show_fields
  @show_fields ||= Utils.clone fields
end