josevalim-inherited_resources 0.9.0 → 0.9.1
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/CHANGELOG +6 -0
- data/{README → README.rdoc} +20 -36
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/inherited_resources/base.rb +1 -1
- data/lib/inherited_resources/base_helpers.rb +11 -5
- data/lib/inherited_resources/class_methods.rb +9 -4
- data/lib/inherited_resources/dsl.rb +26 -0
- data/lib/inherited_resources/has_scope_helpers.rb +1 -1
- data/lib/inherited_resources/legacy/respond_to.rb +27 -22
- data/lib/inherited_resources/legacy/responder.rb +26 -7
- data/lib/inherited_resources/url_helpers.rb +12 -6
- data/test/association_chain_test.rb +125 -0
- data/test/class_methods_test.rb +2 -2
- data/test/has_scope_test.rb +1 -1
- data/test/url_helpers_test.rb +66 -0
- metadata +9 -7
- data/test/base_helpers_test.rb +0 -77
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# Version 0.9
|
2
|
+
|
3
|
+
* Backported ActionController::Responder from Rails 3;
|
4
|
+
* Added parent_url helper;
|
5
|
+
* Added association_chain helper (as suggested by http://github.com/emmanuel);
|
6
|
+
|
1
7
|
# Version 0.8
|
2
8
|
|
3
9
|
* Fixed a small bug on optional belongs to with namespaced controllers.
|
data/{README → README.rdoc}
RENAMED
@@ -1,13 +1,8 @@
|
|
1
1
|
Inherited Resources
|
2
2
|
License: MIT
|
3
|
-
Version: 0.9.
|
3
|
+
Version: 0.9.1
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
http://wiki.github.com/josevalim/inherited_resources
|
8
|
-
|
9
|
-
Description
|
10
|
-
-----------
|
5
|
+
== Description
|
11
6
|
|
12
7
|
Inherited Resources speeds up development by making your controllers inherit
|
13
8
|
all restful actions so you just have to focus on what is important. It makes
|
@@ -20,8 +15,7 @@ Inherited Resources is tested and compatible with Rails 2.2.x and Rails 2.3.x.
|
|
20
15
|
|
21
16
|
keywords: resources, controller, singleton, belongs_to, polymorphic, named_scope and I18n
|
22
17
|
|
23
|
-
Installation
|
24
|
-
------------
|
18
|
+
== Installation
|
25
19
|
|
26
20
|
Install Inherited Resources is very easy. It is stored in GitHub, so just run
|
27
21
|
the following:
|
@@ -33,8 +27,7 @@ If you want it as plugin, just do:
|
|
33
27
|
|
34
28
|
script/plugin install git://github.com/josevalim/inherited_resources.git
|
35
29
|
|
36
|
-
Basic Usage
|
37
|
-
-----------
|
30
|
+
== Basic Usage
|
38
31
|
|
39
32
|
To use Inherited Resources you just have to inherit (duh) it:
|
40
33
|
|
@@ -92,8 +85,7 @@ call inherit_resources in your controller class scope:
|
|
92
85
|
inherit_resources
|
93
86
|
end
|
94
87
|
|
95
|
-
Overwriting defaults
|
96
|
-
--------------------
|
88
|
+
== Overwriting defaults
|
97
89
|
|
98
90
|
Whenever you inherit from InheritedResources, several defaults are assumed.
|
99
91
|
For example you can have an AccountsController to account management while the
|
@@ -146,8 +138,7 @@ You can deal with it just doing:
|
|
146
138
|
end
|
147
139
|
end
|
148
140
|
|
149
|
-
Overwriting actions
|
150
|
-
-------------------
|
141
|
+
== Overwriting actions
|
151
142
|
|
152
143
|
Let's suppose that after destroying a project you want to redirect to your
|
153
144
|
root url instead of redirecting to projects url. You just have to do:
|
@@ -228,8 +219,7 @@ give a block that expects two arguments, the first will be executed only in
|
|
228
219
|
success scenarios and the second in failure scenarios. You keep everything
|
229
220
|
clean and organized inside the same action.
|
230
221
|
|
231
|
-
Some DSL
|
232
|
-
--------
|
222
|
+
== Some DSL
|
233
223
|
|
234
224
|
For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
|
235
225
|
your success/failure blocks straight from your class binding. For it, you just
|
@@ -247,8 +237,7 @@ And then you can rewrite the last example as:
|
|
247
237
|
end
|
248
238
|
end
|
249
239
|
|
250
|
-
Flash messages and I18n
|
251
|
-
-----------------------
|
240
|
+
== Flash messages and I18n
|
252
241
|
|
253
242
|
Flash messages are powered by I18n api. It checks for messages in the following
|
254
243
|
order:
|
@@ -311,8 +300,7 @@ the messages will be checked in the following order:
|
|
311
300
|
flash.projects.create.notice
|
312
301
|
flash.actions.create.notice
|
313
302
|
|
314
|
-
Has Scope
|
315
|
-
---------
|
303
|
+
== Has Scope
|
316
304
|
|
317
305
|
InheritedResources tries to integrate nicely with your model. In order to do so,
|
318
306
|
it also is named_scope fluent. Let's suppose our Project model with the scopes:
|
@@ -352,8 +340,7 @@ per page. In such cases, you can give a proc as default value:
|
|
352
340
|
|
353
341
|
has_scope :limit, :default => proc{|c| c.session[:limit] || 10 }
|
354
342
|
|
355
|
-
Belongs to
|
356
|
-
----------
|
343
|
+
== Belongs to
|
357
344
|
|
358
345
|
Finally, our Projects are going to get some Tasks. Then you create a
|
359
346
|
TasksController and do:
|
@@ -373,8 +360,7 @@ customize how InheritedResources find your projects:
|
|
373
360
|
It also accepts :route_name, :parent_class and :instance_name as options.
|
374
361
|
Check the lib/inherited_resources/class_methods.rb for more.
|
375
362
|
|
376
|
-
Nested belongs to
|
377
|
-
-----------------
|
363
|
+
== Nested belongs to
|
378
364
|
|
379
365
|
Now, our Tasks get some Comments and you need to nest even deeper. Good
|
380
366
|
practices says that you should never nest more than two resources, but sometimes
|
@@ -401,8 +387,7 @@ Warning: calling several belongs_to is the same as nesting them:
|
|
401
387
|
|
402
388
|
In other words, the code above is the same as calling nested_belongs_to.
|
403
389
|
|
404
|
-
Polymorphic belongs to
|
405
|
-
----------------------
|
390
|
+
== Polymorphic belongs to
|
406
391
|
|
407
392
|
We can go even further. Let's suppose our Projects can now have Files, Messages
|
408
393
|
and Tasks, and they are all commentable. In this case, the best solution is to
|
@@ -434,8 +419,7 @@ When using polymorphic associations, you get some free helpers:
|
|
434
419
|
parent_class #=> Task
|
435
420
|
parent #=> @task
|
436
421
|
|
437
|
-
Optional belongs to
|
438
|
-
-------------------
|
422
|
+
== Optional belongs to
|
439
423
|
|
440
424
|
Later you decide to create a view to show all comments, independent if they belong
|
441
425
|
to a task, file or message. You can reuse your polymorphic controller just doing:
|
@@ -460,8 +444,7 @@ are available. As you expect, when no parent is found, the helpers return:
|
|
460
444
|
parent_class #=> nil
|
461
445
|
parent #=> nil
|
462
446
|
|
463
|
-
Singletons
|
464
|
-
----------
|
447
|
+
== Singletons
|
465
448
|
|
466
449
|
Now we are going to add manager to projects. We say that Manager is a singleton
|
467
450
|
resource because a Project has just one manager. You should declare it as
|
@@ -477,8 +460,7 @@ option.
|
|
477
460
|
|
478
461
|
It will deal with everything again and hide the action :index from you.
|
479
462
|
|
480
|
-
URL Helpers
|
481
|
-
-----------
|
463
|
+
== URL Helpers
|
482
464
|
|
483
465
|
When you use InheritedResources it creates some URL helpers.
|
484
466
|
And they handle everything for you. :)
|
@@ -490,6 +472,7 @@ And they handle everything for you. :)
|
|
490
472
|
edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
|
491
473
|
edit_resource_url(comment) #=> /posts/1/comments/#{comment.to_param}/edit
|
492
474
|
collection_url # => /posts/1/comments
|
475
|
+
parent_url # => /posts/1
|
493
476
|
|
494
477
|
# /projects/1/tasks
|
495
478
|
resource_url # => /projects/1/tasks/#{@task.to_param}
|
@@ -498,6 +481,7 @@ And they handle everything for you. :)
|
|
498
481
|
edit_resource_url # => /projects/1/tasks/#{@task.to_param}/edit
|
499
482
|
edit_resource_url(task) # => /projects/1/tasks/#{task.to_param}/edit
|
500
483
|
collection_url # => /projects/1/tasks
|
484
|
+
parent_url # => /projects/1
|
501
485
|
|
502
486
|
# /users
|
503
487
|
resource_url # => /users/#{@user.to_param}
|
@@ -506,6 +490,7 @@ And they handle everything for you. :)
|
|
506
490
|
edit_resource_url # => /users/#{@user.to_param}/edit
|
507
491
|
edit_resource_url(user) # => /users/#{user.to_param}/edit
|
508
492
|
collection_url # => /users
|
493
|
+
parent_url # => /
|
509
494
|
|
510
495
|
Those urls helpers also accepts a hash as options, just as in named routes.
|
511
496
|
|
@@ -518,11 +503,10 @@ Another nice thing is that those urls are not guessed during runtime. They are
|
|
518
503
|
all created when your application is loaded (except for polymorphic
|
519
504
|
associations, that relies on Rails polymorphic_url).
|
520
505
|
|
521
|
-
Bugs and Feedback
|
522
|
-
-----------------
|
506
|
+
== Bugs and Feedback
|
523
507
|
|
524
508
|
If you discover any bugs, please send an e-mail to jose.valim@plataformatec.com.br
|
525
509
|
If you just want to give some positive feedback or drop a line, that's fine too!
|
526
510
|
|
527
|
-
Copyright (c) 2009 José Valim
|
511
|
+
Copyright (c) 2009 José Valim<br />
|
528
512
|
http://blog.plataformatec.com.br/
|
data/Rakefile
CHANGED
@@ -31,7 +31,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
31
31
|
rdoc.rdoc_dir = 'rdoc'
|
32
32
|
rdoc.title = 'InheritedResources'
|
33
33
|
rdoc.options << '--line-numbers' << '--inline-source'
|
34
|
-
rdoc.rdoc_files.include('README')
|
34
|
+
rdoc.rdoc_files.include('README.rdoc')
|
35
35
|
rdoc.rdoc_files.include('MIT-LICENSE')
|
36
36
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
37
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.1
|
@@ -25,7 +25,7 @@ module InheritedResources
|
|
25
25
|
|
26
26
|
helper_method :collection_url, :collection_path, :resource_url, :resource_path,
|
27
27
|
:new_resource_url, :new_resource_path, :edit_resource_url, :edit_resource_path,
|
28
|
-
:resource, :collection, :resource_class
|
28
|
+
:parent_url, :parent_path, :resource, :collection, :resource_class, :association_chain
|
29
29
|
|
30
30
|
base.with_options :instance_writer => false do |c|
|
31
31
|
c.class_inheritable_accessor :resource_class
|
@@ -82,6 +82,16 @@ module InheritedResources
|
|
82
82
|
false
|
83
83
|
end
|
84
84
|
|
85
|
+
# Returns the association chain, with all parents (does not include the
|
86
|
+
# current resource).
|
87
|
+
#
|
88
|
+
def association_chain
|
89
|
+
@association_chain ||=
|
90
|
+
symbols_for_association_chain.inject([begin_of_association_chain]) do |chain, symbol|
|
91
|
+
chain << evaluate_parent(symbol, resources_configuration[symbol], chain.last)
|
92
|
+
end.compact.freeze
|
93
|
+
end
|
94
|
+
|
85
95
|
# Overwrite this method to provide other interpolation options when
|
86
96
|
# the flash message is going to be set.
|
87
97
|
#
|
@@ -107,11 +117,7 @@ module InheritedResources
|
|
107
117
|
# parents chain and returns the scoped association.
|
108
118
|
#
|
109
119
|
def end_of_association_chain #:nodoc:
|
110
|
-
chain =
|
111
|
-
evaluate_parent(symbol, resources_configuration[symbol], chain)
|
112
|
-
end
|
113
|
-
|
114
|
-
if chain
|
120
|
+
if chain = association_chain.last
|
115
121
|
if method_for_association_chain
|
116
122
|
apply_scope_to(chain.send(method_for_association_chain))
|
117
123
|
else
|
@@ -115,8 +115,8 @@ module InheritedResources
|
|
115
115
|
#
|
116
116
|
# * <tt>:except</tt> - In which actions the scope is not applied. By default is :none.
|
117
117
|
#
|
118
|
-
# * <tt>:
|
119
|
-
#
|
118
|
+
# * <tt>:as</tt> - The key in the params hash expected to find the scope.
|
119
|
+
# Defaults to the scope name.
|
120
120
|
#
|
121
121
|
# * <tt>:default</tt> - Default value for the scope. Whenever supplied the scope
|
122
122
|
# is always called. This is useful to add easy pagination.
|
@@ -125,7 +125,12 @@ module InheritedResources
|
|
125
125
|
options = scopes.extract_options!
|
126
126
|
|
127
127
|
options.symbolize_keys!
|
128
|
-
options.assert_valid_keys(:boolean, :key, :only, :except, :default)
|
128
|
+
options.assert_valid_keys(:boolean, :key, :only, :except, :default, :as)
|
129
|
+
|
130
|
+
if options[:key]
|
131
|
+
ActiveSupport::Deprecation.warn "has_scope :key is deprecated, use :as instead"
|
132
|
+
options[:as] ||= options[:key]
|
133
|
+
end
|
129
134
|
|
130
135
|
if self.scopes_configuration.empty?
|
131
136
|
include HasScopeHelpers
|
@@ -134,7 +139,7 @@ module InheritedResources
|
|
134
139
|
|
135
140
|
scopes.each do |scope|
|
136
141
|
self.scopes_configuration[scope] ||= {}
|
137
|
-
self.scopes_configuration[scope][:
|
142
|
+
self.scopes_configuration[scope][:as] = options[:as] || scope
|
138
143
|
self.scopes_configuration[scope][:only] = Array(options[:only])
|
139
144
|
self.scopes_configuration[scope][:except] = Array(options[:except])
|
140
145
|
self.scopes_configuration[scope][:boolean] = options[:boolean] if options.key?(:boolean)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module InheritedResources
|
2
|
+
# Allows controllers to write actions using a class method DSL.
|
3
|
+
#
|
4
|
+
# class MyController < InheritedResources::Base
|
5
|
+
# create! do |success, failure|
|
6
|
+
# success.html { render :text => "It works!" }
|
7
|
+
# end
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
module DSL
|
11
|
+
def self.included(base)
|
12
|
+
ACTIONS.each do |action|
|
13
|
+
base.class_eval <<-WRITTER
|
14
|
+
def self.#{action}!(options={}, &block)
|
15
|
+
define_method #{action.inspect}!, &block
|
16
|
+
class_eval <<-ACTION
|
17
|
+
def #{action}
|
18
|
+
super(\#{options.inspect}, &method(#{action.inspect}!))
|
19
|
+
end
|
20
|
+
ACTION
|
21
|
+
end
|
22
|
+
WRITTER
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -53,32 +53,17 @@ module ActionController #:nodoc:
|
|
53
53
|
|
54
54
|
def respond_to(*mimes, &block)
|
55
55
|
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
|
56
|
-
|
57
|
-
|
58
|
-
mimes = collect_mimes_from_class_level if mimes.empty?
|
59
|
-
mimes.each { |mime| responder.send(mime) }
|
60
|
-
block.call(responder) if block_given?
|
61
|
-
|
62
|
-
if format = responder.negotiate_mime
|
63
|
-
self.response.template.template_format = format.to_sym
|
64
|
-
self.response.content_type = format.to_s
|
65
|
-
self.formats = [ format.to_sym ]
|
66
|
-
|
67
|
-
if response = responder.response_for(format)
|
68
|
-
response.call
|
69
|
-
else
|
70
|
-
default_render
|
71
|
-
end
|
72
|
-
else
|
73
|
-
head :not_acceptable
|
56
|
+
if response = retrieve_response_from_mimes(mimes, &block)
|
57
|
+
response.call
|
74
58
|
end
|
75
59
|
end
|
76
60
|
|
77
61
|
def respond_with(*resources, &block)
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
62
|
+
if response = retrieve_response_from_mimes([], &block)
|
63
|
+
options = resources.extract_options!
|
64
|
+
options.merge!(:default_response => response)
|
65
|
+
(options.delete(:responder) || responder).call(self, resources, options)
|
66
|
+
end
|
82
67
|
end
|
83
68
|
|
84
69
|
def responder
|
@@ -105,6 +90,26 @@ module ActionController #:nodoc:
|
|
105
90
|
end
|
106
91
|
end
|
107
92
|
end
|
93
|
+
|
94
|
+
# Collects mimes and return the response for the negotiated format. Returns
|
95
|
+
# nil if :not_acceptable was sent to the client.
|
96
|
+
#
|
97
|
+
def retrieve_response_from_mimes(mimes, &block)
|
98
|
+
responder = ActionController::MimeResponds::Responder.new(self)
|
99
|
+
mimes = collect_mimes_from_class_level if mimes.empty?
|
100
|
+
mimes.each { |mime| responder.send(mime) }
|
101
|
+
block.call(responder) if block_given?
|
102
|
+
|
103
|
+
if format = responder.negotiate_mime
|
104
|
+
self.response.template.template_format = format.to_sym
|
105
|
+
self.response.content_type = format.to_s
|
106
|
+
self.formats = [ format.to_sym ]
|
107
|
+
responder.response_for(format) || proc { default_render }
|
108
|
+
else
|
109
|
+
head :not_acceptable
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
end
|
108
113
|
end
|
109
114
|
|
110
115
|
module MimeResponds
|
@@ -79,15 +79,16 @@ module ActionController #:nodoc:
|
|
79
79
|
# Check polymorphic_url documentation for more examples.
|
80
80
|
#
|
81
81
|
class Responder
|
82
|
-
attr_reader :controller, :request, :format, :resource, :
|
82
|
+
attr_reader :controller, :request, :format, :resource, :resources, :options
|
83
83
|
|
84
84
|
def initialize(controller, resources, options={})
|
85
85
|
@controller = controller
|
86
86
|
@request = controller.request
|
87
87
|
@format = controller.formats.first
|
88
88
|
@resource = resources.is_a?(Array) ? resources.last : resources
|
89
|
-
@
|
89
|
+
@resources = resources
|
90
90
|
@options = options
|
91
|
+
@default_response = options.delete(:default_response)
|
91
92
|
end
|
92
93
|
|
93
94
|
delegate :head, :render, :redirect_to, :to => :controller
|
@@ -109,8 +110,10 @@ module ActionController #:nodoc:
|
|
109
110
|
# template.
|
110
111
|
#
|
111
112
|
def to_html
|
113
|
+
default_render
|
114
|
+
rescue ActionView::MissingTemplate
|
112
115
|
if get?
|
113
|
-
|
116
|
+
raise
|
114
117
|
elsif has_errors?
|
115
118
|
render :action => default_action
|
116
119
|
else
|
@@ -118,12 +121,14 @@ module ActionController #:nodoc:
|
|
118
121
|
end
|
119
122
|
end
|
120
123
|
|
121
|
-
# All others formats
|
122
|
-
#
|
123
|
-
#
|
124
|
+
# All others formats follow the procedure below. First we try to render a
|
125
|
+
# template, if the template is not available, we verify if the resource
|
126
|
+
# responds to :to_format and display it.
|
124
127
|
#
|
125
128
|
def to_format
|
126
|
-
|
129
|
+
default_render
|
130
|
+
rescue ActionView::MissingTemplate
|
131
|
+
raise unless resourceful?
|
127
132
|
|
128
133
|
if get?
|
129
134
|
display resource
|
@@ -144,6 +149,20 @@ module ActionController #:nodoc:
|
|
144
149
|
resource.respond_to?(:"to_#{format}")
|
145
150
|
end
|
146
151
|
|
152
|
+
# Returns the resource location by retrieving it from the options or
|
153
|
+
# returning the resources array.
|
154
|
+
#
|
155
|
+
def resource_location
|
156
|
+
options[:location] || resources
|
157
|
+
end
|
158
|
+
|
159
|
+
# If a given response block was given, use it, otherwise call render on
|
160
|
+
# controller.
|
161
|
+
#
|
162
|
+
def default_render
|
163
|
+
@default_response.call
|
164
|
+
end
|
165
|
+
|
147
166
|
# display is just a shortcut to render a resource with the current format.
|
148
167
|
#
|
149
168
|
# display @user, :status => :ok
|
@@ -10,13 +10,15 @@ module InheritedResources
|
|
10
10
|
# new_resource_url # => /posts/1/comments/new
|
11
11
|
# edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
|
12
12
|
# collection_url # => /posts/1/comments
|
13
|
+
# parent_url # => /posts/1
|
13
14
|
#
|
14
15
|
# # /projects/1/tasks
|
15
|
-
# resource_url # => /
|
16
|
-
# resource_url(task) # => /
|
17
|
-
# new_resource_url # => /
|
18
|
-
# edit_resource_url # => /
|
19
|
-
# collection_url # => /
|
16
|
+
# resource_url # => /projects/1/tasks/#{@task.to_param}
|
17
|
+
# resource_url(task) # => /projects/1/tasks/#{task.to_param}
|
18
|
+
# new_resource_url # => /projects/1/tasks/new
|
19
|
+
# edit_resource_url # => /projects/1/tasks/#{@task.to_param}/edit
|
20
|
+
# collection_url # => /projects/1/tasks
|
21
|
+
# parent_url # => /projects/1
|
20
22
|
#
|
21
23
|
# # /users
|
22
24
|
# resource_url # => /users/#{@user.to_param}
|
@@ -24,6 +26,7 @@ module InheritedResources
|
|
24
26
|
# new_resource_url # => /users/new
|
25
27
|
# edit_resource_url # => /users/#{@user.to_param}/edit
|
26
28
|
# collection_url # => /users
|
29
|
+
# parent_url # => /
|
27
30
|
#
|
28
31
|
# The nice thing is that those urls are not guessed during runtime. They are
|
29
32
|
# all created when you inherit.
|
@@ -72,6 +75,9 @@ module InheritedResources
|
|
72
75
|
collection_ivars = resource_ivars.dup
|
73
76
|
collection_segments = resource_segments.dup
|
74
77
|
|
78
|
+
# Generate parent url before we add resource instances.
|
79
|
+
generate_url_and_path_helpers nil, :parent, resource_segments, resource_ivars
|
80
|
+
|
75
81
|
# This is the default route configuration, later we have to deal with
|
76
82
|
# exception from polymorphic and singleton cases.
|
77
83
|
#
|
@@ -126,7 +132,7 @@ module InheritedResources
|
|
126
132
|
# If it's not a singleton, ivars are not empty, not a collection or
|
127
133
|
# not a "new" named route, we can pass a resource as argument.
|
128
134
|
#
|
129
|
-
unless singleton || ivars.empty? || name == :collection || prefix == :new
|
135
|
+
unless (singleton && name != :parent) || ivars.empty? || name == :collection || prefix == :new
|
130
136
|
ivars.push "(given_args.first || #{ivars.pop})"
|
131
137
|
end
|
132
138
|
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Pet
|
4
|
+
def self.human_name; 'Pet'; end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Puppet
|
8
|
+
def self.human_name; 'Puppet'; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class PetsController < InheritedResources::Base
|
12
|
+
attr_accessor :current_user
|
13
|
+
|
14
|
+
def edit
|
15
|
+
@pet = 'new pet'
|
16
|
+
edit!
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def collection
|
21
|
+
@pets ||= end_of_association_chain.all
|
22
|
+
end
|
23
|
+
|
24
|
+
def begin_of_association_chain
|
25
|
+
@current_user
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class BeginOfAssociationChainTest < ActionController::TestCase
|
30
|
+
tests PetsController
|
31
|
+
|
32
|
+
def setup
|
33
|
+
@controller.current_user = mock()
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_begin_of_association_chain_is_called_on_index
|
37
|
+
@controller.current_user.expects(:pets).returns(Pet)
|
38
|
+
Pet.expects(:all).returns(mock_pet)
|
39
|
+
get :index
|
40
|
+
assert_response :success
|
41
|
+
assert_equal 'Index HTML', @response.body.strip
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_begin_of_association_chain_is_called_on_new
|
45
|
+
@controller.current_user.expects(:pets).returns(Pet)
|
46
|
+
Pet.expects(:build).returns(mock_pet)
|
47
|
+
get :new
|
48
|
+
assert_response :success
|
49
|
+
assert_equal 'New HTML', @response.body.strip
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_begin_of_association_chain_is_called_on_show
|
53
|
+
@controller.current_user.expects(:pets).returns(Pet)
|
54
|
+
Pet.expects(:find).with('47').returns(mock_pet)
|
55
|
+
get :show, :id => '47'
|
56
|
+
assert_response :success
|
57
|
+
assert_equal 'Show HTML', @response.body.strip
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_instance_variable_should_not_be_set_if_already_defined
|
61
|
+
@controller.current_user.expects(:pets).never
|
62
|
+
Pet.expects(:find).never
|
63
|
+
get :edit
|
64
|
+
assert_response :success
|
65
|
+
assert_equal 'new pet', assigns(:pet)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_model_is_not_initialized_with_nil
|
69
|
+
@controller.current_user.expects(:pets).returns(Pet)
|
70
|
+
Pet.expects(:build).with({}).returns(mock_pet)
|
71
|
+
get :new
|
72
|
+
assert_equal mock_pet, assigns(:pet)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_begin_of_association_chain_is_included_in_chain
|
76
|
+
@controller.current_user.expects(:pets).returns(Pet)
|
77
|
+
Pet.expects(:build).with({}).returns(mock_pet)
|
78
|
+
get :new
|
79
|
+
assert_equal [@controller.current_user], @controller.send(:association_chain)
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def mock_pet(stubs={})
|
84
|
+
@mock_pet ||= mock(stubs)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
class PuppetsController < InheritedResources::Base
|
90
|
+
optional_belongs_to :pet
|
91
|
+
end
|
92
|
+
|
93
|
+
class AssociationChainTest < ActionController::TestCase
|
94
|
+
tests PuppetsController
|
95
|
+
|
96
|
+
def setup
|
97
|
+
@controller.stubs(:resource_url).returns('/')
|
98
|
+
@controller.stubs(:collection_url).returns('/')
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_parent_is_added_to_association_chain
|
102
|
+
Pet.expects(:find).with('37').returns(mock_pet)
|
103
|
+
mock_pet.expects(:puppets).returns(Puppet)
|
104
|
+
Puppet.expects(:find).with('42').returns(mock_puppet)
|
105
|
+
mock_puppet.expects(:destroy)
|
106
|
+
delete :destroy, :id => '42', :pet_id => '37'
|
107
|
+
assert_equal [mock_pet], @controller.send(:association_chain)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_parent_is_added_to_association_chain_if_not_available
|
111
|
+
Puppet.expects(:find).with('42').returns(mock_puppet)
|
112
|
+
mock_puppet.expects(:destroy)
|
113
|
+
delete :destroy, :id => '42'
|
114
|
+
assert_equal [], @controller.send(:association_chain)
|
115
|
+
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
def mock_pet(stubs={})
|
119
|
+
@mock_pet ||= mock(stubs)
|
120
|
+
end
|
121
|
+
|
122
|
+
def mock_puppet(stubs={})
|
123
|
+
@mock_puppet ||= mock(stubs)
|
124
|
+
end
|
125
|
+
end
|
data/test/class_methods_test.rb
CHANGED
@@ -131,7 +131,7 @@ class HasScopeClassMethods < ActiveSupport::TestCase
|
|
131
131
|
assert config.key?(:by_city)
|
132
132
|
assert config.key?(:featured)
|
133
133
|
|
134
|
-
assert_equal config[:by_city], { :
|
135
|
-
assert_equal config[:featured], { :
|
134
|
+
assert_equal config[:by_city], { :as => :by_city, :only => [], :except => [] }
|
135
|
+
assert_equal config[:featured], { :as => :by_featured, :only => [ :index ], :except => [], :boolean => true }
|
136
136
|
end
|
137
137
|
end
|
data/test/has_scope_test.rb
CHANGED
@@ -8,7 +8,7 @@ class TreesController < InheritedResources::Base
|
|
8
8
|
has_scope :color
|
9
9
|
has_scope :only_tall, :boolean => true, :only => :index
|
10
10
|
has_scope :shadown_range, :default => 10, :except => [ :index, :show, :destroy, :new ]
|
11
|
-
has_scope :root_type, :
|
11
|
+
has_scope :root_type, :as => :root
|
12
12
|
has_scope :calculate_height, :default => proc {|c| c.session[:height] || 20 }, :only => :new
|
13
13
|
end
|
14
14
|
|
data/test/url_helpers_test.rb
CHANGED
@@ -82,6 +82,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
82
82
|
controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
|
83
83
|
controller.send("edit_resource_#{path_or_url}")
|
84
84
|
|
85
|
+
controller.expects("root_#{path_or_url}").with({}).once
|
86
|
+
controller.send("parent_#{path_or_url}")
|
87
|
+
|
85
88
|
# With arg
|
86
89
|
controller.expects("house_#{path_or_url}").with(:arg, {}).once
|
87
90
|
controller.send("resource_#{path_or_url}", :arg)
|
@@ -114,6 +117,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
114
117
|
controller.expects("edit_admin_backpack_#{path_or_url}").with(:backpack, {}).once
|
115
118
|
controller.send("edit_resource_#{path_or_url}")
|
116
119
|
|
120
|
+
controller.expects("admin_#{path_or_url}").with({}).once
|
121
|
+
controller.send("parent_#{path_or_url}")
|
122
|
+
|
117
123
|
# With arg
|
118
124
|
controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
|
119
125
|
controller.send("resource_#{path_or_url}", :arg)
|
@@ -144,6 +150,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
144
150
|
controller.expects("edit_universum_#{path_or_url}").with({}).once
|
145
151
|
controller.send("edit_resource_#{path_or_url}")
|
146
152
|
|
153
|
+
controller.expects("root_#{path_or_url}").with({}).once
|
154
|
+
controller.send("parent_#{path_or_url}")
|
155
|
+
|
147
156
|
# With options
|
148
157
|
# Also tests that argument sent are not used
|
149
158
|
controller.expects("universum_#{path_or_url}").with(:page => 1).once
|
@@ -169,6 +178,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
169
178
|
controller.expects("edit_house_table_#{path_or_url}").with(:house, :table, {}).once
|
170
179
|
controller.send("edit_resource_#{path_or_url}")
|
171
180
|
|
181
|
+
controller.expects("house_#{path_or_url}").with(:house, {}).once
|
182
|
+
controller.send("parent_#{path_or_url}")
|
183
|
+
|
172
184
|
# With arg
|
173
185
|
controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
|
174
186
|
controller.send("resource_#{path_or_url}", :arg)
|
@@ -176,6 +188,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
176
188
|
controller.expects("edit_house_table_#{path_or_url}").with(:house, :arg, {}).once
|
177
189
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
178
190
|
|
191
|
+
controller.expects("house_#{path_or_url}").with(:arg, {}).once
|
192
|
+
controller.send("parent_#{path_or_url}", :arg)
|
193
|
+
|
179
194
|
# With options
|
180
195
|
controller.expects("house_table_#{path_or_url}").with(:house, :arg, :page => 1).once
|
181
196
|
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
@@ -200,6 +215,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
200
215
|
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :room, {}).once
|
201
216
|
controller.send("edit_resource_#{path_or_url}")
|
202
217
|
|
218
|
+
controller.expects("big_house_#{path_or_url}").with(:house, {}).once
|
219
|
+
controller.send("parent_#{path_or_url}")
|
220
|
+
|
203
221
|
# With args
|
204
222
|
controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, {}).once
|
205
223
|
controller.send("resource_#{path_or_url}", :arg)
|
@@ -207,6 +225,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
207
225
|
controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :arg, {}).once
|
208
226
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
209
227
|
|
228
|
+
controller.expects("big_house_#{path_or_url}").with(:arg, {}).once
|
229
|
+
controller.send("parent_#{path_or_url}", :arg)
|
230
|
+
|
210
231
|
# With options
|
211
232
|
controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, :page => 1).once
|
212
233
|
controller.send("resource_#{path_or_url}", :arg, :page => 1)
|
@@ -232,6 +253,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
232
253
|
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
|
233
254
|
controller.send("edit_resource_#{path_or_url}")
|
234
255
|
|
256
|
+
controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
|
257
|
+
controller.send("parent_#{path_or_url}")
|
258
|
+
|
235
259
|
# With args
|
236
260
|
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
|
237
261
|
controller.send("edit_resource_#{path_or_url}", :arg)
|
@@ -239,6 +263,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
239
263
|
controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
|
240
264
|
controller.send("resource_#{path_or_url}", :arg)
|
241
265
|
|
266
|
+
controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
|
267
|
+
controller.send("parent_#{path_or_url}", :arg)
|
268
|
+
|
242
269
|
# With options
|
243
270
|
controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, :page => 1).once
|
244
271
|
controller.send("edit_resource_#{path_or_url}", :arg, :page => 1)
|
@@ -263,6 +290,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
263
290
|
controller.expects("edit_house_owner_#{path_or_url}").with(:house, {}).once
|
264
291
|
controller.send("edit_resource_#{path_or_url}")
|
265
292
|
|
293
|
+
controller.expects("house_#{path_or_url}").with(:house, {}).once
|
294
|
+
controller.send("parent_#{path_or_url}")
|
295
|
+
|
266
296
|
# With options
|
267
297
|
# Also tests that argument sent are not used
|
268
298
|
controller.expects("house_owner_#{path_or_url}").with(:house, :page => 1).once
|
@@ -295,12 +325,18 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
295
325
|
|
296
326
|
controller.expects("edit_house_bed_#{path_or_url}").with(house, bed).once
|
297
327
|
controller.send("edit_resource_#{path_or_url}")
|
328
|
+
|
329
|
+
controller.expects("house_#{path_or_url}").with(house).once
|
330
|
+
controller.send("parent_#{path_or_url}")
|
298
331
|
end
|
299
332
|
|
300
333
|
# With options
|
301
334
|
controller.expects("house_bed_url").with(house, bed, :page => 1).once
|
302
335
|
controller.send("resource_url", :page => 1)
|
303
336
|
|
337
|
+
controller.expects("house_url").with(house, :page => 1).once
|
338
|
+
controller.send("parent_url", :page => 1)
|
339
|
+
|
304
340
|
# With args
|
305
341
|
controller.expects("polymorphic_url").with([:arg, new_bed], {}).once
|
306
342
|
controller.send("collection_url", :arg)
|
@@ -310,6 +346,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
310
346
|
|
311
347
|
controller.expects("edit_polymorphic_url").with([house, :arg], {}).once
|
312
348
|
controller.send("edit_resource_url", :arg)
|
349
|
+
|
350
|
+
controller.expects("polymorphic_url").with([:arg], {}).once
|
351
|
+
controller.send("parent_url", :arg)
|
313
352
|
end
|
314
353
|
|
315
354
|
def test_url_helpers_on_namespaced_polymorphic_belongs_to
|
@@ -337,12 +376,18 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
337
376
|
|
338
377
|
controller.expects("edit_admin_house_desk_#{path_or_url}").with(house, desk).once
|
339
378
|
controller.send("edit_resource_#{path_or_url}")
|
379
|
+
|
380
|
+
controller.expects("admin_house_#{path_or_url}").with(house).once
|
381
|
+
controller.send("parent_#{path_or_url}")
|
340
382
|
end
|
341
383
|
|
342
384
|
# With options
|
343
385
|
controller.expects("admin_house_desk_url").with(house, desk, :page => 1).once
|
344
386
|
controller.send("resource_url", :page => 1)
|
345
387
|
|
388
|
+
controller.expects("admin_house_url").with(house, :page => 1).once
|
389
|
+
controller.send("parent_url", :page => 1)
|
390
|
+
|
346
391
|
# With args
|
347
392
|
controller.expects("polymorphic_url").with(['admin', :arg, new_desk], {}).once
|
348
393
|
controller.send("collection_url", :arg)
|
@@ -352,6 +397,9 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
352
397
|
|
353
398
|
controller.expects("edit_polymorphic_url").with(['admin', house, :arg], {}).once
|
354
399
|
controller.send("edit_resource_url", :arg)
|
400
|
+
|
401
|
+
controller.expects("polymorphic_url").with(['admin', :arg], {}).once
|
402
|
+
controller.send("parent_url", :arg)
|
355
403
|
end
|
356
404
|
|
357
405
|
def test_url_helpers_on_nested_polymorphic_belongs_to
|
@@ -381,18 +429,27 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
381
429
|
|
382
430
|
controller.expects("edit_house_table_dish_#{path_or_url}").with(house, table, dish).once
|
383
431
|
controller.send("edit_resource_#{path_or_url}")
|
432
|
+
|
433
|
+
controller.expects("house_table_#{path_or_url}").with(house, table).once
|
434
|
+
controller.send("parent_#{path_or_url}")
|
384
435
|
end
|
385
436
|
|
386
437
|
# With options
|
387
438
|
controller.expects("house_table_dish_url").with(house, table, dish, :page => 1).once
|
388
439
|
controller.send("resource_url", :page => 1)
|
389
440
|
|
441
|
+
controller.expects("house_table_url").with(house, table, :page => 1).once
|
442
|
+
controller.send("parent_url", :page => 1)
|
443
|
+
|
390
444
|
# With args
|
391
445
|
controller.expects("polymorphic_url").with([house, table, :arg], {}).once
|
392
446
|
controller.send("resource_url", :arg)
|
393
447
|
|
394
448
|
controller.expects("edit_polymorphic_url").with([house, table, :arg], {}).once
|
395
449
|
controller.send("edit_resource_url", :arg)
|
450
|
+
|
451
|
+
controller.expects("polymorphic_url").with([house, :arg], {}).once
|
452
|
+
controller.send("parent_url", :arg)
|
396
453
|
end
|
397
454
|
|
398
455
|
def test_url_helpers_on_singleton_nested_polymorphic_belongs_to
|
@@ -421,15 +478,24 @@ class UrlHelpersTest < ActiveSupport::TestCase
|
|
421
478
|
|
422
479
|
controller.expects("edit_house_table_center_#{path_or_url}").with(house, table).once
|
423
480
|
controller.send("edit_resource_#{path_or_url}")
|
481
|
+
|
482
|
+
controller.expects("house_table_#{path_or_url}").with(house, table).once
|
483
|
+
controller.send("parent_#{path_or_url}")
|
424
484
|
end
|
425
485
|
|
426
486
|
# With options
|
427
487
|
controller.expects("house_table_center_url").with(house, table, :page => 1)
|
428
488
|
controller.send("resource_url", :page => 1)
|
429
489
|
|
490
|
+
controller.expects("house_table_url").with(house, table, :page => 1)
|
491
|
+
controller.send("parent_url", :page => 1)
|
492
|
+
|
430
493
|
# With args
|
431
494
|
controller.expects("polymorphic_url").with([house, table, :center], {}).once
|
432
495
|
controller.send("resource_url", :arg)
|
496
|
+
|
497
|
+
controller.expects("polymorphic_url").with([house, :arg], {}).once
|
498
|
+
controller.send("parent_url", :arg)
|
433
499
|
end
|
434
500
|
|
435
501
|
def test_url_helpers_on_optional_polymorphic_belongs_to
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: josevalim-inherited_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -20,11 +20,11 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
- README
|
23
|
+
- README.rdoc
|
24
24
|
files:
|
25
25
|
- CHANGELOG
|
26
26
|
- MIT-LICENSE
|
27
|
-
- README
|
27
|
+
- README.rdoc
|
28
28
|
- Rakefile
|
29
29
|
- VERSION
|
30
30
|
- lib/inherited_resources.rb
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/inherited_resources/base_helpers.rb
|
34
34
|
- lib/inherited_resources/belongs_to_helpers.rb
|
35
35
|
- lib/inherited_resources/class_methods.rb
|
36
|
+
- lib/inherited_resources/dsl.rb
|
36
37
|
- lib/inherited_resources/dumb_responder.rb
|
37
38
|
- lib/inherited_resources/has_scope_helpers.rb
|
38
39
|
- lib/inherited_resources/legacy/respond_to.rb
|
@@ -40,8 +41,9 @@ files:
|
|
40
41
|
- lib/inherited_resources/polymorphic_helpers.rb
|
41
42
|
- lib/inherited_resources/singleton_helpers.rb
|
42
43
|
- lib/inherited_resources/url_helpers.rb
|
43
|
-
has_rdoc:
|
44
|
+
has_rdoc: false
|
44
45
|
homepage: http://github.com/josevalim/inherited_resources
|
46
|
+
licenses:
|
45
47
|
post_install_message:
|
46
48
|
rdoc_options:
|
47
49
|
- --charset=UTF-8
|
@@ -62,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
64
|
requirements: []
|
63
65
|
|
64
66
|
rubyforge_project: inherited_resources
|
65
|
-
rubygems_version: 1.
|
67
|
+
rubygems_version: 1.3.5
|
66
68
|
signing_key:
|
67
69
|
specification_version: 3
|
68
70
|
summary: Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important.
|
@@ -74,10 +76,10 @@ test_files:
|
|
74
76
|
- test/redirect_to_test.rb
|
75
77
|
- test/has_scope_test.rb
|
76
78
|
- test/class_methods_test.rb
|
79
|
+
- test/association_chain_test.rb
|
77
80
|
- test/aliases_test.rb
|
78
81
|
- test/flash_test.rb
|
79
82
|
- test/url_helpers_test.rb
|
80
|
-
- test/base_helpers_test.rb
|
81
83
|
- test/belongs_to_test.rb
|
82
84
|
- test/polymorphic_test.rb
|
83
85
|
- test/defaults_test.rb
|
data/test/base_helpers_test.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
|
3
|
-
class Pet
|
4
|
-
def self.human_name; 'Pet'; end
|
5
|
-
end
|
6
|
-
|
7
|
-
class PetsController < InheritedResources::Base
|
8
|
-
attr_accessor :current_user
|
9
|
-
|
10
|
-
def edit
|
11
|
-
@pet = 'new pet'
|
12
|
-
edit!
|
13
|
-
end
|
14
|
-
|
15
|
-
protected
|
16
|
-
def collection
|
17
|
-
@pets ||= end_of_association_chain.all
|
18
|
-
end
|
19
|
-
|
20
|
-
def begin_of_association_chain
|
21
|
-
@current_user
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class AssociationChainBaseHelpersTest < ActionController::TestCase
|
26
|
-
tests PetsController
|
27
|
-
|
28
|
-
def setup
|
29
|
-
@controller.current_user = mock()
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_begin_of_association_chain_is_called_on_index
|
33
|
-
@controller.current_user.expects(:pets).returns(Pet)
|
34
|
-
Pet.expects(:all).returns(mock_pet)
|
35
|
-
get :index
|
36
|
-
assert_response :success
|
37
|
-
assert 'Index HTML', @response.body.strip
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_begin_of_association_chain_is_called_on_new
|
41
|
-
@controller.current_user.expects(:pets).returns(Pet)
|
42
|
-
Pet.expects(:build).returns(mock_pet)
|
43
|
-
get :new
|
44
|
-
assert_response :success
|
45
|
-
assert 'New HTML', @response.body.strip
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_begin_of_association_chain_is_called_on_show
|
49
|
-
@controller.current_user.expects(:pets).returns(Pet)
|
50
|
-
Pet.expects(:find).with('47').returns(mock_pet)
|
51
|
-
get :show, :id => '47'
|
52
|
-
assert_response :success
|
53
|
-
assert 'Show HTML', @response.body.strip
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_instance_variable_should_not_be_set_if_already_defined
|
57
|
-
@controller.current_user.expects(:pets).never
|
58
|
-
Pet.expects(:find).never
|
59
|
-
get :edit
|
60
|
-
assert_response :success
|
61
|
-
assert_equal 'new pet', assigns(:pet)
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_model_is_not_initialized_with_nil
|
65
|
-
@controller.current_user.expects(:pets).returns(Pet)
|
66
|
-
Pet.expects(:build).with({}).returns(mock_pet)
|
67
|
-
get :new
|
68
|
-
assert mock_pet, assigns(:pet)
|
69
|
-
end
|
70
|
-
|
71
|
-
protected
|
72
|
-
def mock_pet(stubs={})
|
73
|
-
@mock_pet ||= mock(stubs)
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|