Skip to content

Commit

Permalink
added site update of status, and spec test
Browse files Browse the repository at this point in the history
  • Loading branch information
banane committed Feb 2, 2010
1 parent 8658c50 commit 29ef21f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -12,6 +12,7 @@
* Prevent admin from removing her own admin privilege [Jim Gay]
* Move to Rails 2.3.5 [Jim Gay]
* Simplify admin tab API [Jim Gay, John Long, Sean Cribbs]
* Add future publishing feature [Anna Billstrom]

=== 0.9.0 RC1 (October 9, 2009)

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -19,6 +19,7 @@ core:
* Daniel Beach
* Chris Parrish
* John Long
* Anna billstrom

=== 0.8.2 Lustery

Expand Down
20 changes: 17 additions & 3 deletions app/controllers/site_controller.rb
@@ -1,10 +1,8 @@
class SiteController < ApplicationController
skip_before_filter :verify_authenticity_token
no_login_required

cattr_writer :cache_timeout


def self.cache_timeout
@@cache_timeout ||= 5.minutes
end
Expand All @@ -16,8 +14,10 @@ def show_page
else
url = url.to_s
end


if @page = find_page(url)
batch_page_status_refresh if (url == "/" || url == "")
process_page(@page)
set_cache_control
@performed_render ||= true
Expand All @@ -29,6 +29,20 @@ def show_page
end

private
def batch_page_status_refresh
@changed_pages = []
@pages = Page.find(:all, :conditions => {:status_id => 90})
@pages.each do |page|
if page.published_at <= Time.now
page.status_id = 100
page.save
@changed_pages << page.id
end
end

expires_in nil, :private=>true, "no-cache" => true if @changed_pages.length > 0
end

def set_cache_control
if (request.head? || request.get?) && @page.cache? && live?
expires_in self.class.cache_timeout, :public => true, :private => false
Expand All @@ -37,7 +51,7 @@ def set_cache_control
headers['ETag'] = ''
end
end

def find_page(url)
found = Page.find_by_url(url, live?)
found if found and (found.published? or dev?)
Expand Down
30 changes: 19 additions & 11 deletions app/models/page.rb
Expand Up @@ -37,6 +37,7 @@ def initialize(message = 'Database missing root page'); super end

set_inheritance_column :class_name


def layout_with_inheritance
unless layout_without_inheritance
parent.layout if parent?
Expand Down Expand Up @@ -90,9 +91,14 @@ def inherits_part?(name)
def published?
status == Status[:published]
end


def status
Status.find(self.status_id)
status = Status.find(self.status_id)
if status == 90
puts "HELLO IN HERE"
end
status
end

def status=(value)
Expand Down Expand Up @@ -167,6 +173,18 @@ def find_by_url(url, live = true, clean = true)
end
end

def update_status
self[:published_at] = Time.now if self[:status_id] == 100 && self[:published_at] == nil

if self[:published_at] != nil
self[:status_id] = 90 if self[:published_at] > Time.now
self[:status_id] = 100 if self[:published_at] <= Time.now
end

true
end


def to_xml(options={}, &block)
super(options.reverse_merge(:include => :parts), &block)
end
Expand Down Expand Up @@ -254,16 +272,6 @@ def attributes_protected_by_default
super - [self.class.inheritance_column]
end

def update_status
self[:published_at] = Time.now if self[:status_id] == 100 && self[:published_at] == nil

if self[:published_at] != nil
self[:status_id] = 90 if self[:published_at] > Time.now
self[:status_id] = 100 if self[:published_at] <= Time.now
end

true
end

def update_virtual
unless self.class == Page.descendant_class(class_name)
Expand Down
31 changes: 29 additions & 2 deletions spec/controllers/site_controller_spec.rb
Expand Up @@ -72,14 +72,41 @@
response.should be_missing
end
end

it "should not require login" do
lambda { get :show_page, :url => '/' }.should_not require_login
end

describe "scheduling" do
before do
@sched_page = Page.find(103)
end
it "should not display scheduled pages on live site" do
@sched_page.published_at = Time.now + 5000
@sched_page.save
request.host = 'mysite.com'
get :show_page, :url => @sched_page.slug
response.response_code.should == 404
response.should render_template('site/not_found')
end

it "should update status of scheduled pages on home page" do
@sched_page.published_at = Time.now - 50000
@sched_page.status_id = 90

get :show_page, :url => '/'
response.body.should == 'Hello world!'

@sched_page2 = Page.find(103)
@sched_page2.status_id.should == 100
end

end


describe "caching" do
it "should add a default Cache-Control header with public and max-age of 5 minutes" do
get :show_page, :url => '/'
get :show_page, :url => ''
response.headers['Cache-Control'].should =~ /public/
response.headers['Cache-Control'].should =~ /max-age=300/
end
Expand Down

0 comments on commit 29ef21f

Please sign in to comment.