Skip to content

Instantly share code, notes, and snippets.

@Pistos
Created February 8, 2009 07:00
Show Gist options
  • Save Pistos/60287 to your computer and use it in GitHub Desktop.
Save Pistos/60287 to your computer and use it in GitHub Desktop.
class Ramaze::Controller
Page = Model::Page
OldPage = Model::OldPage
end
class MainController < Ramaze::Controller
def index(title = 'Main', version = nil)
query = {:title => title}
query[:version] = version.to_i if version
model = version ? OldPage : Page
if page = model[query]
@text, @version = page.text, page.version
else
@text = "No Page known as '#{title}'"
@version = false
end
end
end
class PageController < Ramaze::Controller
map '/page'
helper :aspect
def create
change "Created Page '%s'" do |title, text|
Page.create(:title => title, :text => text, :version => 1)
redirect R(MainController, title)
end
end
def save
change "Updated Page '%s'" do |title, text|
page = Page[:title => title]
page.backup
page.text = text
page.version += 1
page.save
redirect R(MainController, title)
end
end
def delete(title)
page = Page[:title => title]
page.backup
page.delete
end
def rename(title, to)
change("Renamed #{title} to '%s'", to) do |title, text|
page = Page[:title => title]
page.backup
page.title = to
page.version += 1
page.save
end
end
def revert(title)
page = Page[:title => title]
page.revert
redirect R(MainController, title)
end
before :create, :save do
redirect_referer unless request.post?
end
private
def change(message, redirect_to = '/')
if title = request['title'] and text = request['text']
yield(title, text)
message % title
end
redirect(R(MainController, redirect_to))
end
end
<div id="text">
#@text
</div>
<?r if @version ?>
<div id="version">
Version: #@version
</div>
<?r end ?>
require 'sequel'
begin
case $wikore_db
when :memory
DB = Sequel.sqlite
else
DB_FILE = __DIR__('wikore.db')
DB = Sequel.connect("sqlite://#{DB_FILE}")
end
rescue NoMethodError
raise LoadError, 'Install latest Sequel gem'
end
module Model
PAGE_SCHEMA = lambda{
primary_key :id
boolean :active, :default => true
text :text
integer :version
}
class Page < Sequel::Model(:page)
set_schema do
instance_eval(&PAGE_SCHEMA)
text :title, :unique => true, :null => false
end
def backup
hash = @values.dup
hash.delete :id
OldPage.create(hash)
end
def revert
backup = OldPage[:title => title].values.dup
backup.delete :id
delete
self.class.create(backup)
end
end
class OldPage < Sequel::Model(:old_page)
set_schema do
instance_eval(&PAGE_SCHEMA)
text :title, :unique => false, :null => false
end
end
[Page, OldPage].each do |klass|
begin
klass.create_table
rescue Sequel::DatabaseError => e
if e.message !~ /table.*already exists/
raise e
end
end
end
end
require 'rubygems'
require 'ramaze'
$wikore = {}
require 'src/model'
require 'src/controller'
Ramaze.start
require 'ramaze'
require 'ramaze/spec/helper'
spec_require 'hpricot', 'sequel'
$LOAD_PATH.unshift base = __DIR__('..')
require 'start'
describe 'Wikore' do
behaves_like 'http'
ramaze :public_root => base/:public,
:view_root => base/:template
def check_redirect(to = '/')
response = yield
response.status.should == 302
response.body.should =~ /<a href="#{to}">/
end
def page_should_exist(name, *matches)
page = get("/#{name}")
page.status.should == 200
matches.each do |match|
page.body.should =~ match
end
end
it 'should have no Main page' do
page = get('/Main')
page.status.should == 200
page.body.should =~ /No Page known as 'Main'/
end
it 'should create a Main page' do
check_redirect '/Main' do
post('/page/create', 'title' => 'Main', 'text' => 'Newly created Main page')
end
matches = [
/Newly created Main page/,
/Version: 1/
]
page_should_exist('Main', *matches)
end
it 'should update Main page' do
check_redirect '/Main' do
post('/page/save', 'title' => 'Main', 'text' => 'Newly updated Main page')
end
matches = [
/Newly updated Main page/,
/Version: 2/
]
page_should_exist('Main', *matches)
end
it 'should maintain a backup' do
matches = [
/Newly created Main page/,
/Version: 1/
]
page_should_exist('Main/1', *matches)
end
it 'should revert' do
get('/page/revert/Main')
matches = [
/Newly created Main page/,
/Version: 1/
]
page_should_exist('Main', *matches)
end
it 'should incrememt version of Main page' do
(2..4).each do |n|
post('/page/save', 'title' => 'Main', 'text' => 'updated Main page')
matches = [ /updated Main page/, /Version: #{n}/ ]
page_should_exist('Main', *matches)
end
end
it 'should rename Main page to Other and back' do
check_redirect '/Other' do
get('/page/rename/Main/Other')
end
check_redirect '/Main' do
get('/page/rename/Other/Main')
end
end
it 'should delete Main page' do
get('/page/delete/Main')
page_should_exist('Main', /No Page known as 'Main'/)
end
it 'should fail if create/save is not POSTed to' do
check_redirect '/' do
get('/page/save', 'title' => 'Main', 'text' => 'Newly updated Main page')
end
check_redirect '/' do
get('/page/create', 'title' => 'Main', 'text' => 'Newly updated Main page')
end
end
FileUtils.rm_f(DB_FILE)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment