Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To facilitate making reusable chunks of actions and responses, simply…
… extend Ardes::ResponsesModule into your action modules. See Ardes::ResponsesModule for details
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |