public
Description: Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
Clone URL: git://github.com/jnewland/resource_this.git
Search Repo:
lots of work to improve nested resource support
jnewland (author)
Mon Mar 03 13:51:18 -0800 2008
commit  5f3af150a298bb3fe0bb99f1709b139f3fd81a8d
tree    76cbe431b86efa0a3860d5c8f812dfd608f88dda
parent  0f80f8e919d74f95fa4e64759491b5654eed5ebe
...
12
13
14
15
16
17
 
 
 
 
18
19
20
21
22
23
24
25
26
27
 
 
 
 
 
 
28
29
30
31
32
33
34
35
36
 
37
38
39
40
41
42
43
44
45
46
47
...
42
43
44
 
45
 
 
 
 
 
46
47
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
56
57
58
 
59
60
61
62
 
63
64
65
66
67
 
68
69
70
71
72
...
80
81
82
83
84
85
86
87
88
 
89
90
91
92
93
94
95
 
96
97
98
...
127
128
129
130
131
 
 
132
133
134
...
149
150
151
152
 
153
154
155
...
162
163
164
165
 
166
167
168
...
12
13
14
 
 
 
15
16
17
18
19
20
21
22
23
 
 
 
 
 
24
25
26
27
28
29
30
31
32
33
 
 
 
 
 
34
35
36
37
38
39
40
41
42
43
44
45
...
40
41
42
43
44
45
46
47
48
49
50
51
 
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
96
97
98
99
 
100
101
102
103
104
 
105
106
107
108
109
110
...
118
119
120
 
121
122
123
 
 
124
125
126
127
128
129
 
 
130
131
132
133
...
162
163
164
 
 
165
166
167
168
169
...
184
185
186
 
187
188
189
190
...
197
198
199
 
200
201
202
203
0
@@ -12,28 +12,26 @@
0
       class_name = options[:class_name] || singular_name.camelize
0
       plural_name = singular_name.pluralize
0
       will_paginate_index = options[:will_paginate] || false
0
- url_string = "#{singular_name}_url(@#{singular_name})"
0
- list_url_string = "#{plural_name}_url"
0
- finder_base = class_name
0
+ resource_url = "#{singular_name}_url(@#{singular_name})"
0
+ collection_url = "#{plural_name}_url"
0
+ resource_url = options[:path_prefix] + resource_url unless options[:path_prefix].nil?
0
+ collection_url = options[:path_prefix] + collection_url unless options[:path_prefix].nil?
0
       
0
       class_inheritable_accessor :resource_this_finder_options
0
       self.resource_this_finder_options = options[:finder_options] || {}
0
       
0
       unless options[:nested].nil?
0
- nested = options[:nested].to_s.singularize
0
- nested_class = nested.camelize
0
- url_string = "#{nested}_#{singular_name}_url(" + [nested, singular_name].map { |route| "@#{route}"}.join(', ') + ')'
0
- list_url_string = "#{nested}_#{plural_name}_url(@#{nested})"
0
- finder_base = "@#{nested}.#{plural_name}"
0
+ nested = options[:nested].to_s.singularize
0
+ nested_class = nested.camelize
0
+ nested_resource_url = "#{nested}_#{singular_name}_url(" + [nested, singular_name].map { |route| "@#{route}"}.join(', ') + ')'
0
+ nested_collection_url = "#{nested}_#{plural_name}_url(@#{nested})"
0
+ nested_resource_url = options[:path_prefix] + nested_resource_url unless options[:path_prefix].nil?
0
+ nested_collection_url = options[:path_prefix] + nested_collection_url unless options[:path_prefix].nil?
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
           before_filter :load_#{nested}
0
         end_eval
0
       end
0
-
0
- #process path_prefix
0
- url_string = options[:path_prefix] + url_string unless options[:path_prefix].nil?
0
- list_url_string = options[:path_prefix] + list_url_string unless options[:path_prefix].nil?
0
-
0
+
0
       #standard before_filters
0
       module_eval <<-"end_eval", __FILE__, __LINE__
0
         before_filter :load_#{singular_name}, :only => [ :show, :edit, :update, :destroy ]
0
0
0
0
0
0
0
0
0
@@ -42,29 +40,69 @@
0
         before_filter :create_#{singular_name}, :only => [ :create ]
0
         before_filter :update_#{singular_name}, :only => [ :update ]
0
         before_filter :destroy_#{singular_name}, :only => [ :destroy ]
0
+
0
       protected
0
+
0
+ def finder_options
0
+ resource_this_finder_options.class == Proc ? resource_this_finder_options.call : {}
0
+ end
0
+
0
       end_eval
0
       
0
- unless options[:nested].nil?
0
+ if options[:nested].nil?
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
+ def finder_base
0
+ #{class_name}
0
+ end
0
+
0
+ def collection
0
+ #{class_name}.find(:all, finder_options)
0
+ end
0
+
0
+ def collection_url
0
+ #{collection_url}
0
+ end
0
+
0
+ def resource_url
0
+ #{resource_url}
0
+ end
0
+ end_eval
0
+ else
0
+ module_eval <<-"end_eval", __FILE__, __LINE__
0
           def load_#{nested}
