Skip to content

Commit

Permalink
Added note about placement of extend Ardes::ResponsesModule, also mov…
Browse files Browse the repository at this point in the history
…ed some code into response_for where it belongs
  • Loading branch information
ianwhite committed Oct 10, 2008
1 parent e3135e6 commit 06da19d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/ardes/response_for.rb
Expand Up @@ -103,6 +103,14 @@ def action_responses
instance_variable_get('@action_responses') || instance_variable_set('@action_responses', copy_of_each_of_superclass_action_responses)
end

# takes any responses from the argument (a controller, or responses module) and adds them to this controller's responses
def include_responses_from(responses_container)
responses_container.action_responses.each do |action, responses|
action_responses[action] ||= []
action_responses[action].unshift(responses)
end
end

private
def copy_of_each_of_superclass_action_responses
(superclass.action_responses rescue {}).inject({}){|m,(k,v)| m.merge(k => v.dup)}
Expand Down
9 changes: 5 additions & 4 deletions lib/ardes/responses_module.rb
Expand Up @@ -5,6 +5,9 @@ module Ardes#:nodoc:
# when this module is included into a controller, the responses will be copied
# over, along with the actions.
#
# NOTE: If you are defining self.included on your module, make sure you put the
# extend Ardes::ResponsesModule *after* self.included method definition.
#
# Example:
#
# module MyActions
Expand All @@ -29,10 +32,8 @@ module ResponsesModule
def self.extended(mixin)
class << mixin
def included_with_responses(controller_class)
action_responses.each do |action, responses|
controller_class.action_responses[action] ||= []
controller_class.action_responses[action].unshift(responses)
end
controller_class.include_responses_from(self)
included_without_responses(controller_class)
end
alias_method_chain :included, :responses
end
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/responses_module_spec.rb
Expand Up @@ -2,6 +2,13 @@

module ResponsesModuleSpec
module MyActionsAndResponses

def self.included(controller)
controller.class_eval do
def defined_by_included; end
end
end

extend Ardes::ResponsesModule

def foo; end
Expand All @@ -23,5 +30,9 @@ class MyController < ActionController::Base
it "should have action :foo" do
@controller.should respond_to(:foo)
end

it "should have method defined by included" do
@controller.should respond_to(:defined_by_included)
end
end
end

0 comments on commit 06da19d

Please sign in to comment.