Module: Brainstem::ControllerMethods

Extended by:
ActiveSupport::Concern
Includes:
Brainstem::Concerns::ControllerDSL, Brainstem::Concerns::ControllerParamManagement, Brainstem::Concerns::ErrorPresentation
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.

Constant Summary

Constants included from Brainstem::Concerns::ControllerDSL

Brainstem::Concerns::ControllerDSL::DEFAULT_BRAINSTEM_PARAMS_CONTEXT

Instance Method Summary collapse

Methods included from Brainstem::Concerns::ControllerDSL

#brainstem_valid_params, #transforms, #valid_params_tree

Methods included from Brainstem::Concerns::InheritableConfiguration

#configuration

Methods included from Brainstem::Concerns::ErrorPresentation

#brainstem_full_error_message, #brainstem_model_error, #brainstem_system_error

Methods included from Brainstem::Concerns::ControllerParamManagement

#brainstem_model_name, #brainstem_plural_model_name

Instance Method Details

#brainstem_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 => brainstem_present("post"){ Post.where(:draft => false) }

Parameters:

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

    a customizable set of options

  • name (Class, String)

    Either the ActiveRecord Class itself, or its pluralized table name as a string.

  • 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.



27
28
29
# File 'lib/brainstem/controller_methods.rb', line 27

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

#brainstem_present_object(objects, options = {}) ⇒ Hash Also known as: brainstem_present_objects

Similar to ControllerMethods#brainstem_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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brainstem/controller_methods.rb', line 38

def brainstem_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

  if options[:key_map]
    raise "brainstem_present_object no longer accepts a :key_map.  Use brainstem_key annotations on your presenters instead."
  end

  brainstem_present(klass, options) { klass.where(:id => ids) }
end