module GardenVariety::Controller::ClassMethods

Public Instance Methods

garden_variety(*actions) click to toggle source

Macro to include garden variety implementations of specified actions in the controller. If no actions are specified, all typical REST actions (index, show, new, create, edit, update, destroy) are included.

See also:

  • {GardenVariety::IndexAction}

  • {GardenVariety::ShowAction}

  • {GardenVariety::NewAction}

  • {GardenVariety::CreateAction}

  • {GardenVariety::EditAction}

  • {GardenVariety::UpdateAction}

  • {GardenVariety::DestroyAction}

@example default usage

# This...
class PostsController < ApplicationController
  garden_variety
end

# ...is equivalent to:
class PostsController < ApplicationController
  include GardenVariety::IndexAction
  include GardenVariety::ShowAction
  include GardenVariety::NewAction
  include GardenVariety::CreateAction
  include GardenVariety::EditAction
  include GardenVariety::UpdateAction
  include GardenVariety::DestroyAction
end

@example specific usage

# This...
class PostsController < ApplicationController
  garden_variety :index, :show
end

# ...is equivalent to:
class PostsController < ApplicationController
  include GardenVariety::IndexAction
  include GardenVariety::ShowAction
end

@param actions [Array<:index, :show, :new, :create, :edit, :update, :destroy>] @return [void] @raise [ArgumentError]

if an invalid action is specified
# File lib/garden_variety/controller.rb, line 55
def garden_variety(*actions)
  actions.each do |action|
    unless ::GardenVariety::ACTION_MODULES.key?(action)
      raise ArgumentError, "Invalid action: #{action.inspect}"
    end
  end

  action_modules = actions.empty? ?
    ::GardenVariety::ACTION_MODULES.values :
    ::GardenVariety::ACTION_MODULES.values_at(*actions)

  action_modules.each{|m| include m }
end
model_class() click to toggle source

Returns the controller model class. Defaults to a class corresponding to the singular-form of the controller name.

@example

class PostsController < ApplicationController
end

PostsController.model_class  # == Post (class)

@return [Class]

# File lib/garden_variety/controller.rb, line 79
def model_class
  @model_class ||= controller_path.classify.constantize
end
model_class=(klass) click to toggle source

Sets the controller model class.

@example

class PublishedPostsController < ApplicationController
  self.model_class = Post
end

@param klass [Class] @return [klass]

# File lib/garden_variety/controller.rb, line 92
def model_class=(klass)
  @model_name = nil
  @model_class = klass
end
model_name() click to toggle source

@!visibility private

# File lib/garden_variety/controller.rb, line 98
def model_name
  @model_name ||= model_class.try(:model_name) || ActiveModel::Name.new(model_class)
end