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
-
#present(name, options = {}) { ... } ⇒ Hash
Return a Ruby hash that contains models requested by the user’s params and allowed by the
namepresenter’s configuration. -
#present_object(objects, options = {}) ⇒ Hash
(also: #present_objects)
Similar to ControllerMethods#present, but always returns all of the given objects, not just those that match any provided filters.
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) }
18 19 20 |
# File 'lib/brainstem/controller_methods.rb', line 18 def present(name, = {}, &block) Brainstem.presenter_collection([:namespace]).presenting(name, .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.
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, = {}) .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 [:as] = ([:key_map] || {})[klass.to_s] || klass.table_name present(klass, ) { klass.where(:id => ids) } end |