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

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.

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)


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

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)


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.

Examples:

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

Parameters:

  • values (Object)

Returns:

  • (values)


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

Returns an ActiveRecord::Relation representing instances of ::model_class. 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)


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

def find_collection
  self.class.model_class.all
end

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

Examples:

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

Returns:

  • (ActiveRecord::Base)

Raises:

  • (ActiveRecord::RecordNotFound)

    if a model instance with matching :id cannot be found



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.

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)


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

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, **flash_options)
end

#flash_optionsHash{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.

Returns:

  • (Hash{Symbol => #to_s})


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

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)


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.

Examples:

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

Parameters:

  • value (Object)

Returns:

  • (value)


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

Returns a new instance of ::model_class. Designed for use in generic new and create action methods.

Examples:

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

Returns:

  • (ActiveRecord::Base)


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

def new_model
  self.class.model_class.new
end