Skip to content

Commit

Permalink
To facilitate making reusable chunks of actions and responses, simply…
Browse files Browse the repository at this point in the history
… extend Ardes::ResponsesModule into

  your action modules.  See Ardes::ResponsesModule for details
  • Loading branch information
ianwhite committed Oct 10, 2008
1 parent c89bc63 commit e3135e6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,3 +1,6 @@
* To facilitate making reusable chunks of actions and responses, simply extend Ardes::ResponsesModule into
your action modules. See Ardes::ResponsesModule for details

* now intercepting template_exists? to decide whether to render a response for an action
that has no action method defined. (Removes somewhat mysterious behaviour of empty
actions being defined)
Expand Down
41 changes: 41 additions & 0 deletions lib/ardes/responses_module.rb
@@ -0,0 +1,41 @@
module Ardes#:nodoc:
# Extension to facilitate writing responses in mixins
#
# extend this into your own module to have it act as a response_for namespace
# when this module is included into a controller, the responses will be copied
# over, along with the actions.
#
# Example:
#
# module MyActions
# extend Ardes::ResponsesModule
#
# def foo
# do_foo
# end
#
# response_for :foo do |format|
# format.html { # do a response }
# end
# end
#
# class AController < ApplicationController
# include MyActions
# # now this controller has foo and response_for :foo
# end
module ResponsesModule
include ResponseFor::ClassMethods

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
end
alias_method_chain :included, :responses
end
end
end
end
27 changes: 27 additions & 0 deletions spec/controllers/responses_module_spec.rb
@@ -0,0 +1,27 @@
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))

module ResponsesModuleSpec
module MyActionsAndResponses
extend Ardes::ResponsesModule

def foo; end

response_for :foo do |format|
format.html {}
end
end

class MyController < ActionController::Base
include MyActionsAndResponses
end

describe MyController do
it "should have action_response for :foo" do
@controller.class.action_responses['foo'].should_not == nil
end

it "should have action :foo" do
@controller.should respond_to(:foo)
end
end
end

0 comments on commit e3135e6

Please sign in to comment.