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:
resource_this / README
100644 187 lines (148 sloc) 4.742 kb
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
181
182
183
184
185
186
187
resouce_this
===========
 
Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
 
  class PostsController < ActionController::Base
   resource_this
  end
 
...will generate the following code:
 
  class PostsController < ActionController::Base
    before_filter :load_post, :only => [ :show, :edit, :update, :destroy ]
    before_filter :load_posts, :only => [ :index ]
    before_filter :new_post, :only => [ :new ]
    before_filter :create_post, :only => [ :create ]
    before_filter :update_post, :only => [ :update ]
    before_filter :destroy_post, :only => [ :destroy ]
 
  protected
    def load_post
      @post = Post.find(params[:id])
    end
  
    def new_post
      @post = Post.new
    end
  
    def create_post
      returning true do
        @post = Post.new(params[:post])
        @created = @post.save
      end
    end
  
    def update_post
      returning true do
        @updated = @post.update_attributes(params[:post])
      end
    end
  
    def destroy_post
      @post = @post.destroy
    end
  
    def load_posts
      @posts = Post.find(:all)
    end
  
  public
    def index
      respond_to do |format|
        format.html
        format.xml { render :xml => @posts }
        format.js
      end
    end
 
    def show
      respond_to do |format|
        format.html
        format.xml { render :xml => @post }
        format.js
      end
    end
 
    def new
      respond_to do |format|
        format.html { render :action => :edit }
        format.xml { render :xml => @post }
        format.js
      end
    end
 
    def create
      respond_to do |format|
        if @created
          flash[:notice] = 'Post was successfully created.'
          format.html { redirect_to @post }
          format.xml { render :xml => @post, :status => :created, :location => @post }
          format.js
        else
          format.html { render :action => :new }
          format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
          format.js
        end
      end
    end
 
    def edit
      respond_to do |format|
        format.html
        format.js
      end
    end
 
    def update
      respond_to do |format|
        if @updated
          flash[:notice] = 'Post was successfully updated.'
          format.html { redirect_to @post }
          format.xml { head :ok }
          format.js
        else
          format.html { render :action => :edit }
          format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
          format.js
        end
      end
    end
 
    def destroy
      respond_to do |format|
        format.html { redirect_to :action => posts_url }
        format.xml { head :ok }
        format.js
      end
    end
  end
 
Nested Resources
===========
 
  class CommentsController < ActionController::Base
    resource_this :nested => [:posts]
  end
 
This generates a very similar controller to the one above with adjusted redirects and one additional before_filter / loader method pair to grab the parent resource. In this case:
 
  before_filter :load_post
  
  def load_post
    @post = Post.find(params[:post_id])
  end
  
Sorting, etc
===========
 
  class CommentsController < ActionController::Base
    resource_this :finder_options => {:order => 'created_on'}
  end
  
...or, for lazily evaluated sorting options:
 
  class CommentsController < ActionController::Base
    resource_this :finder_options => Proc.new { finder_options }
    
  protected
  
    def finder_options
      order = case params[:sort]
        when 'date_reverse' then 'created_on desc'
        else 'created_on'
      end
      {:order => order, :limit => params[:limit] || 10 }
    end
    
  end
 
will_paginate
===========
 
will_paginate support is baked right in:
 
  class CommentsController < ActionController::Base
    resource_this :will_paginate => true
  end
  
This works with the :finder_options option as well
  
Opinionated Software
===========
  
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.
 
Generator
===========
 
A resource_this generator is included - does the same thing as the 'resource' generator but adds 'resource_this' to the generated controller.
 
Questions? Comments? Flames? Patches?
===========
jnewland@gmail.com
 
Copyright
===========
Copyright (c) 2007 Jesse Newland, released under the MIT license