Class: Wallaby::AbstractResourceDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/decorators/wallaby/abstract_resource_decorator.rb

Overview

Resource Decorator base class

Direct Known Subclasses

ResourceDecorator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ AbstractResourceDecorator

Returns a new instance of AbstractResourceDecorator.

Parameters:

  • resource (Object)

    resource object



29
30
31
32
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 29

def initialize(resource)
  @resource = resource
  @model_decorator = Map.model_decorator_map model_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object

We delegate missing methods to resource

Parameters:

  • method_id (String, Symbol)
  • args (Array)


68
69
70
71
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 68

def method_missing(method_id, *args)
  return super unless @resource.respond_to? method_id
  @resource.public_send method_id, *args
end

Instance Attribute Details

#model_decoratorObject (readonly)

Returns the value of attribute model_decorator.



26
27
28
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 26

def model_decorator
  @model_decorator
end

#resourceObject (readonly)

Returns the value of attribute resource.



26
27
28
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 26

def resource
  @resource
end

Class Method Details

.model_classClass

Guess the model class from class name

Returns:

  • (Class)


7
8
9
10
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 7

def model_class
  return unless self < ::Wallaby.configuration.mapping.resource_decorator
  Map.model_class_map name.gsub('Decorator', EMPTY_STRING)
end

.model_decoratorWallaby::ModelDecorator

Get the model decorator for the model class It should be the same as #model_decorator



15
16
17
18
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 15

def model_decorator
  return unless self < ::Wallaby.configuration.mapping.resource_decorator
  Map.model_decorator_map model_class
end

Instance Method Details

#errorsHash, Array

Return the validation errors

Returns:

  • (Hash, Array)


50
51
52
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 50

def errors
  @model_decorator.form_active_errors(@resource)
end

#model_classClass

Returns resource’s class.

Returns:

  • (Class)

    resource’s class



35
36
37
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 35

def model_class
  @resource.class
end

#primary_key_valueObject

Returns primary key value.

Returns:

  • (Object)

    primary key value



55
56
57
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 55

def primary_key_value
  @resource.public_send primary_key
end

#respond_to_missing?(method_id, _include_private) ⇒ Boolean

Parameters:

  • method_id (String, Symbol)
  • _include_private (Boolean)

Returns:

  • (Boolean)


75
76
77
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 75

def respond_to_missing?(method_id, _include_private)
  @resource.respond_to?(method_id) || super
end

#to_labelString

Guess the title for given resource It falls back to primary key value when no text field is found ‘.to_s` at the end is to ensure String is returned that won’t cause any issue when ‘#to_label` is used in a link_to block. Coz integer is ignored.

Returns:

  • (String)


44
45
46
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 44

def to_label
  (@model_decorator.guess_title(@resource) || primary_key_value).to_s
end