module HalApi::Controller::Actions

Public Instance Methods

create() click to toggle source
# File lib/hal_api/controller/actions.rb, line 18
def create
  create_resource.tap do |res|
    consume! res, create_options
    hal_authorize res
    res.save!
    respond_with root_resource(res), create_options
  end
end
destroy() click to toggle source
# File lib/hal_api/controller/actions.rb, line 36
def destroy
  destroy_resource.tap do |res|
    hal_authorize res
    res.destroy
    head :no_content
  end
end
index() click to toggle source
# File lib/hal_api/controller/actions.rb, line 10
def index
  respond_with index_collection, index_options
end
show() click to toggle source
# File lib/hal_api/controller/actions.rb, line 14
def show
  respond_with root_resource(show_resource), show_options
end
update() click to toggle source
# File lib/hal_api/controller/actions.rb, line 27
def update
  update_resource.tap do |res|
    consume! res, show_options
    hal_authorize res
    res.save!
    respond_with root_resource(res), show_options
  end
end

Private Instance Methods

create_options() click to toggle source
# File lib/hal_api/controller/actions.rb, line 57
def create_options
  show_options.tap do |options|
    options[:location] = ''
  end
end
destroy_redirect() click to toggle source
# File lib/hal_api/controller/actions.rb, line 46
def destroy_redirect
  { action: 'index' }
end
hal_authorize(resource) click to toggle source
# File lib/hal_api/controller/actions.rb, line 92
def hal_authorize(resource)
  unless respond_to?(:authorize, true)
    define_singleton_method(:authorize) do |_resource|
      true
    end
    singleton_class.send(:alias_method, :hal_authorize, :authorize)
  end

  authorize(resource)
end
index_options() click to toggle source
# File lib/hal_api/controller/actions.rb, line 50
def index_options
  valid_params_for_action(:index).tap do |options|
    options[:_keys] = options.keys
    options[:represent_with] = HalApi::PagedCollection.representer
  end
end
root_resource(resource) click to toggle source
# File lib/hal_api/controller/actions.rb, line 88
def root_resource(resource)
  resource.tap { |res| res.is_root_resource = true }
end
show_options() click to toggle source
# File lib/hal_api/controller/actions.rb, line 63
def show_options
  valid_params_for_action(:show).tap do |options|
    options[:_keys] = options.keys
    if self.class.resource_representer
      options[:represent_with] = self.class.resource_representer
    end
  end
end
valid_params_for_action(action) click to toggle source
# File lib/hal_api/controller/actions.rb, line 72
def valid_params_for_action(action)
  (params.permit(*self.class.valid_params_list(action)) || {}).tap do |p|
    if zoom_param
      p[:user_options] = (p[:user_options] || {}).merge(zoom: zoom_param)
    end
  end.to_h
end
zoom_param() click to toggle source
# File lib/hal_api/controller/actions.rb, line 80
def zoom_param
  @zoom_param ||= begin
    if (zp = params[:zoom]) && !zp.nil?
      Array(zp.split(',')).map(&:strip).compact.sort
    end
  end
end