First steps for an own wiki.

* Scaffold for Page Objekt
* Using textile for rendering html
* Easy wiki-links with [[wikipage]]
This commit is contained in:
Benjamin Meichsner 2009-03-25 19:54:04 +01:00
parent 37199bae1d
commit f450070dbf
16 changed files with 252 additions and 1 deletions

View File

@ -0,0 +1,86 @@
class PagesController < ApplicationController
def index
@page = Page.find_by_permalink "home"
if @page
render :action => 'show'
else
redirect_to all_pages_path
end
end
def show
@page = Page.find_by_permalink(params[:permalink])
if @page.nil?
redirect_to new_page_path(:title => params[:permalink])
end
end
# GET /pages/new
# GET /pages/new.xml
def new
@page = Page.new(:title => params[:title])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @page }
end
end
# GET /pages/1/edit
def edit
@page = Page.find(params[:id])
end
# POST /pages
# POST /pages.xml
def create
@page = Page.new(params[:page])
respond_to do |format|
if @page.save
flash[:notice] = 'Page was successfully created.'
format.html { redirect_to(@page) }
format.xml { render :xml => @page, :status => :created, :location => @page }
else
format.html { render :action => "new" }
format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
end
end
end
# PUT /pages/1
# PUT /pages/1.xml
def update
@page = Page.find(params[:id])
respond_to do |format|
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
format.html { redirect_to(@page) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.xml
def destroy
@page = Page.find(params[:id])
@page.destroy
respond_to do |format|
format.html { redirect_to(pages_url) }
format.xml { head :ok }
end
end
def all
@pages = Page.all :order => 'created_at'
end
end

View File

@ -0,0 +1,18 @@
module PagesHelper
def wikified_body(body)
r = RedCloth.new(body)
r.gsub!(/\[\[(.*?)(\|(.*?))?\]\]/) { wiki_link($1, $3) }
sanitize r.to_html
r.to_html
end
def wiki_link(wiki_words, link_text = nil)
permalink = wiki_words.downcase.gsub(' ', '-')
if Page.exists?(:permalink => permalink)
link_to((link_text || wiki_words), wiki_page_url(permalink))
else
link_to((link_text || wiki_words), wiki_page_url(permalink), :class => "new_wiki_link")
end
end
end

12
app/models/page.rb Normal file
View File

@ -0,0 +1,12 @@
class Page < ActiveRecord::Base
validates_presence_of :title, :body
before_save :set_permalink
def set_permalink
if self.permalink.blank? #FIXME: or title.changed?
self.permalink = Page.count == 0 ? "home" : "#{title.downcase.strip.gsub(/ |\.|@/, '-')}"
end
end
end

View File

@ -16,6 +16,11 @@
{ :name => "Aufgaben", :url => "/tasks"}
]
},
{ :name => "Wiki", :url => "/pages", :active => ["pages", "wiki"],
:subnav => [
{ :name => "Alle Seiten", :url => "pages/all" }
]
},
{ :name => "Bestellungen", :url => u.ordergroup ? "/ordering/" : "/orders",
:active => ["orders", "ordering"],
:subnav => [

View File

@ -0,0 +1,15 @@
- form_for @page do |f|
= f.error_messages
%p
%b Title
%br/
= f.text_field :title
%p
%b Inhalt
%br/
= f.text_area :body, :size => "60x30"
%p
= f.submit "Speichern"
|
= link_to "Abbrechen", :back

View File

@ -0,0 +1,12 @@
- title "Alle Wikiseiten"
%p= link_to "Neue Seite anlegen", new_page_path
%table
%tr
%th Title
%th zuletzt aktualisiert
- for page in @pages
%tr
%td= link_to page.title, page
%td= format_date page.updated_at

View File

@ -0,0 +1,3 @@
- title "#{@page.title} bearbeiten"
= render :partial => 'form'

View File

@ -0,0 +1,3 @@
- title "Neue Wikiseite anlegen"
= render :partial => 'form'

View File

@ -0,0 +1,7 @@
- title @page.title
= wikified_body @page.body
%hr/
%p
= link_to "Seite bearbeiten", edit_page_path(@page)

View File

@ -68,6 +68,7 @@ Rails::Initializer.run do |config|
config.gem "fastercsv"
config.gem "prawn"
config.gem "haml", :version => '>=2.0.6'
config.gem 'RedCloth'
# The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.

View File

@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :pages, :collection => { :all => :get }
map.wiki_page "/wiki/:permalink", :controller => 'pages', :action => 'show'
map.logout '/logout', :controller => 'login', :action => 'logout'
map.my_profile '/home/profile', :controller => 'home', :action => 'profile'

View File

@ -0,0 +1,15 @@
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.string :title
t.text :body
t.string :permalink
t.timestamps
end
end
def self.down
drop_table :pages
end
end

View File

@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20090317175355) do
ActiveRecord::Schema.define(:version => 20090325175756) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false
@ -211,6 +211,14 @@ ActiveRecord::Schema.define(:version => 20090317175355) do
t.decimal "foodcoop_result", :precision => 8, :scale => 2
end
create_table "pages", :force => true do |t|
t.string "title"
t.text "body"
t.string "permalink"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "stock_changes", :force => true do |t|
t.integer "delivery_id"
t.integer "order_id"

11
test/fixtures/pages.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
title: MyString
body: MyText
permalink: MyString
two:
title: MyString
body: MyText
permalink: MyString

View File

@ -0,0 +1,45 @@
require 'test_helper'
class PagesControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:pages)
end
test "should get new" do
get :new
assert_response :success
end
test "should create page" do
assert_difference('Page.count') do
post :create, :page => { }
end
assert_redirected_to page_path(assigns(:page))
end
test "should show page" do
get :show, :id => pages(:one).id
assert_response :success
end
test "should get edit" do
get :edit, :id => pages(:one).id
assert_response :success
end
test "should update page" do
put :update, :id => pages(:one).id, :page => { }
assert_redirected_to page_path(assigns(:page))
end
test "should destroy page" do
assert_difference('Page.count', -1) do
delete :destroy, :id => pages(:one).id
end
assert_redirected_to pages_path
end
end

8
test/unit/page_test.rb Normal file
View File

@ -0,0 +1,8 @@
require 'test_helper'
class PageTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end