# File lib/interfaces/wallaby/model_decorator.rb, line 125 def primary_key raise NotImplemented end
class Wallaby::ModelDecorator
Model Decorator interface, designed to maintain metadata for all fields from the data source (database/api) @see Wallaby::ResourceDecorator
for more information on how to customize metadata
Attributes
@!attribute [w] form_field_names
@!attribute [w] index_field_names
@!attribute [r] model_class
@return [Class]
@!attribute [w] primary_key
@!attribute [w] show_field_names
Public Class Methods
Initialize with model class @param model_class
[Class]
# File lib/interfaces/wallaby/model_decorator.rb, line 11 def initialize(model_class) @model_class = model_class end
Public Instance Methods
@!attribute [r] fields @note to be implemented in subclasses. Origin fields metadata.
Initially, {#index_fields}, {#show_fields} and {#form_fields} are copies of it. @return [ActiveSupport::HashWithIndifferentAccess]
# File lib/interfaces/wallaby/model_decorator.rb, line 25 def fields raise NotImplemented end
@!attribute [w] model_class
# File lib/interfaces/wallaby/model_decorator.rb, line 30 def fields=(fields) @fields = fields.with_indifferent_access end
@!attribute [r] filters @note to be implemented in subclasses. Filter metadata for index page. @return [ActiveSupport::HashWithIndifferentAccess]
# File lib/interfaces/wallaby/model_decorator.rb, line 107 def filters @filters ||= ::ActiveSupport::HashWithIndifferentAccess.new end
@note to be implemented in subclasses. Validation error for given resource @param _resource [Object] @return [ActiveModel::Errors, Hash]
# File lib/interfaces/wallaby/model_decorator.rb, line 115 def form_active_errors(_resource) raise NotImplemented end
@!attribute [r] form_field_names
A list of field names for form (`new`/`edit`) page @return [Array<String, Symbol>]
# File lib/interfaces/wallaby/model_decorator.rb, line 99 def form_field_names @form_field_names ||= reposition form_fields.keys, primary_key end
@!attribute [r] form_fields
@note to be implemented in subclasses. Fields metadata for form (`new`/`edit`) page. @return [ActiveSupport::HashWithIndifferentAccess]
# File lib/interfaces/wallaby/model_decorator.rb, line 84 def form_fields raise NotImplemented end
@!attribute [w] form_fields
# File lib/interfaces/wallaby/model_decorator.rb, line 89 def form_fields=(fields) @form_fields = fields.with_indifferent_access end
@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
@param _resource [Object] @return [String] title of current resource
# File lib/interfaces/wallaby/model_decorator.rb, line 139 def guess_title(_resource) raise NotImplemented end
@!attribute [r] index_field_names
A list of field names for `index` page @return [Array<String, Symbol>]
# File lib/interfaces/wallaby/model_decorator.rb, line 53 def index_field_names @index_field_names ||= reposition index_fields.keys, primary_key end
@!attribute [r] index_fields
@note to be implemented in subclasses. Fields metadata for `index` page. @return [ActiveSupport::HashWithIndifferentAccess]
# File lib/interfaces/wallaby/model_decorator.rb, line 38 def index_fields raise NotImplemented end
@!attribute [w] index_fields
# File lib/interfaces/wallaby/model_decorator.rb, line 43 def index_fields=(fields) @index_fields = fields.with_indifferent_access end
@!attribute [r] primary_key
@note to be implemented in subclasses. @return [String] primary key name
@return [String] @see Wallaby::Map.resources_name_map
# File lib/interfaces/wallaby/model_decorator.rb, line 145 def resources_name Map.resources_name_map model_class end
@!attribute [r] show_field_names
A list of field names for `show` page @return [Array<String, Symbol>]
# File lib/interfaces/wallaby/model_decorator.rb, line 76 def show_field_names @show_field_names ||= reposition show_fields.keys, primary_key end
@!attribute [r] show_fields
@note to be implemented in subclasses. Fields metadata for `show` page. @return [ActiveSupport::HashWithIndifferentAccess]
# File lib/interfaces/wallaby/model_decorator.rb, line 61 def show_fields raise NotImplemented end
@!attribute [w] show_fields
# File lib/interfaces/wallaby/model_decorator.rb, line 66 def show_fields=(fields) @show_fields = fields.with_indifferent_access end
Protected Instance Methods
Ensure the type is present for given field name @param type [String, Symbol, nil] @return [String, Symbol] type @raise [ArgumentError] when type is nil
# File lib/interfaces/wallaby/model_decorator.rb, line 164 def ensure_type_is_present(field_name, type, metadata_prefix = '') type || raise(::ArgumentError, <<~INSTRUCTION The type for field `#{field_name}` is missing in metadata `#{metadata_prefix}_fields`. The possible causes are: 1. Check type's value from metadata `#{metadata_prefix}_fields[:#{field_name}][:type]`. If it is missing, specify the type as below: #{metadata_prefix}field_name[:#{field_name}][:type] = 'string' 2. If metadata `#{metadata_prefix}_fields` is blank, maybe table hasn't be created yet or there is some error in the decorator class declaration. INSTRUCTION ) end
Move primary key to the front for given field names. @param field_names [Array<String, Symbol>] field names @param primary_key
[String, Symbol] primary key name @return [Array<String, Symbol>] a new list of field names that primary key goes first
# File lib/interfaces/wallaby/model_decorator.rb, line 156 def reposition(field_names, primary_key) field_names.unshift(primary_key.to_s).uniq end