Module: Wallaby::Resourcable

Included in:
ResourcesConcern, ResourcesHelper
Defined in:
lib/concerns/wallaby/resourcable.rb

Overview

Resources related attributes & helper methods

Instance Method Summary collapse

Instance Method Details

#collection(params: self.params, paginate: true, **paginate_options) {|collection| ... } ⇒ #each Also known as: collection!

Note:

This is a template method that can be overridden by subclasses.

This is a method to return collection for index action and page.

It can be customized as below in subclasses:

def collection
  # do something before the original action
  options = {} # @see arguments for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  collection!(options) do |query|
    # NOTE: make sure to return a collection in the block
    query.where(active: true)
  end
end

Otherwise, it can be replaced completely in subclasses:

def collection
  # NOTE: pagination should happen here if needed
  # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
  @collection ||= paginate Product.active
end

Parameters:

  • params (Hash, ActionController::Parameters) (defaults to: self.params)

    parameters for collection query, default to request parameters

  • paginate (Boolean) (defaults to: true)

    see #paginate

  • paginate_options (Hash)

    options accepted by #paginate (since 0.3.0)

Yields:

  • (collection)

    (since wallaby-5.2.0) a block to run to extend/convert the original collection, e.g. call chain with more queries

Returns:

  • (#each)

    a collection of records



99
100
101
102
103
104
105
106
107
108
# File 'lib/concerns/wallaby/resourcable.rb', line 99

def collection(params: self.params, paginate: true, **paginate_options, &block)
  @collection ||=
    ModuleUtils.yield_for(
      paginate(
        current_servicer.collection(params),
        paginate_options.merge(paginate: paginate)
      ),
      &block
    )
end

#create_paramsActionController::Parameters

Note:

This is a template method that can be overridden by subclasses.

To allowlist the request params for create action.

It can be replaced completely in subclasses:

def create_params
  params.fetch(:product, {}).permit(:name, :sku)
end

Returns:

  • (ActionController::Parameters)

    allowlisted params



54
55
56
# File 'lib/concerns/wallaby/resourcable.rb', line 54

def create_params
  resource_params
end

#current_model_classClass

Model class for current request.

It comes from two places:

Returns:

  • (Class)

    model class for current request



20
21
22
23
# File 'lib/concerns/wallaby/resourcable.rb', line 20

def current_model_class
  @current_model_class ||=
    wallaby_controller.model_class || Map.model_class_map(current_resources_name || controller_path)
end

#current_resources_nameString

Returns resources name for current request.

Returns:

  • (String)

    resources name for current request



7
8
9
# File 'lib/concerns/wallaby/resourcable.rb', line 7

def current_resources_name
  @current_resources_name ||= params[:resources]
end

#new_resource {|resource| ... } ⇒ Object Also known as: new_resource!

Note:

This is a template method that can be overridden by subclasses.

This is a method to return resource for new and create actions.

It can be customized as below in subclasses:

def new_resource
  # do something before the original action
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  new_resource! do |object|
    object.preload_status_from_api
    # NOTE: make sure to return an object in the block
    object
  end
end

Otherwise, it can be replaced completely in subclasses:

def new_resource
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource ||= Product.new(arrival: true)
end

Yields:

  • (resource)

    (since wallaby-5.2.0) a block to run to extend resource, e.g. making change to the resource. Please make sure to return the expected resource at the end of block

Returns:

  • (Object)

    unpersisted resource instance

Since:

  • 0.3.0



170
171
172
173
# File 'lib/concerns/wallaby/resourcable.rb', line 170

def new_resource(&block)
  @resource ||=
    ModuleUtils.yield_for(current_servicer.new({}), &block)
end

#resource {|resource| ... } ⇒ Object Also known as: resource!

Note:

This is a template method that can be overridden by subclasses.

This is a method to return resource for show, edit, update and destroy actions.

It can be customized as below in subclasses:

def resource
  # do something before the original action
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  resource! do |object|
    object.preload_status_from_api
    # NOTE: make sure to return an object in the block
    object
  end
end

Otherwise, it can be replaced completely in subclasses:

def resource
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource ||= resource_id.present? ? Product.find_by_slug(resource_id) : Product.new(arrival: true)
end

Yields:

  • (resource)

    (since wallaby-5.2.0) a block to run to extend resource, e.g. making change to the resource. Please make sure to return the expected resource at the end of block

Returns:

  • (Object)

    persisted resource instance

Raises:



138
139
140
141
# File 'lib/concerns/wallaby/resourcable.rb', line 138

def resource(&block)
  @resource ||=
    ModuleUtils.yield_for(current_servicer.find(resource_id, {}), &block)
end

#resource_idString?

Shorthand of params

Returns:

  • (String, nil)

    ID param



27
28
29
# File 'lib/concerns/wallaby/resourcable.rb', line 27

def resource_id
  params[:id]
end

#resource_paramsActionController::Parameters

Note:

This is a template method that can be overridden by subclasses.

It’s used by #create_params and #update_params.

It can be replaced completely in subclasses and will impact both #create_params and #update_params:

def resource_params
  params.fetch(:product, {}).permit(:name, :sku)
end

Returns:

  • (ActionController::Parameters)

    allowlisted params



41
42
43
# File 'lib/concerns/wallaby/resourcable.rb', line 41

def resource_params
  @resource_params ||= current_servicer.permit params, action_name
end

#update_paramsActionController::Parameters

Note:

This is a template method that can be overridden by subclasses.

To allowlist the request params for update action.

It can be replaced completely in subclasses:

def update_params
  params.fetch(:product, {}).permit(:name, :sku)
end

Returns:

  • (ActionController::Parameters)

    allowlisted params



67
68
69
# File 'lib/concerns/wallaby/resourcable.rb', line 67

def update_params
  resource_params
end