Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 7264 Bytes
1 | require File.dirname(__FILE__) + "/../../spec_helper" |
2 | |
3 | describe "Radiant::ResourceResponses" do |
4 | before :each do |
5 | @klass = Class.new(ApplicationController) |
6 | @klass.extend Radiant::ResourceResponses |
7 | end |
8 | |
9 | describe "extending the controller" do |
10 | it "should add the responses method" do |
11 | @klass.should respond_to(:responses) |
12 | end |
13 | |
14 | it "should return a response collector" do |
15 | @klass.responses.should be_kind_of(Radiant::ResourceResponses::Collector) |
16 | end |
17 | |
18 | it "should yield the collector to the passed block" do |
19 | @klass.responses {|r| r.should be_kind_of(Radiant::ResourceResponses::Collector) } |
20 | end |
21 | |
22 | it "should add a response_for instance method" do |
23 | @klass.new.should respond_to(:response_for) |
24 | end |
25 | |
26 | it "should add a wrap instance method" do |
27 | @klass.new.should respond_to(:wrap) |
28 | end |
29 | |
30 | it "should duplicate on inheritance" do |
31 | @subclass = Class.new(@klass) |
32 | @subclass.responses.should_not equal(@klass.responses) |
33 | end |
34 | end |
35 | |
36 | describe "responding to configured formats" do |
37 | before :each do |
38 | @default = lambda { render :text => "Hello, world!" } |
39 | @klass.responses do |r| |
40 | r.plural.default(&@default) |
41 | end |
42 | @responder = mock('responder') |
43 | @instance = @klass.new |
44 | @instance.stub!(:respond_to).and_yield(@responder) |
45 | end |
46 | |
47 | describe "when wrapping a block" do |
48 | it "should evaluate the block in the context of the controller" do |
49 | @instance.send(:instance_variable_set, "@foo", "foo") |
50 | @instance.wrap(lambda { @foo }).call.should == 'foo' |
51 | end |
52 | end |
53 | |
54 | it "should apply the default block to the :any format" do |
55 | @instance.should_receive(:wrap).with(@default).and_return(@default) |
56 | @responder.should_receive(:any).with(&@default) |
57 | @instance.response_for(:plural) |
58 | end |
59 | |
60 | it "should apply the publish block to the published formats before the default format" do |
61 | @pblock = lambda { render :text => 'bar' } |
62 | @klass.responses.plural.publish(:xml, :json, &@pblock) |
63 | @instance.should_receive(:wrap).with(@pblock).twice.and_return(@pblock) |
64 | @instance.should_receive(:wrap).with(@default).and_return(@default) |
65 | @responder.should_receive(:xml).with(&@pblock).once.ordered |
66 | @responder.should_receive(:json).with(&@pblock).once.ordered |
67 | @responder.should_receive(:any).with(&@default).once.ordered |
68 | @instance.response_for(:plural) |
69 | end |
70 | |
71 | it "should apply custom formats before the published and default formats" do |
72 | @iblock = lambda { render :text => 'baz' } |
73 | @pblock = lambda { render :text => 'bar' } |
74 | @klass.responses.plural.iphone(&@iblock) |
75 | @klass.responses.plural.publish(:xml, &@pblock) |
76 | @instance.should_receive(:wrap).with(@iblock).and_return(@iblock) |
77 | @instance.should_receive(:wrap).with(@pblock).and_return(@pblock) |
78 | @instance.should_receive(:wrap).with(@default).and_return(@default) |
79 | @responder.should_receive(:iphone).with(&@iblock).once.ordered |
80 | @responder.should_receive(:xml).with(&@pblock).once.ordered |
81 | @responder.should_receive(:any).with(&@default).once.ordered |
82 | @instance.response_for(:plural) |
83 | end |
84 | |
85 | it "should apply the :any format when the default block is blank" do |
86 | @klass.responses.plural.send(:instance_variable_set, "@default", nil) |
87 | @responder.should_receive(:any).with(no_args()) |
88 | @instance.response_for(:plural) |
89 | end |
90 | |
91 | it "should apply a custom format when no block is given" do |
92 | @klass.responses.plural.iphone |
93 | @instance.should_receive(:wrap).with(@default).and_return(@default) |
94 | @responder.should_receive(:iphone) |
95 | @responder.should_receive(:any) |
96 | @instance.response_for(:plural) |
97 | end |
98 | end |
99 | end |
100 | |
101 | describe Radiant::ResourceResponses::Collector do |
102 | before :each do |
103 | @collector = Radiant::ResourceResponses::Collector.new |
104 | end |
105 | |
106 | it "should provide a Response object as the default property" do |
107 | @collector.plural.should be_kind_of(Radiant::ResourceResponses::Response) |
108 | end |
109 | |
110 | it "should be duplicable" do |
111 | @collector.should be_duplicable |
112 | end |
113 | |
114 | it "should duplicate its elements when duplicating" do |
115 | @collector.plural.html |
116 | @duplicate = @collector.dup |
117 | @collector.plural.should_not equal(@duplicate.plural) |
118 | end |
119 | end |
120 | |
121 | describe Radiant::ResourceResponses::Response do |
122 | before :each do |
123 | @response = Radiant::ResourceResponses::Response.new |
124 | end |
125 | |
126 | it "should duplicate its elements when duplicating" do |
127 | @response.default { render :text => "foo" } |
128 | @response.html |
129 | @response.publish(:xml) { render } |
130 | @duplicate = @response.dup |
131 | @response.blocks.should_not equal(@duplicate.blocks) |
132 | @response.default.should_not equal(@duplicate.default) |
133 | @response.publish_block.should_not equal(@duplicate.publish_block) |
134 | @response.publish_formats.should_not equal(@duplicate.publish_formats) |
135 | @response.block_order.should_not equal(@duplicate.block_order) |
136 | end |
137 | |
138 | it "should accept a default response block" do |
139 | @block = lambda { render :text => 'foo' } |
140 | @response.default(&@block) |
141 | @response.default.should == @block |
142 | end |
143 | |
144 | it "should accept a format symbol and block to publish" do |
145 | @block = lambda { render :xml => object } |
146 | @response.publish(:xml, &@block) |
147 | @response.publish_formats.should == [:xml] |
148 | @response.publish_block.should == @block |
149 | end |
150 | |
151 | it "should require a publish block if one is not already assigned" do |
152 | lambda do |
153 | @response.publish(:json) |
154 | end.should raise_error |
155 | end |
156 | |
157 | it "should accept multiple formats to publish" do |
158 | @response.publish(:xml, :json) { render format_symbol => object } |
159 | @response.publish_formats.should == [:xml, :json] |
160 | end |
161 | |
162 | it "should add a new format to publish" do |
163 | @response.publish(:xml) { render format_symbol => object } |
164 | @response.publish_formats.should == [:xml] |
165 | @response.publish(:json) |
166 | @response.publish_formats.should == [:xml, :json] |
167 | end |
168 | |
169 | it "should accept an arbitrary format block" do |
170 | @block = lambda { render :template => "foo" } |
171 | @response.iphone(&@block) |
172 | @response.blocks[:iphone].should == @block |
173 | end |
174 | |
175 | it "should accept an arbitrary format without a block" do |
176 | @response.iphone |
177 | @response.each_format.should == [:iphone] |
178 | end |
179 | |
180 | describe "prepared with some formats" do |
181 | before :each do |
182 | @responder = mock("responder") |
183 | @pblock = lambda { 'foo' } |
184 | @response.publish(:xml, :json, &@pblock) |
185 | @iblock = lambda { 'iphone' } |
186 | @popblock = lambda { 'popup' } |
187 | @response.iphone(&@iblock) |
188 | @response.popup(&@popblock) |
189 | end |
190 | |
191 | it "should iterate over the publish formats" do |
192 | @responder.should_receive(:xml).with(&@pblock).once.ordered |
193 | @responder.should_receive(:json).with(&@pblock).once.ordered |
194 | @response.each_published do |format, block| |
195 | @responder.send(format, &@block) |
196 | end |
197 | end |
198 | |
199 | it "should iterate over the regular formats" do |
200 | @responder.should_receive(:iphone).with(&@iblock).once.ordered |
201 | @responder.should_receive(:popup).with(&@popblock).once.ordered |
202 | @response.each_format do |format, block| |
203 | @responder.send(format, &@block) |
204 | end |
205 | end |
206 | end |
207 | end |