Created
December 8, 2008 10:28
-
-
Save ismasan/33418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# we want /posts, /posts/published, /posts/drafts, etc. without too much duplication | |
# model | |
class Post < ActiveRecord::Base | |
named_scope :published, lambda {{:conditions => ...}} | |
named_scope :draft, lambda {{:conditions => ...}} | |
named_scope :expired, lambda {{:conditions => ...}} | |
named_scope :upcoming, lambda {{:conditions => ...}} | |
end | |
# app controller | |
class ApplicationController < ActionController::Base | |
protected | |
# this creates actions for each publish status, which call find_resources with the correct scope | |
def self.publish_status_actions | |
[:published, :draft, :expired, :upcoming].each do |action| | |
define_method action do | |
self.resources = find_resources(action) | |
render :action => :index | |
end | |
end | |
end | |
def find_resources(scope = :all) | |
resource_service.send(scope).paginate(:page => params[:page]) | |
end | |
end | |
# posts controller | |
class PostsController < ApplicationController | |
resources_controller :posts | |
publish_status_actions | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment