Class: ResourcesController::ResourceService

Inherits:
ActiveSupport::BasicObject
Defined in:
lib/resources_controller.rb

Overview

Proxy class to provide a consistent API for resource_service. This is mostly required for Singleton resources. Also allows decoration of the resource service with custom finders

Direct Known Subclasses

SingletonResourceService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ ResourceService

Returns a new instance of ResourceService.



746
747
748
# File 'lib/resources_controller.rb', line 746

def initialize(controller)
  @controller = controller
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



750
751
752
# File 'lib/resources_controller.rb', line 750

def method_missing(*args, &block)
  service.send(*args, &block)
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



743
744
745
# File 'lib/resources_controller.rb', line 743

def controller
  @controller
end

Instance Method Details

#destroy(*args) ⇒ Object

find the resource If we have a resource service, we call destroy on it with the reosurce id, so that any callbacks can be triggered Otherwise, just call destroy on the resource



766
767
768
769
770
771
772
773
774
# File 'lib/resources_controller.rb', line 766

def destroy(*args)
  resource = find(*args)
  if enclosing_resource
    service.destroy(*args)
    resource
  else
    resource.destroy
  end
end

#find(*args, &block) ⇒ Object



754
755
756
# File 'lib/resources_controller.rb', line 754

def find(*args, &block)
  resource_specification.find ? resource_specification.find_custom(controller) : super
end

#new(*args, &block) ⇒ Object

build association on the enclosing resource if there is one, otherwise call new



759
760
761
# File 'lib/resources_controller.rb', line 759

def new(*args, &block)
  enclosing_resource ? service.build(*args, &block) : service.new(*args, &block)
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


776
777
778
# File 'lib/resources_controller.rb', line 776

def respond_to?(method, include_private = false)
  super || service.respond_to?(method)
end

#serviceObject



780
781
782
# File 'lib/resources_controller.rb', line 780

def service
  @service ||= enclosing_resource ? enclosing_resource.send(resource_specification.source) : resource_class
end