Module: GardenVariety::Controller
- Extended by:
- ActiveSupport::Concern
- Includes:
- TalentScout::FindCollectionOverride, Pundit::Authorization
- Defined in:
- lib/garden_variety/controller.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#assign_attributes(model) ⇒ ActiveRecord::Base
Populates the given
model‘s attributes with the current request params permitted by the corresponding Pundit policy. -
#collection ⇒ Object
Returns the value of the plural-form instance variable dictated by ::model_class.
-
#collection=(values) ⇒ values
Sets the value of the plural-form instance variable dictated by ::model_class.
-
#find_collection ⇒ ActiveRecord::Relation
Returns an ActiveRecord::Relation representing instances of ::model_class.
-
#find_model ⇒ ActiveRecord::Base
Returns an instance of ::model_class matching the
:idparameter of the current request (i.e.params[:id]). -
#flash_message(status) ⇒ String
Returns a flash message appropriate to the controller, the current action, and a given
status. -
#flash_options ⇒ Hash{Symbol => #to_s}
Returns a Hash of values for interpolation in flash messages via I18n.
-
#model ⇒ Object
Returns the value of the singular-form instance variable dictated by ::model_class.
-
#model=(value) ⇒ value
Sets the value of the singular-form instance variable dictated by ::model_class.
-
#new_model ⇒ ActiveRecord::Base
Returns a new instance of ::model_class.
Instance Method Details
#assign_attributes(model) ⇒ ActiveRecord::Base
Populates the given model‘s attributes with the current request params permitted by the corresponding Pundit policy. Returns the given model modified but not persisted.
254 255 256 257 |
# File 'lib/garden_variety/controller.rb', line 254 def assign_attributes(model) model.assign_attributes(permitted_attributes(model)) model end |
#collection ⇒ Object
Returns the value of the plural-form instance variable dictated by ::model_class.
158 159 160 |
# File 'lib/garden_variety/controller.rb', line 158 def collection instance_variable_get(:"@#{self.class.model_name.plural}") end |
#collection=(values) ⇒ values
Sets the value of the plural-form instance variable dictated by ::model_class.
178 179 180 |
# File 'lib/garden_variety/controller.rb', line 178 def collection=(values) instance_variable_set(:"@#{self.class.model_name.plural}", values) end |
#find_collection ⇒ ActiveRecord::Relation
Returns an ActiveRecord::Relation representing instances of ::model_class. Designed for use in generic index action methods.
195 196 197 |
# File 'lib/garden_variety/controller.rb', line 195 def find_collection self.class.model_class.all end |
#find_model ⇒ ActiveRecord::Base
Returns an instance of ::model_class matching the :id parameter of the current request (i.e. params[:id]). Designed for use in generic show, edit, update, and destroy action methods.
215 216 217 |
# File 'lib/garden_variety/controller.rb', line 215 def find_model self.class.model_class.find(params[:id]) end |
#flash_message(status) ⇒ String
Returns a flash message appropriate to the controller, the current action, and a given status. The flash message is looked up via I18n using a prioritized list of possible keys. The key priority is as follows:
-
{controller_name}.{action_name}.{status}
-
{controller_name}.{action_name}.{status}_html
-
{action_name}.{status}
-
{action_name}.{status}_html
-
{status}
-
{status}_html
If the controller is namespaced, the namespace will prefix (dot-separated) the {controller_name} portion of the key.
I18n string interpolation can be used in flash messages, with interpolated values provided by the #flash_options method.
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/garden_variety/controller.rb', line 323 def (status) controller_key = controller_path.tr("/", I18n.default_separator) keys = [ :"flash.#{controller_key}.#{action_name}.#{status}", :"flash.#{controller_key}.#{action_name}.#{status}_html", :"flash.#{action_name}.#{status}", :"flash.#{action_name}.#{status}_html", :"flash.#{status}", :"flash.#{status}_html", ] helpers.translate(keys.shift, default: keys, **) end |
#flash_options ⇒ Hash{Symbol => #to_s}
Returns a Hash of values for interpolation in flash messages via I18n. By default, returns a :model_name key / value pair with the humanized name of ::model_class. Override this method to provide your own values. Be aware that certain option names, such as :default and :scope, are reserved by the I18n gem, and can not be used for interpolation. See the I18n documentation for more information.
270 271 272 |
# File 'lib/garden_variety/controller.rb', line 270 def { model_name: self.class.model_name.human } end |
#model ⇒ Object
Returns the value of the singular-form instance variable dictated by ::model_class.
119 120 121 |
# File 'lib/garden_variety/controller.rb', line 119 def model instance_variable_get(:"@#{self.class.model_name.singular}") end |
#model=(value) ⇒ value
Sets the value of the singular-form instance variable dictated by ::model_class.
139 140 141 |
# File 'lib/garden_variety/controller.rb', line 139 def model=(value) instance_variable_set(:"@#{self.class.model_name.singular}", value) end |
#new_model ⇒ ActiveRecord::Base
Returns a new instance of ::model_class. Designed for use in generic new and create action methods.
231 232 233 |
# File 'lib/garden_variety/controller.rb', line 231 def new_model self.class.model_class.new end |