adding first page controller and first revision of the Page model. also adding unit tests for the model.
git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@6 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7master
parent
0e8af78659
commit
da95e5dc7b
|
@ -0,0 +1,2 @@
|
|||
class PagesController < ApplicationController
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
module PagesHelper
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
##
|
||||
# This is a representation of a page of the wiki. My wiki is awfully simple
|
||||
# because i don't like too many features. This works and it has many different
|
||||
# associations. All of the major models has_one of these and it belongs_to
|
||||
# some kind of polymorphic owner.
|
||||
#
|
||||
# Bonuses:
|
||||
# * Uses RedCloth markup.
|
||||
# * Automatically converts/caches HTML
|
||||
# * Allows any character or space in the name
|
||||
# * Uses [[ and ]] for WikiWord representation.
|
||||
#
|
||||
class Page < ActiveRecord::Base
|
||||
acts_as_versioned
|
||||
|
||||
belongs_to :owner, :polymorphic => true
|
||||
validates_presence_of :title
|
||||
validates_uniqueness_of :title
|
||||
validates_format_of :title, :with => /[A-Za-z0-9 ]/,
|
||||
:message => 'may only contain letters, numbers and spaces'
|
||||
before_save :update_html
|
||||
|
||||
protected
|
||||
|
||||
##
|
||||
# Updates the HTML chunk from the RedCloth source.
|
||||
#
|
||||
def update_html
|
||||
# need to filter HTML first... remove <script> and chunks and the like...
|
||||
res = RedCloth.new(self.redcloth.to_s, [ :no_span_caps ])
|
||||
self.html = res.to_html(
|
||||
# no link references. messes up lines starting with [[WikiWord]]
|
||||
:block_textile_table, :block_textile_lists, :block_textile_prefix,
|
||||
:inline_textile_image, :inline_textile_link, :inline_textile_code,
|
||||
:inline_textile_span, :glyphs_textile)
|
||||
end
|
||||
end
|
|
@ -1,4 +1,6 @@
|
|||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :pages
|
||||
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
|
||||
# Sample of regular route:
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
class CreatePages < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :pages do |t|
|
||||
t.column :title, :string
|
||||
t.column :redcloth, :text
|
||||
t.column :html, :text
|
||||
t.column :owner_id, :integer
|
||||
t.column :owner_type, :string, :limit => 32
|
||||
t.column :version, :integer
|
||||
end
|
||||
add_index :pages, :owner_id
|
||||
Page.create_versioned_table
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :pages
|
||||
Page.drop_versioned_table
|
||||
end
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
||||
one:
|
||||
id: 1
|
||||
title: my title page for testing
|
||||
two:
|
||||
id: 2
|
||||
title: my other title page
|
|
@ -0,0 +1,20 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'pages_controller'
|
||||
|
||||
# Re-raise errors caught by the controller.
|
||||
class PagesController; def rescue_action(e) raise e end; end
|
||||
|
||||
class PagesControllerTest < Test::Unit::TestCase
|
||||
fixtures :pages
|
||||
|
||||
def setup
|
||||
@controller = PagesController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
# Replace this with your real tests.
|
||||
def test_truth
|
||||
assert true
|
||||
end
|
||||
end
|
|
@ -25,4 +25,16 @@ class Test::Unit::TestCase
|
|||
self.use_instantiated_fixtures = false
|
||||
|
||||
# Add more helper methods to be used by all tests here...
|
||||
def assert_create(class_name, params = {})
|
||||
obj = Class.class_eval(class_name).new(params)
|
||||
assert obj.save
|
||||
end
|
||||
|
||||
def assert_destroy(class_name, find = :first)
|
||||
c = Class.class_eval(class_name)
|
||||
obj = c.find(find)
|
||||
obj_count = c.count
|
||||
obj.destroy
|
||||
assert c.count == obj_count - 1
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class PageTest < Test::Unit::TestCase
|
||||
fixtures :pages
|
||||
|
||||
def test_create
|
||||
assert_create('Page', :title => 'my test page')
|
||||
end
|
||||
|
||||
def test_destroy
|
||||
assert_destroy('Page')
|
||||
end
|
||||
end
|
Reference in New Issue