diff --git a/CHANGELOG b/CHANGELOG index 3a30cc4..82d745a 100644 --- a/CHANGELOG +++ b/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) diff --git a/lib/ardes/responses_module.rb b/lib/ardes/responses_module.rb new file mode 100644 index 0000000..0cf6029 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/spec/controllers/responses_module_spec.rb b/spec/controllers/responses_module_spec.rb new file mode 100644 index 0000000..4404057 --- /dev/null +++ b/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