module Pakyow::Routing::Extension::Resource::NestedResource

Public Class Methods

define(controller, nested_resource_id, nested_param) click to toggle source

Nest resources as members of the current resource.

Calls superclass method
# File lib/pakyow/routing/extensions/resource.rb, line 130
def self.define(controller, nested_resource_id, nested_param)
  unless controller.singleton_class.instance_methods(false).include?(:namespace)
    controller.define_singleton_method :namespace do |*args, &block|
      super(*args, &block).tap do |namespace|
        namespace.allow_params nested_param
        namespace.action :update_request_path_for_parent do
          connection.get(:__endpoint_path).gsub!("/#{nested_resource_id}", "")
        end
      end
    end
  end

  unless controller.singleton_class.instance_methods(false).include?(:resource)
    controller.define_singleton_method :resource do |name, matcher, param: DEFAULT_PARAM, &block|
      if existing_resource = children.find { |child| child.expansions.include?(:resource) && child.__object_name.name == name }
        existing_resource.instance_exec(&block); existing_resource
      else
        expand(:resource, name, File.join(nested_resource_id, matcher), param: param) do
          allow_params nested_param

          action :update_request_path_for_parent do
            connection.get(:__endpoint_path).gsub!("/#{nested_resource_id}", "")
          end

          class_eval(&block)
        end
      end
    end
  end
end