Commit 4c1b8558df1a874716989b8217ab0acf97d6da04

Authored by Valery Sizov
1 parent eacea15a

Wiki: base implemetation logic

app/controllers/wikis_controller.rb
1 class WikisController < ApplicationController 1 class WikisController < ApplicationController
2 before_filter :project 2 before_filter :project
3 layout "project" 3 layout "project"
4 - respond_to :html  
5 4
6 def show 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 respond_to do |format| 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 end 14 end
18 end 15 end
19 16
20 def edit 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 end 20 end
23 21
24 def create 22 def create
25 - @wiki = Wiki.new(params[:wiki]) 23 + @wiki = @project.wikis.new(params[:wiki])
26 24
27 respond_to do |format| 25 respond_to do |format|
28 if @wiki.save 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 else 28 else
46 format.html { render action: "edit" } 29 format.html { render action: "edit" }
47 - format.json { render json: @wiki.errors, status: :unprocessable_entity }  
48 end 30 end
49 end 31 end
50 end 32 end
51 - 33 +
52 def destroy 34 def destroy
53 - @wiki = Wiki.find(params[:id]) 35 + @wiki = @project.wikis.find(params[:id])
54 @wiki.destroy 36 @wiki.destroy
55 37
56 respond_to do |format| 38 respond_to do |format|
57 format.html { redirect_to wikis_url } 39 format.html { redirect_to wikis_url }
58 - format.json { head :no_content }  
59 end 40 end
60 end 41 end
61 end 42 end
app/models/wiki.rb
@@ -2,10 +2,9 @@ class Wiki &lt; ActiveRecord::Base @@ -2,10 +2,9 @@ class Wiki &lt; ActiveRecord::Base
2 belongs_to :project 2 belongs_to :project
3 3
4 validates :content, :title, :presence => true 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 def to_param 10 def to_param
@@ -17,4 +16,17 @@ class Wiki &lt; ActiveRecord::Base @@ -17,4 +16,17 @@ class Wiki &lt; ActiveRecord::Base
17 def set_slug 16 def set_slug
18 self.slug = self.title.parameterize 17 self.slug = self.title.parameterize
19 end 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 end 32 end
app/views/layouts/_project_menu.html.haml
@@ -21,5 +21,5 @@ @@ -21,5 +21,5 @@
21 Wall 21 Wall
22 22
23 - if @project.wiki_enabled 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 Wiki 25 Wiki
app/views/wikis/_form.html.haml
1 -= form_for @wiki do |f| 1 += form_for [@project, @wiki] do |f|
2 -if @wiki.errors.any? 2 -if @wiki.errors.any?
3 #error_explanation 3 #error_explanation
4 %h2= "#{pluralize(@wiki.errors.count, "error")} prohibited this wiki from being saved:" 4 %h2= "#{pluralize(@wiki.errors.count, "error")} prohibited this wiki from being saved:"
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 .field 9 .field
10 = f.label :title 10 = f.label :title
11 = f.text_field :title 11 = f.text_field :title
  12 + = f.hidden_field :slug
12 .field 13 .field
13 = f.label :content 14 = f.label :content
14 = f.text_area :content 15 = f.text_area :content
app/views/wikis/edit.html.haml
1 -%h1 Editing wiki 1 +%h1 Editing page
2 2
3 = render 'form' 3 = render 'form'
4 4
5 -= link_to 'Show', @wiki  
6 -\|  
7 -= link_to 'Back', wikis_path 5 += link_to 'Show', [@project, @wiki]
app/views/wikis/index.html.haml
@@ -1,21 +0,0 @@ @@ -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
@@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
1 -%h1 New wiki  
2 -  
3 -= render 'form'  
4 -  
5 -= link_to 'Back', wikis_path  
config/routes.rb
@@ -56,7 +56,8 @@ Gitlab::Application.routes.draw do @@ -56,7 +56,8 @@ Gitlab::Application.routes.draw do
56 get "files" 56 get "files"
57 end 57 end
58 58
59 - resources :wikis, :only => [:show, :edit, :destroy] 59 + resources :wikis, :only => [:show, :edit, :destroy, :create]
  60 +
60 resource :repository do 61 resource :repository do
61 member do 62 member do
62 get "branches" 63 get "branches"