From f450070dbf43f5f9051df2800e10851d4dd98642 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Wed, 25 Mar 2009 19:54:04 +0100 Subject: [PATCH] First steps for an own wiki. * Scaffold for Page Objekt * Using textile for rendering html * Easy wiki-links with [[wikipage]] --- app/controllers/pages_controller.rb | 86 +++++++++++++++++++++++ app/helpers/pages_helper.rb | 18 +++++ app/models/page.rb | 12 ++++ app/views/layouts/_main_tabnav.html.erb | 5 ++ app/views/pages/_form.html.haml | 15 ++++ app/views/pages/all.html.haml | 12 ++++ app/views/pages/edit.html.haml | 3 + app/views/pages/new.html.haml | 3 + app/views/pages/show.html.haml | 7 ++ config/environment.rb | 1 + config/routes.rb | 2 + db/migrate/20090325175756_create_pages.rb | 15 ++++ db/schema.rb | 10 ++- test/fixtures/pages.yml | 11 +++ test/functional/pages_controller_test.rb | 45 ++++++++++++ test/unit/page_test.rb | 8 +++ 16 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 app/controllers/pages_controller.rb create mode 100644 app/helpers/pages_helper.rb create mode 100644 app/models/page.rb create mode 100644 app/views/pages/_form.html.haml create mode 100644 app/views/pages/all.html.haml create mode 100644 app/views/pages/edit.html.haml create mode 100644 app/views/pages/new.html.haml create mode 100644 app/views/pages/show.html.haml create mode 100644 db/migrate/20090325175756_create_pages.rb create mode 100644 test/fixtures/pages.yml create mode 100644 test/functional/pages_controller_test.rb create mode 100644 test/unit/page_test.rb diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb new file mode 100644 index 00000000..e4e19beb --- /dev/null +++ b/app/controllers/pages_controller.rb @@ -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 diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb new file mode 100644 index 00000000..a2e75b51 --- /dev/null +++ b/app/helpers/pages_helper.rb @@ -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 diff --git a/app/models/page.rb b/app/models/page.rb new file mode 100644 index 00000000..53816498 --- /dev/null +++ b/app/models/page.rb @@ -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 diff --git a/app/views/layouts/_main_tabnav.html.erb b/app/views/layouts/_main_tabnav.html.erb index feb2604e..a0d2f3f4 100644 --- a/app/views/layouts/_main_tabnav.html.erb +++ b/app/views/layouts/_main_tabnav.html.erb @@ -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 => [ diff --git a/app/views/pages/_form.html.haml b/app/views/pages/_form.html.haml new file mode 100644 index 00000000..9e32c461 --- /dev/null +++ b/app/views/pages/_form.html.haml @@ -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 \ No newline at end of file diff --git a/app/views/pages/all.html.haml b/app/views/pages/all.html.haml new file mode 100644 index 00000000..9af42995 --- /dev/null +++ b/app/views/pages/all.html.haml @@ -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 diff --git a/app/views/pages/edit.html.haml b/app/views/pages/edit.html.haml new file mode 100644 index 00000000..23d473d7 --- /dev/null +++ b/app/views/pages/edit.html.haml @@ -0,0 +1,3 @@ +- title "#{@page.title} bearbeiten" + += render :partial => 'form' \ No newline at end of file diff --git a/app/views/pages/new.html.haml b/app/views/pages/new.html.haml new file mode 100644 index 00000000..a7ac7eae --- /dev/null +++ b/app/views/pages/new.html.haml @@ -0,0 +1,3 @@ +- title "Neue Wikiseite anlegen" + += render :partial => 'form' \ No newline at end of file diff --git a/app/views/pages/show.html.haml b/app/views/pages/show.html.haml new file mode 100644 index 00000000..cfd6eeef --- /dev/null +++ b/app/views/pages/show.html.haml @@ -0,0 +1,7 @@ +- title @page.title + += wikified_body @page.body + +%hr/ +%p + = link_to "Seite bearbeiten", edit_page_path(@page) \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index e8a510c7..af17606d 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -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. diff --git a/config/routes.rb b/config/routes.rb index e44693bc..0bab7696 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/db/migrate/20090325175756_create_pages.rb b/db/migrate/20090325175756_create_pages.rb new file mode 100644 index 00000000..cca1e419 --- /dev/null +++ b/db/migrate/20090325175756_create_pages.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 514a422f..92c4a70d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml new file mode 100644 index 00000000..9a2e19cb --- /dev/null +++ b/test/fixtures/pages.yml @@ -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 diff --git a/test/functional/pages_controller_test.rb b/test/functional/pages_controller_test.rb new file mode 100644 index 00000000..97ca1b62 --- /dev/null +++ b/test/functional/pages_controller_test.rb @@ -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 diff --git a/test/unit/page_test.rb b/test/unit/page_test.rb new file mode 100644 index 00000000..b12dd9a6 --- /dev/null +++ b/test/unit/page_test.rb @@ -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