F6eddf2f983d23c2d031e407852625e9

Before / After / Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class PostsController < ResourceController::Base

  # block syntax
  create do
    before { @post.user = current_user }
    response do |wants|
      wants.html
      wants.js
    end
  end
  
  # chain syntax
  update.failure.wants.js
  
  # hybrid syntax
  update.failure do
    flash "There was a problem saving your object."
    wants.js
  end

end

Alternate Model/Route/Object Naming

1
2
3
4
5
6
7
8
9
10
11
12
class PostsController < ResourceController::Base
  private
    def model_name
      'blog_post'
    end
    
    def route_name
      'article'
    end
end

Alternate find syntax

1
2
3
4
5
6
7
8
9
10
11
12
class PostsController < ResourceController::Base
  private
    def object
      @object ||= end_of_association_chain.find_by_permalink(param)
    end
    
    def collection
      @collection ||= end_of_association_chain.find(:all, :page => {:current => params[:page], :size => 10})
    end
end

Specific Actions

1
2
3
4
5
class PostsController < ResourceController::Base
  actions :all, :except => :edit
end

Possible Parents

1
2
3
4
class PostsController < ResourceController::Base
  belongs_to :user, :category
end

Refactorings

No refactoring yet !

F6eddf2f983d23c2d031e407852625e9

jamesgolick

November 5, 2007, November 05, 2007 20:28, permalink

No rating. Login to rate!

First refactoring from my blog...

Alternate Model/Route/Object Naming

1
2
3
4
5
class PostsController < ResourceController::Base
  model_name :blog_post
  route_name :article
end
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

November 5, 2007, November 05, 2007 22:24, permalink

1 rating. Login to rate!

A couple of suggestions. I'm used to the default respond_to rather then wants. And I don't know if the *_finder macros are even possible, but would look like a true dsl

Before / After / Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PostsController < ApplicationController
  acts_as_resource_controller

  # block syntax
  create do
    before { @post.user = current_user }
    respond_to do |format|
      format.html
      format.js
    end
  end
  
  # chain syntax
  update.failure.respond_to %w(js html)
end

Alternate find syntax

1
2
3
4
5
class PostsController < ResourceController::Base
  object_finder :find_by_permalink
  collection_finder { find :all, :page => {:current => params[:page], :size => 10} }
end
F6eddf2f983d23c2d031e407852625e9

jamesgolick

November 5, 2007, November 05, 2007 22:42, permalink

No rating. Login to rate!

I love those. The *_finder macros are definitely possible. I really like the acts_as_resource_controller too. Currently, that syntax is just resource_controller, which I'm not crazy about.

I agree with you about the respond_to, although, I think for open syntax it should be responds_to, just because it reads like english - update responds to js and html. OTOH, I don't see much of a problem with having all of these names as aliases, and so I think I'll probably do that.

1
update.failure.responds_to %(js html)
880cbab435f00197613c9cc2065b4f5a

danielharan

November 6, 2007, November 06, 2007 01:02, permalink

No rating. Login to rate!

I prefer responds_to. Since we're on the topic, how about making the block optional?

1
2
3
4
5
6
  create do
    before { @post.user = current_user }
    responds_to :html, :js do |format|
      format.gif { create_gif(@post) }
    end
  end

Your refactoring





Format Copy from initial code

or Cancel