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

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.

Examples:

class PostsController < ApplicationController
  def create
    @post = assign_attributes(authorize(Post.new))
    if @post.save
      redirect_to @post
    else
      render :new
    end
  end
end

Parameters:

  • model (ActiveRecord::Base)

Returns:

  • (ActiveRecord::Base)


253
254
255
256
# File 'lib/garden_variety/controller.rb', line 253

def assign_attributes(model)
  model.assign_attributes(permitted_attributes(model))
  model
end

#collectionObject

Returns the value of the plural-form instance variable dictated by model_class.

Examples:

class PostsController
  def index
    # This...
    self.collection
    # ...is equivalent to:
    @posts
  end
end

Returns:

  • (Object)


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.

Examples:

class PostsController
  def index
    # This...
    self.collection = values
    # ...is equivalent to:
    @posts = values
  end
end

Parameters:

  • values (Object)

Returns:

  • (values)


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_collectionActiveRecord::Relation

Returns an ActiveRecord::Relation representing model instances corresponding to the controller. Designed for use in generic index action methods.

Examples:

class PostsController < ApplicationController
  def index
     @posts = find_collection.where(status: "published")
  end
end

Returns:

  • (ActiveRecord::Relation)


196
197
198
# File 'lib/garden_variety/controller.rb', line 196

def find_collection
  self.class.model_class.all
end

#find_modelActiveRecord::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.

Examples:

class PostsController < ApplicationController
  def show
     @post = find_model
  end
end

Returns:

  • (ActiveRecord::Base)


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.

Examples:

Key priority

### config/locales/garden_variety.en.yml
# en:
#   success: "Success!"
#   create:
#     success: "%{model_name} created."
#   delete:
#     success: "%{model_name} deleted."
#   posts:
#     create:
#       success: "Congratulations on your new post!"
#   messages:
#     drafts:
#       update:
#         success: "Draft saved."

# via PostsController#create
flash_message(:success)  # == "Congratulations on your new post!"

# via PostsController#update
flash_message(:success)  # == "Success!"

# via PostsController#delete
flash_message(:success)  # == "Post deleted."

# via Messages::DraftsController#update
flash_message(:success)  # == "Draft saved."

Parameters:

  • status (Symbol, String)

Returns:

  • (String)


322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/garden_variety/controller.rb', line 322

def flash_message(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!(flash_options))
end

#flash_optionsHash

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.

Returns:

  • (Hash)


269
270
271
# File 'lib/garden_variety/controller.rb', line 269

def flash_options
  { model_name: self.class.model_name.human }
end

#modelObject

Returns the value of the singular-form instance variable dictated by model_class.

Examples:

class PostsController
  def show
    # This...
    self.model
    # ...is equivalent to:
    @post
  end
end

Returns:

  • (Object)


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.

Examples:

class PostsController
  def show
    # This...
    self.model = value
    # ...is equivalent to:
    @post = value
  end
end

Parameters:

  • value (Object)

Returns:

  • (value)


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_modelActiveRecord::Base

Returns a new model instance corresponding to the controller. Designed for use in generic new and create action methods.

Examples:

class PostsController < ApplicationController
  def new
     @post = new_model
  end
end

Returns:

  • (ActiveRecord::Base)


230
231
232
# File 'lib/garden_variety/controller.rb', line 230

def new_model
  self.class.model_class.new
end