josevalim-inherited_resources 0.1.2 → 0.2.0
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 +9 -0
- data/README +37 -3
- data/lib/inherited_resources/base.rb +17 -6
- data/lib/inherited_resources/base_helpers.rb +21 -0
- data/lib/inherited_resources/dumb_responder.rb +19 -0
- data/test/aliases_test.rb +59 -2
- data/test/test_helper.rb +0 -7
- metadata +3 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# Version 0.2.0
|
2
|
+
|
3
|
+
* Added support success/failure blocks.
|
4
|
+
* Eager loading of files to work properly in multithreaded environments.
|
5
|
+
|
6
|
+
# Version 0.1.2
|
7
|
+
|
8
|
+
* Added more helper_methods.
|
9
|
+
|
1
10
|
# Version 0.1.1
|
2
11
|
|
3
12
|
* Added Rails 2.3.0 and changed tests to work with ActionController::TestCase.
|
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Inherited Resources
|
2
2
|
License: MIT
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.2.0
|
4
4
|
|
5
5
|
You can also read this README in pretty html at the GitHub project Wiki page:
|
6
6
|
|
@@ -141,8 +141,9 @@ twin brother: begin_of_association_chain.
|
|
141
141
|
It's mostly used when you want to create resources based on the @current_user.
|
142
142
|
In such cases, you don't have your @current_user in the url, but in the session.
|
143
143
|
|
144
|
-
|
145
|
-
|
144
|
+
This is usually when you have urls like "account/projects" and you have to do
|
145
|
+
@current_user.projects.find or @current_user.projects.build in your actions.
|
146
|
+
You can deal with it just doing:
|
146
147
|
|
147
148
|
class ProjectsController < InheritedResources::Base
|
148
149
|
protected
|
@@ -195,6 +196,39 @@ Yeap, that simple! The nice part is since you already set the instance variable
|
|
195
196
|
@project, it will not do it again and overwrite your @project with something
|
196
197
|
special. :)
|
197
198
|
|
199
|
+
Before we finish this topic, we should talk about one more thing: "success/failure
|
200
|
+
blocks". Let's suppose that when we update our project, in case of failure, we
|
201
|
+
want to redirect to the project url instead of re-rendering the edit template.
|
202
|
+
|
203
|
+
Our first attempt to do this would be:
|
204
|
+
|
205
|
+
class ProjectsController < InheritedResources::Base
|
206
|
+
def update
|
207
|
+
update! do |format|
|
208
|
+
unless @project.errors.empty? # failure
|
209
|
+
format.html { redirect_to project_url(@project) }
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
Looks to verbose, right? (Maybe it reminds you of something! :P)
|
216
|
+
But this is Ruby and we can actually do:
|
217
|
+
|
218
|
+
class ProjectsController < InheritedResources::Base
|
219
|
+
def update
|
220
|
+
update! do |success, failure|
|
221
|
+
failure.html { redirect_to project_url(@project) }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
Much better! So explaning everything: when you give a block which expects one
|
227
|
+
argument it will be executed in both scenarios: success and failure. But If you
|
228
|
+
give a block that expects two arguments, the first will be executed only in
|
229
|
+
success scenarios and the second in failure scenarios. You keep everything
|
230
|
+
clean and organized inside the same action.
|
231
|
+
|
198
232
|
Flash messages and I18n
|
199
233
|
-----------------------
|
200
234
|
|
@@ -157,7 +157,18 @@
|
|
157
157
|
#
|
158
158
|
# Further customizations can be done replacing some methods. Check
|
159
159
|
# base_helpers.rb file for more information.
|
160
|
-
|
160
|
+
|
161
|
+
# Let's require all needed files here. We are still on time to eager load
|
162
|
+
# everything on multithreaded environments.
|
163
|
+
require File.dirname(__FILE__) + '/base_helpers.rb'
|
164
|
+
require File.dirname(__FILE__) + '/belongs_to.rb'
|
165
|
+
require File.dirname(__FILE__) + '/belongs_to_helpers.rb'
|
166
|
+
require File.dirname(__FILE__) + '/class_methods.rb'
|
167
|
+
require File.dirname(__FILE__) + '/dumb_responder.rb'
|
168
|
+
require File.dirname(__FILE__) + '/polymorphic_helpers.rb'
|
169
|
+
require File.dirname(__FILE__) + '/singleton_helpers.rb'
|
170
|
+
require File.dirname(__FILE__) + '/url_helpers.rb'
|
171
|
+
|
161
172
|
module InheritedResources
|
162
173
|
RESOURCES_ACTIONS = [ :index, :show, :new, :edit, :create, :update, :destroy ]
|
163
174
|
|
@@ -217,14 +228,14 @@ module InheritedResources
|
|
217
228
|
set_flash_message!(:notice, '{{resource}} was successfully created.')
|
218
229
|
|
219
230
|
respond_to(:with => object, :status => :created, :location => resource_url) do |format|
|
220
|
-
|
231
|
+
block.call args_for_block(block, format, true) if block_given?
|
221
232
|
format.html { redirect_to(resource_url) }
|
222
233
|
end
|
223
234
|
else
|
224
235
|
set_flash_message!(:error)
|
225
236
|
|
226
237
|
respond_to(:with => object.errors, :status => :unprocessable_entity) do |format|
|
227
|
-
|
238
|
+
block.call args_for_block(block, format, false) if block_given?
|
228
239
|
format.html { render :action => "new" }
|
229
240
|
end
|
230
241
|
end
|
@@ -239,7 +250,7 @@ module InheritedResources
|
|
239
250
|
set_flash_message!(:notice, '{{resource}} was successfully updated.')
|
240
251
|
|
241
252
|
respond_to do |format|
|
242
|
-
|
253
|
+
block.call args_for_block(block, format, true) if block_given?
|
243
254
|
format.html { redirect_to(resource_url) }
|
244
255
|
format.all { head :ok }
|
245
256
|
end
|
@@ -247,7 +258,7 @@ module InheritedResources
|
|
247
258
|
set_flash_message!(:error)
|
248
259
|
|
249
260
|
respond_to(:with => object.errors, :status => :unprocessable_entity) do |format|
|
250
|
-
|
261
|
+
block.call args_for_block(block, format, false) if block_given?
|
251
262
|
format.html { render :action => "edit" }
|
252
263
|
end
|
253
264
|
end
|
@@ -255,7 +266,7 @@ module InheritedResources
|
|
255
266
|
alias :update! :update
|
256
267
|
|
257
268
|
# DELETE /resources/1
|
258
|
-
def destroy
|
269
|
+
def destroy
|
259
270
|
resource.destroy
|
260
271
|
|
261
272
|
set_flash_message!(:notice, '{{resource}} was successfully destroyed.')
|
@@ -195,5 +195,26 @@ module InheritedResources #:nodoc:
|
|
195
195
|
{ }
|
196
196
|
end
|
197
197
|
|
198
|
+
# Used to allow to specify success and failure within just one block:
|
199
|
+
#
|
200
|
+
# def create
|
201
|
+
# create! do |success, failure|
|
202
|
+
# failure.html { redirect_to root_url }
|
203
|
+
# end
|
204
|
+
# end
|
205
|
+
#
|
206
|
+
def args_for_block(block_to_check_args, format, success = true)
|
207
|
+
if block_to_check_args.arity == 2
|
208
|
+
dumb_responder = InheritedResources::DumbResponder.new
|
209
|
+
if success
|
210
|
+
return format, dumb_responder
|
211
|
+
else
|
212
|
+
return dumb_responder, format
|
213
|
+
end
|
214
|
+
else
|
215
|
+
return format
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
198
219
|
end
|
199
220
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# = Dumb Responder
|
2
|
+
#
|
3
|
+
# This responder discards all messages sent to him.
|
4
|
+
#
|
5
|
+
module InheritedResources #:nodoc:
|
6
|
+
class DumbResponder #:nodoc:
|
7
|
+
|
8
|
+
instance_methods.each do |m|
|
9
|
+
undef_method m unless m =~ /^__/
|
10
|
+
end
|
11
|
+
|
12
|
+
# This is like a good husband, he will just listen everything that his wife
|
13
|
+
# says (which is a lot) without complaining. :)
|
14
|
+
def method_missing(*args)
|
15
|
+
return true
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/test/aliases_test.rb
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
# methods works properly.
|
3
3
|
require File.dirname(__FILE__) + '/test_helper'
|
4
4
|
|
5
|
-
class Student;
|
5
|
+
class Student;
|
6
|
+
def self.human_name; 'Student'; end
|
7
|
+
end
|
6
8
|
|
7
9
|
class StudentsController < InheritedResources::Base
|
8
10
|
|
@@ -17,6 +19,25 @@ class StudentsController < InheritedResources::Base
|
|
17
19
|
new!
|
18
20
|
end
|
19
21
|
|
22
|
+
def create
|
23
|
+
create! do |success, failure|
|
24
|
+
success.html { render :text => "I won't redirect!" }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
update! do |success, failure|
|
30
|
+
success.html { redirect_to(resource_url) }
|
31
|
+
failure.html { render :text => "I won't render!" }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def destroy
|
36
|
+
destroy! do |format|
|
37
|
+
format.html { render :text => "Destroyed!" }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
20
41
|
end
|
21
42
|
|
22
43
|
class AliasesBaseTest < TEST_CLASS
|
@@ -41,7 +62,7 @@ class AliasesBaseTest < TEST_CLASS
|
|
41
62
|
assert_equal 'New HTML', @response.body.strip
|
42
63
|
end
|
43
64
|
|
44
|
-
def
|
65
|
+
def test_expose_the_resquested_user_on_edit
|
45
66
|
Student.expects(:find).with('42').returns(mock_student)
|
46
67
|
get :edit, :id => '42'
|
47
68
|
assert_equal mock_student, assigns(:student)
|
@@ -63,6 +84,42 @@ class AliasesBaseTest < TEST_CLASS
|
|
63
84
|
assert_equal 'Render XML', @response.body
|
64
85
|
end
|
65
86
|
|
87
|
+
def test_is_not_redirected_on_create_with_success_if_success_block_is_given
|
88
|
+
Student.stubs(:new).returns(mock_student(:save => true))
|
89
|
+
@controller.stubs(:resource_url).returns('http://test.host/')
|
90
|
+
post :create
|
91
|
+
assert_response :success
|
92
|
+
assert_equal "I won't redirect!", @response.body
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_dumb_responder_with_quietly_receive_everything_on_failure
|
96
|
+
Student.stubs(:new).returns(mock_student(:save => false, :errors => []))
|
97
|
+
@controller.stubs(:resource_url).returns('http://test.host/')
|
98
|
+
post :create
|
99
|
+
assert_response :success
|
100
|
+
assert_template :edit
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
|
104
|
+
Student.stubs(:find).returns(mock_student(:update_attributes => false, :errors => []))
|
105
|
+
put :update
|
106
|
+
assert_response :success
|
107
|
+
assert_equal "I won't render!", @response.body
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_dumb_responder_with_quietly_receive_everything_on_success
|
111
|
+
Student.stubs(:find).returns(mock_student(:update_attributes => true))
|
112
|
+
put :update, :id => '42', :student => {:these => 'params'}
|
113
|
+
assert_equal mock_student, assigns(:student)
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_block_is_called_when_student_is_destroyed
|
117
|
+
Student.stubs(:find).returns(mock_student(:destroy => true))
|
118
|
+
delete :destroy
|
119
|
+
assert_response :success
|
120
|
+
assert_equal "Destroyed!", @response.body
|
121
|
+
end
|
122
|
+
|
66
123
|
protected
|
67
124
|
def mock_student(stubs={})
|
68
125
|
@mock_student ||= mock(stubs)
|
data/test/test_helper.rb
CHANGED
@@ -26,13 +26,6 @@ require File.dirname(__FILE__) + '/../lib/inherited_resources/respond_to.rb'
|
|
26
26
|
class ApplicationController < ActionController::Base; end
|
27
27
|
|
28
28
|
# Load InheritedResources::Base after defining ApplicationController
|
29
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/base_helpers.rb'
|
30
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/belongs_to.rb'
|
31
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/belongs_to_helpers.rb'
|
32
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/class_methods.rb'
|
33
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/polymorphic_helpers.rb'
|
34
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/singleton_helpers.rb'
|
35
|
-
require File.dirname(__FILE__) + '/../lib/inherited_resources/url_helpers.rb'
|
36
29
|
require File.dirname(__FILE__) + '/../lib/inherited_resources/base.rb'
|
37
30
|
|
38
31
|
# Define view_paths
|
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.
|
4
|
+
version: 0.2.0
|
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-02-
|
12
|
+
date: 2009-02-06 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/inherited_resources/belongs_to.rb
|
34
34
|
- lib/inherited_resources/belongs_to_helpers.rb
|
35
35
|
- lib/inherited_resources/class_methods.rb
|
36
|
+
- lib/inherited_resources/dumb_responder.rb
|
36
37
|
- lib/inherited_resources/polymorphic_helpers.rb
|
37
38
|
- lib/inherited_resources/respond_to.rb
|
38
39
|
- lib/inherited_resources/singleton_helpers.rb
|