0
- @#{nested} = #{nested_class}.find(params[:#{nested}_id])
0
+ @#{nested} = #{nested_class}.find(params[:#{nested}_id]) rescue nil
0
           end
0
+
0
+ def finder_base
0
+ @#{nested}.nil? ? #{class_name} : @#{nested}.#{plural_name}
0
+ end
0
+
0
+ def collection
0
+ @#{nested}.nil? ? #{class_name}.find(:all, finder_options) : @#{nested}.#{plural_name}.find(:all, finder_options)
0
+ end
0
+
0
+ def collection_url
0
+ @#{nested}.nil? ? #{collection_url} : #{nested_collection_url}
0
+ end
0
+
0
+ def resource_url
0
+ @#{nested}.nil? ? #{resource_url} : #{nested_resource_url}
0
+ end
0
         end_eval
0
       end
0
       
0
       module_eval <<-"end_eval", __FILE__, __LINE__
0
         def load_#{singular_name}
0
- @#{singular_name} = #{finder_base}.find(params[:id])
0
+ @#{singular_name} = finder_base.find(params[:id])
0
         end
0
         
0
         def new_#{singular_name}
0
- @#{singular_name} = #{finder_base}.new
0
+ @#{singular_name} = finder_base.new
0
         end
0
         
0
         def create_#{singular_name}
0
           returning true do
0
- @#{singular_name} = #{finder_base}.new(params[:#{singular_name}])
0
+ @#{singular_name} = finder_base.new(params[:#{singular_name}])
0
             @created = @#{singular_name}.save
0
           end
0
         end
0
0
0
@@ -80,19 +118,16 @@
0
         end
0
       end_eval
0
             
0
- #TODO: add sorting customizable by subclassed controllers
0
       if will_paginate_index
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
           def load_#{plural_name}
0
- finder_options = resource_this_finder_options.class == Proc ? resource_this_finder_options.call : resource_this_finder_options
0
- @#{plural_name} = #{finder_base}.paginate(finder_options.merge(:page => params[:page]))
0
+ @#{plural_name} = finder_base.paginate(finder_options.merge(:page => params[:page]))
0
           end
0
         end_eval
0
       else
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
           def load_#{plural_name}
0
- finder_options = resource_this_finder_options.class == Proc ? resource_this_finder_options.call : resource_this_finder_options
0
- @#{plural_name} = #{finder_base}.find(:all, finder_options)
0
+ @#{plural_name} = collection
0
           end
0
         end_eval
0
       end
0
@@ -127,8 +162,8 @@
0
           respond_to do |format|
0
             if @created
0
               flash[:notice] = '#{class_name} was successfully created.'
