Module: GardenVariety::Controller::ClassMethods

Defined in:
lib/garden_variety/controller.rb

Instance Method Summary collapse

Instance Method Details

#garden_variety(*actions) ⇒ void

This method returns an undefined value.

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:

Examples:

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

specific usage

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

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

Parameters:

  • actions (Array<:index, :show, :new, :create, :edit, :update, :destroy>)

Raises:

  • (ArgumentError)

    if an invalid action is specified



55
56
57
58
59
60
61
62
63
64
65
66
67
# 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_classClass

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

Examples:

class PostsController < ApplicationController
end

PostsController.model_class  # == Post (class)

Returns:

  • (Class)


79
80
81
# File 'lib/garden_variety/controller.rb', line 79

def model_class
  @model_class ||= controller_path.classify.constantize
end

#model_class=(klass) ⇒ klass

Sets the controller model class.

Examples:

class PublishedPostsController < ApplicationController
  self.model_class = Post
end

Parameters:

  • klass (Class)

Returns:

  • (klass)


92
93
94
95
# File 'lib/garden_variety/controller.rb', line 92

def model_class=(klass)
  @model_name = nil
  @model_class = klass
end