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
-
#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. -
#brainstem_present_object(objects, options = {}) ⇒ Hash
(also: #brainstem_present_objects)
Similar to ControllerMethods#brainstem_present, but always returns all of the given objects, not just those that match any provided filters.
Methods included from Brainstem::Concerns::ControllerDSL
#brainstem_valid_params, #transforms, #valid_params_tree
Methods included from Brainstem::Concerns::InheritableConfiguration
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) }
27 28 29 |
# File 'lib/brainstem/controller_methods.rb', line 27 def brainstem_present(name, = {}, &block) Brainstem.presenter_collection([:namespace]).presenting(name, .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.
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, = {}) .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 [:params][:only] = ids.to_s end if [:key_map] raise "brainstem_present_object no longer accepts a :key_map. Use brainstem_key annotations on your presenters instead." end brainstem_present(klass, ) { klass.where(:id => ids) } end |