exposure 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest.txt +41 -0
- data/README.mdown +39 -0
- data/Rakefile +26 -0
- data/exposure.gemspec +31 -0
- data/lib/exposure/common.rb +127 -0
- data/lib/exposure/configuration.rb +63 -0
- data/lib/exposure/patterns/resource.rb +226 -0
- data/lib/exposure/patterns/resources.rb +323 -0
- data/lib/exposure.rb +14 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/custom_matchers.rb +41 -0
- data/spec/exposure_spec.rb +4 -0
- data/spec/factories/pirate_factory.rb +8 -0
- data/spec/factories/ship_factory.rb +3 -0
- data/spec/finders/finder_spec.rb +53 -0
- data/spec/finders/nested_resources_finder_spec.rb +29 -0
- data/spec/fixtures/pirates/edit.erb +0 -0
- data/spec/fixtures/pirates/index.erb +0 -0
- data/spec/fixtures/pirates/new.erb +0 -0
- data/spec/fixtures/pirates/show.erb +0 -0
- data/spec/fixtures/ships/edit.erb +0 -0
- data/spec/fixtures/ships/index.erb +0 -0
- data/spec/fixtures/ships/new.erb +0 -0
- data/spec/fixtures/ships/show.erb +0 -0
- data/spec/flashers/flash_with_block_spec.rb +32 -0
- data/spec/flashers/flash_with_method_spec.rb +86 -0
- data/spec/flashers/flash_with_proc_spec.rb +83 -0
- data/spec/flashers/flash_with_string_spec.rb +82 -0
- data/spec/resource_spec.rb +192 -0
- data/spec/resources_spec.rb +204 -0
- data/spec/responders/respond_to_mime_types_spec.rb +44 -0
- data/spec/responders/respond_with_block_spec.rb +84 -0
- data/spec/responders/respond_with_method_spec.rb +84 -0
- data/spec/responders/respond_with_proc_spec.rb +79 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +71 -0
- data/spec/spec_rails_helper.rb +22 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/shoulda.rake +15 -0
- metadata +106 -0
@@ -0,0 +1,323 @@
|
|
1
|
+
module Exposure
|
2
|
+
module Patterns
|
3
|
+
module Resources
|
4
|
+
def self.extended(base)
|
5
|
+
base::const_set(:DefaultResponses, DefaultResponses)
|
6
|
+
base::const_set(:DefaultFlashMessages, DefaultFlashMessages)
|
7
|
+
base::const_set(:Finders, { true => {}, false => {} })
|
8
|
+
base::const_set(:FlashMessages, { true => {}, false => {} })
|
9
|
+
base::const_set(:Responses, {} )
|
10
|
+
end
|
11
|
+
|
12
|
+
Callbacks = %w(
|
13
|
+
before_find after_find after_find_on_failure after_find_on_success
|
14
|
+
before_find_many after_find_many after_find_many_on_failure after_find_many_on_success
|
15
|
+
before_assign after_assign
|
16
|
+
before_save after_save after_save_on_failure after_save_on_success
|
17
|
+
before_create after_create_on_failure after_create_on_success
|
18
|
+
before_update after_update_on_failure after_update_on_success
|
19
|
+
before_destroy after_destroy_on_success
|
20
|
+
before_response before_response_on_success before_response_on_failure
|
21
|
+
)
|
22
|
+
|
23
|
+
DefaultFlashMessages = {
|
24
|
+
true => {
|
25
|
+
:create => Proc.new { "#{resource_name.capitalize} successfully created" },
|
26
|
+
:update => Proc.new { "#{resource_name.capitalize} successfully updated" },
|
27
|
+
:destroy => Proc.new { "#{resource_name.capitalize} successfully removed" }
|
28
|
+
},
|
29
|
+
false => {}
|
30
|
+
}
|
31
|
+
|
32
|
+
DefaultResponses = {
|
33
|
+
'index.success.html' => Proc.new { render('index') },
|
34
|
+
'show.success.html' => Proc.new { render('show') },
|
35
|
+
'new.success.html' => Proc.new { render('new') },
|
36
|
+
'create.success.html' => Proc.new { redirect_to({:action => 'index'}) },
|
37
|
+
'edit.success.html' => Proc.new { render('edit') },
|
38
|
+
'update.success.html' => Proc.new { redirect_to({:action => 'show' }) },
|
39
|
+
'destroy.success.html'=> Proc.new { redirect_to({:action => 'index'}) },
|
40
|
+
'create.failure.html' => Proc.new { render('new') },
|
41
|
+
'update.failure.html' => Proc.new { render('edit') },
|
42
|
+
'index.success.xml' => Proc.new { render(:xml => @resources) },
|
43
|
+
'show.success.xml' => Proc.new { render(:xml => @resource) },
|
44
|
+
'new.success.xml' => Proc.new { render(:xml => @resource) },
|
45
|
+
'create.success.xml' => Proc.new { render({:xml => @resource, :status => :created, :location => @resource}) },
|
46
|
+
'created.failure.xml'=> Proc.new { render(:xml => @resource.errors, :status => :unprocessable_entity)},
|
47
|
+
'update.success.xml' => Proc.new { head(:ok)},
|
48
|
+
'update.failure.xml' => Proc.new { render(:xml => @resource.errors, :status => :unprocessable_entity)},
|
49
|
+
'destroy.success.xml'=> Proc.new { head(:ok)}
|
50
|
+
}
|
51
|
+
|
52
|
+
module Actions
|
53
|
+
def index
|
54
|
+
run_callbacks(:before_find_many)
|
55
|
+
if find_records
|
56
|
+
run_callbacks(:after_find_many_on_success)
|
57
|
+
run_callbacks(:after_find_many)
|
58
|
+
run_callbacks(:before_response)
|
59
|
+
run_callbacks(:before_response_on_success)
|
60
|
+
response_for(:index, :success, request.format.to_sym)
|
61
|
+
else
|
62
|
+
run_callbacks(:after_find_many_on_failure)
|
63
|
+
run_callbacks(:after_find_many)
|
64
|
+
run_callbacks(:before_response)
|
65
|
+
run_callbacks(:before_response_on_failure)
|
66
|
+
response_for(:index, :failure, request.format.to_sym)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def show
|
71
|
+
run_callbacks(:before_find)
|
72
|
+
if find_record
|
73
|
+
run_callbacks(:after_find_on_success)
|
74
|
+
run_callbacks(:after_find)
|
75
|
+
run_callbacks(:before_response)
|
76
|
+
run_callbacks(:before_response_on_success)
|
77
|
+
response_for(:show, :success, request.format.to_sym)
|
78
|
+
else
|
79
|
+
run_callbacks(:after_find_on_failure)
|
80
|
+
run_callbacks(:after_find)
|
81
|
+
run_callbacks(:before_response)
|
82
|
+
run_callbacks(:before_response_on_failure)
|
83
|
+
response_for(:show, :failure, request.format.to_sym)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def new
|
88
|
+
run_callbacks(:before_assign)
|
89
|
+
new_record
|
90
|
+
run_callbacks(:after_assign)
|
91
|
+
run_callbacks(:before_response)
|
92
|
+
run_callbacks(:before_response_on_success)
|
93
|
+
response_for(:new, :success, request.format.to_sym)
|
94
|
+
end
|
95
|
+
|
96
|
+
def create
|
97
|
+
run_callbacks(:before_assign)
|
98
|
+
new_record
|
99
|
+
run_callbacks(:after_assign)
|
100
|
+
|
101
|
+
run_callbacks(:before_create)
|
102
|
+
run_callbacks(:before_save)
|
103
|
+
|
104
|
+
if save_record
|
105
|
+
run_callbacks(:after_save_on_success)
|
106
|
+
run_callbacks(:after_create_on_success)
|
107
|
+
run_callbacks(:before_response)
|
108
|
+
run_callbacks(:before_response_on_success)
|
109
|
+
flash_for(:create, true)
|
110
|
+
response_for(:create, :success, request.format.to_sym)
|
111
|
+
else
|
112
|
+
run_callbacks(:after_save_on_failure)
|
113
|
+
run_callbacks(:after_create_on_failure)
|
114
|
+
run_callbacks(:before_response)
|
115
|
+
run_callbacks(:before_response_on_failure)
|
116
|
+
flash_for(:create, false)
|
117
|
+
response_for(:create, :failure, request.format.to_sym)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
def edit
|
123
|
+
run_callbacks(:before_find)
|
124
|
+
if find_record
|
125
|
+
run_callbacks(:after_find_on_success)
|
126
|
+
run_callbacks(:after_find)
|
127
|
+
run_callbacks(:before_response)
|
128
|
+
run_callbacks(:before_response_on_success)
|
129
|
+
response_for(:edit, :success, request.format.to_sym)
|
130
|
+
else
|
131
|
+
run_callbacks(:after_find_on_failure)
|
132
|
+
run_callbacks(:after_find)
|
133
|
+
run_callbacks(:before_response)
|
134
|
+
run_callbacks(:before_response_on_failure)
|
135
|
+
response_for(:edit, :failure, request.format.to_sym)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def update
|
140
|
+
run_callbacks(:before_find)
|
141
|
+
if find_record
|
142
|
+
run_callbacks(:after_find_on_success)
|
143
|
+
run_callbacks(:after_find)
|
144
|
+
if update_record
|
145
|
+
run_callbacks(:after_save_on_success)
|
146
|
+
run_callbacks(:after_update_on_success)
|
147
|
+
run_callbacks(:before_response)
|
148
|
+
run_callbacks(:before_response_on_success)
|
149
|
+
flash_for(:update, true)
|
150
|
+
response_for(:update, :success, request.format.to_sym)
|
151
|
+
else
|
152
|
+
run_callbacks(:after_save_on_failure)
|
153
|
+
run_callbacks(:after_create_on_failure)
|
154
|
+
run_callbacks(:before_response)
|
155
|
+
run_callbacks(:before_response_on_failure)
|
156
|
+
flash_for(:update, false)
|
157
|
+
response_for(:update, :failure, request.format.to_sym)
|
158
|
+
end
|
159
|
+
else
|
160
|
+
run_callbacks(:after_find_on_failure)
|
161
|
+
run_callbacks(:after_find)
|
162
|
+
run_callbacks(:before_response)
|
163
|
+
run_callbacks(:before_response_on_failure)
|
164
|
+
response_for(:edit, :failure, request.format.to_sym)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def destroy
|
169
|
+
run_callbacks(:before_find)
|
170
|
+
if find_record
|
171
|
+
run_callbacks(:after_find_on_success)
|
172
|
+
run_callbacks(:after_find)
|
173
|
+
run_callbacks(:before_destroy)
|
174
|
+
|
175
|
+
delete_record
|
176
|
+
|
177
|
+
run_callbacks(:after_destroy_on_success)
|
178
|
+
run_callbacks(:before_response)
|
179
|
+
run_callbacks(:before_response_on_success)
|
180
|
+
flash_for(:destroy, true)
|
181
|
+
response_for(:destroy, :success, request.format.to_sym)
|
182
|
+
|
183
|
+
else
|
184
|
+
run_callbacks(:after_find_on_failure)
|
185
|
+
run_callbacks(:after_find)
|
186
|
+
response_for(:destroy, :failure, request.format.to_sym)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
def custom_response_for(action_name, action_status, format)
|
192
|
+
if responder = self.class::Responses["#{action_name}.#{action_status}.#{format}"]
|
193
|
+
case responder
|
194
|
+
when Symbol
|
195
|
+
self.send(responder)
|
196
|
+
when Proc
|
197
|
+
self.instance_eval &responder
|
198
|
+
end
|
199
|
+
else
|
200
|
+
false
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def default_response_for(action_name, action_status, format)
|
205
|
+
if responder = self.class::DefaultResponses["#{action_name}.#{action_status}.#{format}"]
|
206
|
+
self.instance_eval &responder
|
207
|
+
else
|
208
|
+
return false
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def response_for(action_name, action_status, format = :html)
|
213
|
+
format = :html if format == :all
|
214
|
+
custom_response_for(action_name, action_status, format) || default_response_for(action_name, action_status, format) || head(:not_acceptable)
|
215
|
+
end
|
216
|
+
|
217
|
+
def custom_flash_for(action_name, action_successful)
|
218
|
+
if flash_message = self.class::FlashMessages[action_successful][action_name]
|
219
|
+
case flash_message
|
220
|
+
when String
|
221
|
+
flash[:message] = flash_message
|
222
|
+
when Symbol
|
223
|
+
flash[:message] = self.send(flash_message)
|
224
|
+
when Proc
|
225
|
+
flash[:message] = self.instance_eval(&flash_message)
|
226
|
+
end
|
227
|
+
else
|
228
|
+
false
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def default_flash_for(action_name, action_successful)
|
233
|
+
if message_proc = self.class::DefaultFlashMessages[action_successful][action_name]
|
234
|
+
flash[:message] = self.instance_eval(&message_proc)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def flash_for(action_name, action_successful)
|
239
|
+
custom_flash_for(action_name, action_successful) || default_flash_for(action_name, action_successful)
|
240
|
+
end
|
241
|
+
|
242
|
+
def custom_finder_for(resource_name)
|
243
|
+
if finder = self.class::Finders[resource_name]
|
244
|
+
return finder
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def default_finder_for(resource_name)
|
249
|
+
if finder = self.class::DefaultFinders[resource_name]
|
250
|
+
return finder
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def finder_for(resource_name)
|
255
|
+
custom_finder_for(resource_name) || default_finder_for(resource_name)
|
256
|
+
end
|
257
|
+
|
258
|
+
def call_finder_chain(object, chain, use_associaiton = true)
|
259
|
+
links = chain.shift
|
260
|
+
return object unless links
|
261
|
+
|
262
|
+
message = finder_for(links[0])
|
263
|
+
association = links[1] if use_associaiton
|
264
|
+
|
265
|
+
case message
|
266
|
+
when Symbol
|
267
|
+
value = self.send(message)
|
268
|
+
when Proc
|
269
|
+
value = self.instance_eval(&message)
|
270
|
+
else
|
271
|
+
raise "invalid finder of #{message.inspect}"
|
272
|
+
end
|
273
|
+
|
274
|
+
if value.kind_of?(Array) && !value.respond_to?(:proxy_target)
|
275
|
+
if use_associaiton
|
276
|
+
call_finder_chain(object.send(association).send(*value), chain)
|
277
|
+
else
|
278
|
+
call_finder_chain(object.send(*value), chain)
|
279
|
+
end
|
280
|
+
else
|
281
|
+
call_finder_chain(value, chain)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def builder_for(resource_name)
|
286
|
+
custom_builder_for(resource_name) || default_builder_for(resource_name)
|
287
|
+
end
|
288
|
+
|
289
|
+
def resource_name
|
290
|
+
self.class.resource_name
|
291
|
+
end
|
292
|
+
|
293
|
+
def resources_name
|
294
|
+
self.class.resources_name
|
295
|
+
end
|
296
|
+
|
297
|
+
def save_record
|
298
|
+
@resource.save
|
299
|
+
end
|
300
|
+
|
301
|
+
def update_record
|
302
|
+
@resource.update_attributes(params[resource_name])
|
303
|
+
end
|
304
|
+
|
305
|
+
def new_record
|
306
|
+
@resource = instance_variable_set("@#{resource_name}", self.class.parent_model.new(params[resource_name]))
|
307
|
+
end
|
308
|
+
|
309
|
+
def find_record
|
310
|
+
@resource = instance_variable_set("@#{resource_name}", call_finder_chain(self.class.parent_model, self.class.member_nesting.clone, false))
|
311
|
+
end
|
312
|
+
|
313
|
+
def find_records
|
314
|
+
@resources = instance_variable_set("@#{resources_name}", call_finder_chain(self.class.parent_model, self.class.collection_nesting.clone, false))
|
315
|
+
end
|
316
|
+
|
317
|
+
def delete_record
|
318
|
+
@resource.destroy
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
data/lib/exposure.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'exposure/configuration'
|
5
|
+
require 'exposure/common'
|
6
|
+
require 'exposure/patterns/resources'
|
7
|
+
require 'exposure/patterns/resource'
|
8
|
+
|
9
|
+
module Exposure
|
10
|
+
VERSION = '0.0.4'
|
11
|
+
def self.included(base)
|
12
|
+
base.extend Configuration
|
13
|
+
end
|
14
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/exposure.rb'}"
|
9
|
+
puts "Loading exposure gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Shoulda
|
2
|
+
module ActionController
|
3
|
+
module Matchers
|
4
|
+
class RespondWithMatcher
|
5
|
+
def to(where)
|
6
|
+
@location = case where
|
7
|
+
when Symbol
|
8
|
+
where == :back ? where : {:action => where.to_s}
|
9
|
+
else
|
10
|
+
where
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
description = "respond with #{@status}"
|
17
|
+
description << " to #{@location.inspect}" unless @location.nil?
|
18
|
+
description
|
19
|
+
end
|
20
|
+
|
21
|
+
def matches?(controller)
|
22
|
+
@controller = controller
|
23
|
+
(correct_status_code? || correct_status_code_range?) && correct_redirect_location
|
24
|
+
end
|
25
|
+
|
26
|
+
def expectation
|
27
|
+
expectation = "response to be a #{@status},"
|
28
|
+
expectation << " redirecting to #{@location.inspect}, " if @location
|
29
|
+
expectation << " but was #{response_code}"
|
30
|
+
expectation << " redirected to #{@controller.response.redirected_to.inspect}" if @location
|
31
|
+
expectation
|
32
|
+
end
|
33
|
+
private
|
34
|
+
def correct_redirect_location
|
35
|
+
return true unless @location
|
36
|
+
@controller.response.redirected_to == @location
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "finders", :type => :controller do
|
4
|
+
class PiratesController < ActionController::Base
|
5
|
+
expose_many(:pirates)
|
6
|
+
private
|
7
|
+
def find_pirate
|
8
|
+
Pirate.find_by_title(params[:id])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
controller_name :pirates
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@controller = PiratesController.new
|
16
|
+
@request = ActionController::TestRequest.new
|
17
|
+
@response = ActionController::TestResponse.new
|
18
|
+
ActionController::Routing::Routes.draw do |map|
|
19
|
+
map.resources :pirates
|
20
|
+
end
|
21
|
+
|
22
|
+
@pirate = Factory.stub(:pirate)
|
23
|
+
Pirate.stub(:find_by_title => @pirate)
|
24
|
+
end
|
25
|
+
|
26
|
+
after(:each) do
|
27
|
+
PiratesController::Finders.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
it "finds with a method name as symbol" do
|
31
|
+
PiratesController.find :pirate, :with => Proc.new { Pirate.find_by_title(params[:id]) }
|
32
|
+
get(:show, {:id => 'Captain'})
|
33
|
+
|
34
|
+
should assign_to(:pirate).with(@pirate)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "finds with a proc" do
|
38
|
+
PiratesController.find :pirate, :with => :find_pirate
|
39
|
+
get(:show, {:id => 'Captain'})
|
40
|
+
|
41
|
+
should assign_to(:pirate).with(@pirate)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "finds with a block" do
|
45
|
+
PiratesController.find :pirate do
|
46
|
+
Pirate.find_by_title(params[:id])
|
47
|
+
end
|
48
|
+
|
49
|
+
get(:show, {:id => 'Captain'})
|
50
|
+
|
51
|
+
should assign_to(:pirate).with(@pirate)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "nested finders", :type => :controller do
|
4
|
+
class ShipsController < ActionController::Base
|
5
|
+
expose_many(:ships, :nested => [:pirates])
|
6
|
+
end
|
7
|
+
|
8
|
+
controller_name :ships
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
@controller = ShipsController.new
|
12
|
+
@request = ActionController::TestRequest.new
|
13
|
+
@response = ActionController::TestResponse.new
|
14
|
+
ActionController::Routing::Routes.draw do |map|
|
15
|
+
map.resources :pirates do |pirate|
|
16
|
+
pirate.resources :ships
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
@pirate = Factory.create(:pirate_with_ships)
|
21
|
+
Pirate.stub(:find => @pirate)
|
22
|
+
|
23
|
+
get(:index, {:pirate_id => 1})
|
24
|
+
end
|
25
|
+
|
26
|
+
it { should assign_to(:ships) }
|
27
|
+
it { should assign_to(:resources) }
|
28
|
+
|
29
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "flash messages with blocks", :type => :controller do
|
4
|
+
class PiratesController < ActionController::Base
|
5
|
+
expose_many(:pirates)
|
6
|
+
PiratesController.flash_for :create do
|
7
|
+
"the flash is set to #{@pirate.title}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
controller_name :pirates
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
@controller = PiratesController.new
|
15
|
+
@request = ActionController::TestRequest.new
|
16
|
+
@response = ActionController::TestResponse.new
|
17
|
+
ActionController::Routing::Routes.draw do |map|
|
18
|
+
map.resources :pirates, :collection => {:test => :any}
|
19
|
+
end
|
20
|
+
|
21
|
+
@pirate = Factory.stub(:pirate, {:title => 'Captain'})
|
22
|
+
Pirate.stub(:new => @pirate)
|
23
|
+
|
24
|
+
@pirate.stub(:save => true)
|
25
|
+
|
26
|
+
@custom_flash_message = "the flash is set to #{@pirate.title}"
|
27
|
+
|
28
|
+
post(:create)
|
29
|
+
end
|
30
|
+
|
31
|
+
it { should set_the_flash.to(@custom_flash_message) }
|
32
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "flash messages with methods", :type => :controller do
|
4
|
+
class PiratesController < ActionController::Base
|
5
|
+
expose_many(:pirates)
|
6
|
+
private
|
7
|
+
def custom_flash_message
|
8
|
+
'the flash was set'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
controller_name :pirates
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@controller = PiratesController.new
|
16
|
+
@request = ActionController::TestRequest.new
|
17
|
+
@response = ActionController::TestResponse.new
|
18
|
+
ActionController::Routing::Routes.draw do |map|
|
19
|
+
map.resources :pirates, :collection => {:test => :any}
|
20
|
+
end
|
21
|
+
|
22
|
+
@custom_flash_message = 'the flash was set'
|
23
|
+
|
24
|
+
@pirate = Factory.stub(:pirate)
|
25
|
+
Pirate.stub(:new => @pirate)
|
26
|
+
end
|
27
|
+
|
28
|
+
after(:each) do
|
29
|
+
PiratesController::FlashMessages[true].clear
|
30
|
+
PiratesController::FlashMessages[false].clear
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "responding with a method call" do
|
34
|
+
before(:each) do
|
35
|
+
PiratesController.flash_for :create, :is => :custom_flash_message
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should respond with redirect to test on success" do
|
39
|
+
@pirate.stub(:save => true)
|
40
|
+
post(:create)
|
41
|
+
should set_the_flash.to(@custom_flash_message)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should respond with redirect to test on failure" do
|
45
|
+
@pirate.stub(:save => false)
|
46
|
+
post(:create)
|
47
|
+
should set_the_flash.to(@custom_flash_message)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "responding with a method call :on => :success" do
|
52
|
+
before(:each) do
|
53
|
+
PiratesController.flash_for :create, :is => :custom_flash_message, :on => :success
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should respond with custom response on success" do
|
57
|
+
@pirate.stub(:save => true)
|
58
|
+
post(:create)
|
59
|
+
should set_the_flash.to(@custom_flash_message)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not respond with custom response on failure" do
|
63
|
+
@pirate.stub(:save => false)
|
64
|
+
post(:create)
|
65
|
+
should_not redirect_to({:action => 'test'})
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "responding with a method call :on => :failure" do
|
70
|
+
before(:each) do
|
71
|
+
PiratesController.flash_for :create, :is => :custom_flash_message, :on => :failure
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not respond with custom response on success" do
|
75
|
+
@pirate.stub(:save => true)
|
76
|
+
post(:create)
|
77
|
+
should_not redirect_to({:action => 'test'})
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should respond with custom response on failure" do
|
81
|
+
@pirate.stub(:save => false)
|
82
|
+
post(:create)
|
83
|
+
should set_the_flash.to(@custom_flash_message)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|