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:
sorting

git-svn-id: 
http://jnewland.com/svn/public/ruby/rails/plugins/resource_this@43 
9b6b69f6-dd27-0410-8144-a0f3c56a22ea
jnewland (author)
Mon Oct 15 07:33:33 -0700 2007
commit  456fa6eaf9925653336b1608bd6132e0f7d2d52c
tree    ce8fbb7f9a80feb36aec6df89e79630eaee7cb4f
parent  93a28c0993f6b13ea244800452b883f51dfcaf58
0
...
118
119
120
121
 
 
122
123
124
125
...
132
133
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
 
 
136
137
138
...
118
119
120
 
121
122
123
124
125
126
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
0
@@ -118,7 +118,8 @@
0
     end
0
   end
0
 
0
-Nested resources like so:
0
+Nested Resources
0
+===========
0
 
0
   class CommentsController < ActionController::Base
0
     resource_this :nested => [:posts]
0
0
@@ -132,7 +133,48 @@
0
     @post = Post.find(params[:post_id])
0
   end
0
   
0
+Sorting, etc
0
+===========
0
+
0
+ class CommentsController < ActionController::Base
0
+ resource_this :finder_options => {:order => 'created_on'}
0
+ end
0
+
0
+...or, for lazily evaluated sorting options:
0
+
0
+ class CommentsController < ActionController::Base
0
+ resource_this :finder_options => Proc.new { finder_options }
0
+
0
+ protected
0
+
0
+ def finder_options
0
+ order = case params[:sort]
0
+ when 'date_reverse' then 'created_on desc'
0
+ else 'created_on'
0
+ end
0
+ {:order => order, :limit => params[:limit] || 10 }
0
+ end
0
+
0
+ end
0
+
0
+will_paginate
0
+===========
0
+
0
+will_paginate support is baked right in:
0
+
0
+ class CommentsController < ActionController::Base
0
+ resource_this :will_paginate => true
0
+ end
0
+
0
+This works with the :finder_options option as well
0
+
0
+Opinionated Software
0
+===========
0
+
0
 The separation of logic - DB operations in before_filters, rendering in the standard resource controller methods - makes this approach ridiculously easy to customize. Need to load an additional object for the :show action? Slap another before_filter on it. Need to change the path that the :update action redirects to? Override the :update action with your new rendering behavior.
0
+
0
+Generator
0
+===========
0
 
0
 A resource_this generator is included - does the same thing as the 'resource' generator but adds 'resource_this' to the generated controller.
0
 
0
...
1
2
...
 
 
0
@@ -1,3 +1 @@
0
-* Document will_paginate support.
0
-* Add logical sorting support to the :index action.
...
5
6
7
8
 
9
10
11
...
16
17
18
 
 
 
19
20
21
22
...
81
82
83
84
 
 
85
86
87
88
89
90
 
 
91
92
93
...
5
6
7
 
8
9
10
11
...
16
17
18
19
20
21
22
23
24
25
...
84
85
86
 
87
88
89
90
91
92
93
 
94
95
96
97
98
0
@@ -5,7 +5,7 @@
0
 
0
   module ClassMethods
0
     def resource_this(options = {})
0
- options.assert_valid_keys(:class_name, :will_paginate, :sort_method, :nested, :path_prefix)
0
+ options.assert_valid_keys(:class_name, :will_paginate, :finder_options, :nested, :path_prefix)
0
 
0
       singular_name = controller_name.singularize
0
       singular_name = options[:class_name].downcase.singularize unless options[:class_name].nil?
0
@@ -16,6 +16,9 @@
0
       list_url_string = "#{plural_name}_url"
0
       finder_base = class_name
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
0
@@ -81,13 +84,15 @@
0
       if will_paginate_index
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
           def load_#{plural_name}
0
- @#{plural_name} = #{finder_base}.paginate(:page => params[:page])
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
           end
0
         end_eval
0
       else
0
         module_eval <<-"end_eval", __FILE__, __LINE__
0
           def load_#{plural_name}
0
- @#{plural_name} = #{finder_base}.find(:all)
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
           end
0
         end_eval
0
       end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
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
48
49
50
51
52
53
0
@@ -1 +1,54 @@
0
+require File.join(File.dirname(__FILE__), 'test_helper')
0
+
0
+class ResourceThisSortingTest < Test::Unit::TestCase
0
+ def setup
0
+ @controller = WidgetsController.new
0
+ @request = ActionController::TestRequest.new
0
+ @request.accept = 'application/xml'
0
+ @response = ActionController::TestResponse.new
0
+ @a = Widget.create(:title => "aaa", :body => "zzz")
0
+ @z = Widget.create(:title => "zzz", :body => "aaa")
0
+ 100.times do
0
+ Widget.create(:title => "foo", :body => "bar")
0
+ end
0
+ with_routing do |set|
0
+ set.draw do |map|
0
+ map.resources :widgets
0
+ end
0
+ end
0
+ end
0
+
0
+ def teardown
0
+ Widget.find(:all).each { |w| w.destroy }
0
+ end
0
+
0
+ def test_should_get_index
0
+ get :index
0
+ assert_response :success
0
+ assert assigns(:widgets)
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
+ get :index
0
+ assert_response :success
0
+ assert assigns(:widgets)
0
+ assert_equal @z, assigns(:widgets).first
0
+ end
0
+
0
+ def test_should_get_index_sorted_with_proc
0
+ @controller.resource_this_finder_options = Proc.new { finder_options }
0
+ get :index
0
+ assert_response :success
0
+ assert assigns(:widgets)
0
+ assert_equal 2, assigns(:widgets).size
0
+ assert_equal @z, assigns(:widgets).first
0
+ end
0
+
0
+ def finder_options
0
+ {:order => 'body', :limit => 2}
0
+ end
0
+
0
+end
...
1
2
3
 
4
5
6
...
17
18
19
 
 
 
 
 
 
20
21
22
23
...
33
34
35
 
 
 
 
 
36
 
 
 
 
37
38
39
...
1
2
 
3
4
5
6
...
17
18
19
20
21
22
23
24
25
26
27
28
29
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
0
@@ -1,6 +1,6 @@
0
 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
 require 'action_controller/test_process'
0
-require 'active_record/fixtures'
0
+require 'test/unit'
0
 
0
 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
0
 
0
@@ -17,6 +17,12 @@
0
     t.column :created_at, :datetime
0
     t.column :updated_at, :datetime
0
   end
0
+ create_table :widgets do |t|
0
+ t.column :title, :string
0
+ t.column :body, :text
0
+ t.column :created_at, :datetime
0
+ t.column :updated_at, :datetime
0
+ end
0
 end
0
 
0
 class Post < ActiveRecord::Base
0
0
@@ -33,7 +39,16 @@
0
   validates_associated :post
0
 end
0
 
0
+class Widget < ActiveRecord::Base
0
+ def validate
0
+ end
0
+end
0
+
0
 class PostsController < ActionController::Base
0
+ resource_this
0
+end
0
+
0
+class WidgetsController < ActionController::Base
0
   resource_this
0
 end
0
 

Comments

    No one has commented yet.