Module: Brainstem::ControllerMethods

Defined in:
lib/brainstem/controller_methods.rb

Overview

ControllerMethods are intended to be included into controllers that will be handling requests for presented objects. The present method will pass through params, so that any allowed and requested includes, filters, sort orders will be applied to the presented data.

Instance Method Summary collapse

Instance Method Details

#present(name, options = {}) { ... } ⇒ Hash

Return a Ruby hash that contains models requested by the user’s params and allowed by the name presenter’s configuration.

Pass the returned hash to the render method to convert it into a useful format. For example:

render :json => present("post"){ Post.where(:draft => false) }

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

  • name (Class, String)

    The class of the objects to be presented.

  • options (Hash) (defaults to: {})

    The options that will be applied as the objects are converted.

Options Hash (options):

  • :namespace (String) — default: "none"

    the namespace to be presented from

Yields:

  • Must return a scope on the model name, which will then be presented.

Returns:

  • (Hash)

    A hash of arrays of hashes. Top-level hash keys are pluralized model names, with values of arrays containing one hash per object that was found by the given given options.



18
19
20
# File 'lib/brainstem/controller_methods.rb', line 18

def present(name, options = {}, &block)
  Brainstem.presenter_collection(options[:namespace]).presenting(name, options.reverse_merge(:params => params), &block)
end

#present_object(objects, options = {}) ⇒ Hash Also known as: present_objects

Similar to ControllerMethods#present, but always returns all of the given objects, not just those that match any provided filters.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :namespace (String) — default: "none"

    the namespace to be presented from

  • :key_map (Hash)

    a Hash from Class name to json key name, if desired. e.g., map ‘SystemWidgets’ objects to the ‘widgets’ key in the JSON. This is only required if the name cannot be inferred.

Returns:

  • (Hash)

    A hash of arrays of hashes. Top-level hash keys are pluralized model names, with values of arrays containing one hash per object that was found by the given given options.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/brainstem/controller_methods.rb', line 29

def present_object(objects, options = {})
  options.merge!(:params => params, :apply_default_filters => false)

  if objects.is_a?(ActiveRecord::Relation) || objects.is_a?(Array)
    raise ActiveRecord::RecordNotFound if objects.empty?
    klass = objects.first.class
    ids = objects.map(&:id)
  else
    klass = objects.class
    ids = objects.id
    options[:params][:only] = ids.to_s
  end

  options[:as] = (options[:key_map] || {})[klass.to_s] || klass.table_name
  present(klass, options) { klass.where(:id => ids) }
end