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.



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

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



65
66
67
68
# File 'lib/decorators/wallaby/abstract_resource_decorator.rb', line 65

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::ResourceDecorator
  Map.model_class_map name.gsub 'Decorator', EMPTY_STRING
end

.model_decoratorObject

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

Returns:

  • Wallaby::ModelDecorator



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

def model_decorator
  return unless self < ::Wallaby::ResourceDecorator
  Map.model_decorator_map model_class
end

Instance Method Details

#errorsHash, Array

Return the validation errors

Returns:

  • (Hash, Array)


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

def errors
  @model_decorator.form_active_errors(@resource)
end

#model_classClass

Returns resource’s class.

Returns:

  • (Class)

    resource’s class



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

def model_class
  @resource.class
end

#primary_key_valueObject

Returns primary key value.

Returns:

  • (Object)

    primary key value



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

def primary_key_value
  @resource.public_send primary_key
end

#respond_to_missing?(method_id, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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