-
Notifications
You must be signed in to change notification settings - Fork 0
Decorators in Approvals
hackmastera edited this page Jun 14, 2019
·
2 revisions
Decorators are read only classes to combine data for display. In Approvals we are creating decorators as Plain Old Ruby Objects (POROs) that get the class they are decorating on initialization along with any addition data items they might need like the current environment. The decorators are connected to the views in the controllers by instantiating them and passing them back instead of the object they are decorating. The decorator is expected to delegate methods to the object, to allow for normal function.
class RequestListDecorator
attr_reader :request_list, :params_hash
delegate :count, :each, :first, :last, :map, :to_a, to: :request_list
# @param [Array] list of request model objects
# @param [Hash] params_hash current request parameters; filter and sort options
def initialize(request_list, params_hash: {})
@request_list = request_list
@params_hash = prams_hash
end
def title
...
end
...
end
class RequestsController < ApplicationController
# GET /my_requests
# GET /my_requests.json
def all_requests
@requests = RequestListDecorator.new(Request.all, params_hash: request_params.to_h)
end
private
# all params for this controller
def request_params
params.permit(:query, :sort, filters: [:status, :request_type])
end
end