Module: GardenVariety::Controller
- Extended by:
- ActiveSupport::Concern
- Includes:
- TalentScout::FindCollectionOverride, Pundit
- 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 model’s 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 model instances corresponding to the controller.
-
#find_model ⇒ ActiveRecord::Base
Returns a model instance corresponding to the controller and the id parameter 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
Returns 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 model instance corresponding to the controller.
Instance Method Details
#assign_attributes(model) ⇒ ActiveRecord::Base
Populates the given model’s attributes with the current request params permitted by the model’s Pundit policy. Returns the given model modified but not persisted.
253 254 255 256 |
# File 'lib/garden_variety/controller.rb', line 253 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.
159 160 161 |
# File 'lib/garden_variety/controller.rb', line 159 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.
179 180 181 |
# File 'lib/garden_variety/controller.rb', line 179 def collection=(values) instance_variable_set(:"@#{self.class.model_name.plural}", values) end |
#find_collection ⇒ ActiveRecord::Relation
Returns an ActiveRecord::Relation representing model instances corresponding to the controller. Designed for use in generic index action methods.
196 197 198 |
# File 'lib/garden_variety/controller.rb', line 196 def find_collection self.class.model_class.all end |
#find_model ⇒ ActiveRecord::Base
Returns a model instance corresponding to the controller and the id parameter of the current request (i.e. params[:id]). Designed for use in generic show, edit, update, and destroy action methods.
214 215 216 |
# File 'lib/garden_variety/controller.rb', line 214 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.
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/garden_variety/controller.rb', line 322 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 }.merge!()) end |
#flash_options ⇒ Hash
Returns Hash of values for interpolation in flash messages via I18n. By default, returns a model_name key / value pair based on the controller’s GardenVariety::Controller::ClassMethods#model_name. 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.
269 270 271 |
# File 'lib/garden_variety/controller.rb', line 269 def { model_name: self.class.model_name.human } end |
#model ⇒ Object
Returns the value of the singular-form instance variable dictated by model_class.
120 121 122 |
# File 'lib/garden_variety/controller.rb', line 120 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.
140 141 142 |
# File 'lib/garden_variety/controller.rb', line 140 def model=(value) instance_variable_set(:"@#{self.class.model_name.singular}", value) end |
#new_model ⇒ ActiveRecord::Base
Returns a new model instance corresponding to the controller. Designed for use in generic new and create action methods.
230 231 232 |
# File 'lib/garden_variety/controller.rb', line 230 def new_model self.class.model_class.new end |