Commit 4c1b8558df1a874716989b8217ab0acf97d6da04
1 parent
eacea15a
Exists in
master
and in
4 other branches
Wiki: base implemetation logic
Showing
8 changed files
with
35 additions
and
68 deletions
Show diff stats
app/controllers/wikis_controller.rb
| 1 | 1 | class WikisController < ApplicationController |
| 2 | 2 | before_filter :project |
| 3 | 3 | layout "project" |
| 4 | - respond_to :html | |
| 5 | 4 | |
| 6 | 5 | def show |
| 7 | - @wiki = @project.wikis.find_by_slug(params[:id]) | |
| 8 | - respond_with(@wiki) | |
| 9 | - end | |
| 10 | - | |
| 11 | - def new | |
| 12 | - @wiki = Wiki.new | |
| 13 | - | |
| 6 | + @wiki = @project.wikis.where(:slug => params[:id]).order("created_at").last | |
| 14 | 7 | respond_to do |format| |
| 15 | - format.html # new.html.erb | |
| 16 | - format.json { render json: @wiki } | |
| 8 | + if @wiki | |
| 9 | + format.html | |
| 10 | + else | |
| 11 | + @wiki = @project.wikis.new(:slug => params[:id]) | |
| 12 | + format.html { render "edit" } | |
| 13 | + end | |
| 17 | 14 | end |
| 18 | 15 | end |
| 19 | 16 | |
| 20 | 17 | def edit |
| 21 | - @wiki = Wiki.find(params[:id]) | |
| 18 | + @wiki = @project.wikis.where(:slug => params[:id]).order("created_at").last | |
| 19 | + @wiki = Wiki.regenerate_from @wiki | |
| 22 | 20 | end |
| 23 | 21 | |
| 24 | 22 | def create |
| 25 | - @wiki = Wiki.new(params[:wiki]) | |
| 23 | + @wiki = @project.wikis.new(params[:wiki]) | |
| 26 | 24 | |
| 27 | 25 | respond_to do |format| |
| 28 | 26 | if @wiki.save |
| 29 | - format.html { redirect_to @wiki, notice: 'Wiki was successfully created.' } | |
| 30 | - format.json { render json: @wiki, status: :created, location: @wiki } | |
| 31 | - else | |
| 32 | - format.html { render action: "new" } | |
| 33 | - format.json { render json: @wiki.errors, status: :unprocessable_entity } | |
| 34 | - end | |
| 35 | - end | |
| 36 | - end | |
| 37 | - | |
| 38 | - def update | |
| 39 | - @wiki = Wiki.find(params[:id]) | |
| 40 | - | |
| 41 | - respond_to do |format| | |
| 42 | - if @wiki.update_attributes(params[:wiki]) | |
| 43 | - format.html { redirect_to @wiki, notice: 'Wiki was successfully updated.' } | |
| 44 | - format.json { head :no_content } | |
| 27 | + format.html { redirect_to [@project, @wiki], notice: 'Wiki was successfully updated.' } | |
| 45 | 28 | else |
| 46 | 29 | format.html { render action: "edit" } |
| 47 | - format.json { render json: @wiki.errors, status: :unprocessable_entity } | |
| 48 | 30 | end |
| 49 | 31 | end |
| 50 | 32 | end |
| 51 | - | |
| 33 | + | |
| 52 | 34 | def destroy |
| 53 | - @wiki = Wiki.find(params[:id]) | |
| 35 | + @wiki = @project.wikis.find(params[:id]) | |
| 54 | 36 | @wiki.destroy |
| 55 | 37 | |
| 56 | 38 | respond_to do |format| |
| 57 | 39 | format.html { redirect_to wikis_url } |
| 58 | - format.json { head :no_content } | |
| 59 | 40 | end |
| 60 | 41 | end |
| 61 | 42 | end | ... | ... |
app/models/wiki.rb
| ... | ... | @@ -2,10 +2,9 @@ class Wiki < ActiveRecord::Base |
| 2 | 2 | belongs_to :project |
| 3 | 3 | |
| 4 | 4 | validates :content, :title, :presence => true |
| 5 | - validates :title, :length => 1..250, | |
| 6 | - :uniqueness => {:scope => :project_id, :case_sensitive => false} | |
| 5 | + validates :title, :length => 1..250 | |
| 7 | 6 | |
| 8 | - before_save :set_slug | |
| 7 | + before_update :set_slug | |
| 9 | 8 | |
| 10 | 9 | |
| 11 | 10 | def to_param |
| ... | ... | @@ -17,4 +16,17 @@ class Wiki < ActiveRecord::Base |
| 17 | 16 | def set_slug |
| 18 | 17 | self.slug = self.title.parameterize |
| 19 | 18 | end |
| 19 | + | |
| 20 | + class << self | |
| 21 | + def regenerate_from wiki | |
| 22 | + regenerated_field = [:slug, :content, :title] | |
| 23 | + | |
| 24 | + new_wiki = Wiki.new | |
| 25 | + regenerated_field.each do |field| | |
| 26 | + new_wiki.send("#{field}=", wiki.send(field)) | |
| 27 | + end | |
| 28 | + new_wiki | |
| 29 | + end | |
| 30 | + | |
| 31 | + end | |
| 20 | 32 | end | ... | ... |
app/views/layouts/_project_menu.html.haml
| ... | ... | @@ -21,5 +21,5 @@ |
| 21 | 21 | Wall |
| 22 | 22 | |
| 23 | 23 | - if @project.wiki_enabled |
| 24 | - -#= link_to project_wikis_path(@project), :class => current_page?(:controller => "projects", :action => "wiki", :id => @project) ? "current" : nil do | |
| 24 | + = link_to project_wiki_path(@project, :index), :class => current_page?(:controller => "projects", :action => "wiki", :id => @project) ? "current" : nil do | |
| 25 | 25 | Wiki | ... | ... |
app/views/wikis/_form.html.haml
| 1 | -= form_for @wiki do |f| | |
| 1 | += form_for [@project, @wiki] do |f| | |
| 2 | 2 | -if @wiki.errors.any? |
| 3 | 3 | #error_explanation |
| 4 | 4 | %h2= "#{pluralize(@wiki.errors.count, "error")} prohibited this wiki from being saved:" |
| ... | ... | @@ -9,6 +9,7 @@ |
| 9 | 9 | .field |
| 10 | 10 | = f.label :title |
| 11 | 11 | = f.text_field :title |
| 12 | + = f.hidden_field :slug | |
| 12 | 13 | .field |
| 13 | 14 | = f.label :content |
| 14 | 15 | = f.text_area :content | ... | ... |
app/views/wikis/edit.html.haml
app/views/wikis/index.html.haml
| ... | ... | @@ -1,21 +0,0 @@ |
| 1 | -%h1 Listing wikis | |
| 2 | - | |
| 3 | -%table | |
| 4 | - %tr | |
| 5 | - %th Title | |
| 6 | - %th Content | |
| 7 | - %th | |
| 8 | - %th | |
| 9 | - %th | |
| 10 | - | |
| 11 | - - @wikis.each do |wiki| | |
| 12 | - %tr | |
| 13 | - %td= wiki.title | |
| 14 | - %td= wiki.content | |
| 15 | - %td= link_to 'Show', wiki | |
| 16 | - %td= link_to 'Edit', edit_wiki_path(wiki) | |
| 17 | - %td= link_to 'Destroy', wiki, :confirm => 'Are you sure?', :method => :delete | |
| 18 | - | |
| 19 | -%br | |
| 20 | - | |
| 21 | -= link_to 'New Wiki', new_wiki_path |
app/views/wikis/new.html.haml
config/routes.rb