module GardenVariety::Controller

Private Instance Methods

assign_attributes(model) click to toggle source

@!visibility public 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.

@example

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

@param model [ActiveRecord::Base] @return [ActiveRecord::Base]

# File lib/garden_variety/controller.rb, line 253
def assign_attributes(model)
  model.assign_attributes(permitted_attributes(model))
  model
end
collection() click to toggle source

@!visibility public Returns the value of the plural-form instance variable dictated by {::model_class}.

@example

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

@return [Object]

# File lib/garden_variety/controller.rb, line 159
def collection
  instance_variable_get(:"@#{self.class.model_name.plural}")
end
collection=(values) click to toggle source

@!visibility public Sets the value of the plural-form instance variable dictated by {::model_class}.

@example

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

@param values [Object] @return [values]

# File lib/garden_variety/controller.rb, line 179
def collection=(values)
  instance_variable_set(:"@#{self.class.model_name.plural}", values)
end
find_collection() click to toggle source

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

@example

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

@return [ActiveRecord::Relation]

# File lib/garden_variety/controller.rb, line 196
def find_collection
  self.class.model_class.all
end
find_model() click to toggle source

@!visibility public 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.

@example

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

@return [ActiveRecord::Base]

# File lib/garden_variety/controller.rb, line 214
def find_model
  self.class.model_class.find(params[:id])
end
flash_message(status) click to toggle source

@!visibility public 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.

@example 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."

@param status [Symbol, String] @return [String]

# 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_options() click to toggle source

@!visibility public 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 {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 {www.rubydoc.info/gems/i18n I18n documentation} for more information.

@return [Hash]

# File lib/garden_variety/controller.rb, line 269
def flash_options
  { model_name: self.class.model_name.human }
end
model() click to toggle source

@!visibility public Returns the value of the singular-form instance variable dictated by {::model_class}.

@example

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

@return [Object]

# File lib/garden_variety/controller.rb, line 120
def model
  instance_variable_get(:"@#{self.class.model_name.singular}")
end
model=(value) click to toggle source

@!visibility public Sets the value of the singular-form instance variable dictated by {::model_class}.

@example

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

@param value [Object] @return [value]

# File lib/garden_variety/controller.rb, line 140
def model=(value)
  instance_variable_set(:"@#{self.class.model_name.singular}", value)
end
new_model() click to toggle source

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

@example

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

@return [ActiveRecord::Base]

# File lib/garden_variety/controller.rb, line 230
def new_model
  self.class.model_class.new
end