ismasan (owner)

Forks

Revisions

gist: 33418 Download_button fork
public
Public Clone URL: git://gist.github.com/33418.git
Embed All Files: show embed
Ruby #
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
# 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