Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created December 8, 2008 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ismasan/33418 to your computer and use it in GitHub Desktop.
Save ismasan/33418 to your computer and use it in GitHub Desktop.
# 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