0
- format.html { redirect_to #{url_string} }
0
- format.xml { render :xml => @#{singular_name}, :status => :created, :location => #{url_string} }
0
+ format.html { redirect_to(resource_url) }
0
+ format.xml { render :xml => @#{singular_name}, :status => :created, :location => resource_url }
0
               format.js
0
             else
0
               format.html { render :action => :edit }
0
@@ -149,7 +184,7 @@
0
           respond_to do |format|
0
             if @updated
0
               flash[:notice] = '#{class_name} was successfully updated.'
0
- format.html { redirect_to #{url_string} }
0
+ format.html { redirect_to(resource_url) }
0
               format.xml { head :ok }
0
               format.js
0
             else
0
@@ -162,7 +197,7 @@
0
 
0
         def destroy
0
           respond_to do |format|
0
- format.html { redirect_to #{list_url_string} }
0
+ format.html { redirect_to(collection_url) }
0
             format.xml { head :ok }
0
             format.js
0
           end
...
7
8
9
 
10
 
11
12
13
14
 
15
16
17
18
...
23
24
25
26
27
 
 
28
29
 
 
 
 
 
 
 
30
31
32
33
...
34
35
36
 
 
 
 
 
 
 
37
38
39
 
40
41
42
43
...
46
47
48
49
 
50
51
52
53
 
 
 
 
 
 
 
 
54
55
56
...
58
59
60
 
 
 
 
 
 
 
61
62
63
...
71
72
73
 
 
 
 
 
 
74
75
76
...
84
85
86
 
 
 
 
 
 
 
 
87
88
...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
26
27
28
 
 
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
44
45
46
47
48
49
50
51
52
53
54
55
 
56
57
58
59
60
...
63
64
65
 
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
83
84
85
86
87
88
89
90
91
92
93
94
95
...
103
104
105
106
107
108
109
110
111
112
113
114
...
122
123
124
125
126
127
128
129
130
131
132
133
134
0
@@ -7,11 +7,14 @@
0
     @request.accept = 'application/xml'
0
     @response = ActionController::TestResponse.new
0
     @first = Post.create(:title => "test", :body => "test")
0
+ @second = Post.create(:title => "test2", :body => "test2")
0
     @first_comment = Comment.create(:post => @first, :body => "test")
0
+ @second_comment = Comment.create(:post => @second, :body => "test")
0
     ActionController::Routing::Routes.draw do |map|
0
       map.resources :posts do |post|
0
         post.resources :comments
0
       end
0
+ map.resources :comments
0
     end
0
   end
0
   
0
0
@@ -23,10 +26,17 @@
0
   def test_should_get_index
0
     get :index, :post_id => @first.id
0
     assert_response :success
0
- assert assigns(:comments)
0
- assert assigns(:post)
0
+ assert_equal @first.comments, assigns(:comments)
0
+ assert_equal @first, assigns(:post)
0
   end
0
 
0
+ def test_should_get_index_without_post
0
+ get :index
0
+ assert_response :success
0
+ assert assigns(:post).nil?
0
+ assert_equal Comment.find(:all), assigns(:comments)
0
+ end
0
+
0
   def test_should_get_new
0
     get :new, :post_id => @first.id
0
     assert_response :success
0
0
@@ -34,9 +44,16 @@
0
     assert assigns(:post)
0
   end
0
 
0
+ def test_should_get_new_without_post
0
+ get :new
0
+ assert_response :success
0
+ assert assigns(:post).nil?
0
+ assert assigns(:comment)
0
+ end
0
+
0
   def test_should_create_comment
0
     assert_difference('Comment.count') do
0
- post :create, :post_id => @first.id, :post => { :body => "test" }
0
+ post :create, :post_id => @first.id, :comment => { :body => "test" }
0
     end
0
     assert_response :created
0
     assert assigns(:comment)
0
0
@@ -46,11 +63,19 @@
0
   def test_should_create_comment_html
0
     @request.accept = 'text/html'
0
     assert_difference('Comment.count') do
0
- post :create, :post_id => @first.id, :post => { :body => "test" }
0
+ post :create, :post_id => @first.id, :comment => { :body => "test" }
0
     end
0
     assert_redirected_to "posts/#{assigns(:post).id}/comments/#{assigns(:comment).id}"
0
   end
0
 
0
+ def test_should_create_comment_html_without_post
0
+ @request.accept = 'text/html'
0
+ assert_difference('Comment.count') do
0
+ post :create, :comment => { :body => "test" }
0
+ end
0
+ assert_redirected_to "/comments/#{assigns(:comment).id}"
0
+ end
0
+
0
   def test_should_show_comment
0
     get :show, :post_id => @first.id, :id => @first_comment.id
0
     assert_response :success
0
@@ -58,6 +83,13 @@
0
     assert assigns(:post)
0
   end
0
 
0
+ def test_should_show_comment_without_post
0
+ get :show, :id => @first_comment.id
0
+ assert_response :success
0
+ assert_equal Comment.find(@first_comment.id), assigns(:comment)
0
+ assert assigns(:post).nil?
0
+ end
0
+
0
   def test_should_update_comment
0
     put :update, :post_id => @first.id, :id => @first_comment.id, :comment => { :post => @first, :body => "test" }
0
     assert_response :success
0
@@ -71,6 +103,12 @@
0
     assert_redirected_to "posts/#{assigns(:post).id}/comments/#{assigns(:comment).id}"
0
   end
0
 
0
+ def test_should_update_comment_html_without_post
0
+ @request.accept = 'text/html'
0
+ put :update, :id => @first_comment.id, :comment => { :post => @first, :body => "test2" }
0
+ assert_redirected_to "/comments/#{assigns(:comment).id}"
0
+ end
0
+
0
   def test_should_destroy_comment
0
     assert_difference('Comment.count', -1) do
0
       delete :destroy, :post_id => @first.id, :id => @first_comment.id
0
@@ -84,6 +122,14 @@
0
       delete :destroy, :post_id => @first.id, :id => @first_comment.id
0
     end
0
     assert_redirected_to "posts/#{assigns(:post).id}/comments"
0
+ end
0
+
0
+ def test_should_destroy_html_without_post
0
+ @request.accept = 'text/html'
0
+ assert_difference('Comment.count', -1) do
0
+ delete :destroy, :id => @first_comment.id
0
+ end
0
+ assert_redirected_to "/comments"
0
   end
0
 end
...
27
28
29
30
31
 
 
32
33
34
...
27
28
29
 
 
30
31
32
33
34
0
@@ -27,8 +27,8 @@
0
     assert_equal @a, assigns(:widgets).first
0
   end
0
   
0
- def test_should_get_index_sorted
0
- @controller.resource_this_finder_options = {:order => 'body'}
0
+ def test_should_get_index_sorted_with_inline_proc
0
+ @controller.resource_this_finder_options = Proc.new { { :order => 'body' } }
0
     get :index
0
     assert_response :success
0
     assert assigns(:widgets)
...
15
16
17
18
 
 
19
20
21
...
15
16
17
 
18
19
20
21
22
0
@@ -15,7 +15,8 @@
0
 RAILS_ROOT = '.' unless defined? RAILS_ROOT
0
 RAILS_ENV = 'test' unless defined? RAILS_ENV
0
 
0
-
0
+ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV['DEBUG']
0
+ActionController::Base.logger = Logger.new(STDOUT) if ENV['DEBUG']
0
 
0
 ActionController::Base.send :include, ResourceThis
0
 

Comments

    No one has commented yet.