diff app/controllers/.svn/text-base/wiki_controller.rb.svn-base @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents 513646585e45
children af80e5618e9b
line wrap: on
line diff
--- a/app/controllers/.svn/text-base/wiki_controller.rb.svn-base	Fri Sep 24 14:06:04 2010 +0100
+++ b/app/controllers/.svn/text-base/wiki_controller.rb.svn-base	Fri Nov 19 13:24:41 2010 +0000
@@ -17,20 +17,39 @@
 
 require 'diff'
 
+# The WikiController follows the Rails REST controller pattern but with
+# a few differences
+#
+# * index - shows a list of WikiPages grouped by page or date
+# * new - not used
+# * create - not used
+# * show - will also show the form for creating a new wiki page
+# * edit - used to edit an existing or new page
+# * update - used to save a wiki page update to the database, including new pages
+# * destroy - normal
+#
+# Other member and collection methods are also used
+#
+# TODO: still being worked on
 class WikiController < ApplicationController
   default_search_scope :wiki_pages
   before_filter :find_wiki, :authorize
   before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
   
-  verify :method => :post, :only => [:destroy, :protect], :redirect_to => { :action => :index }
+  verify :method => :post, :only => [:protect], :redirect_to => { :action => :show }
 
   helper :attachments
   include AttachmentsHelper   
   helper :watchers
-  
+
+  # List of pages, sorted alphabetically and by parent (hierarchy)
+  def index
+    load_pages_grouped_by_date_without_content
+  end
+
   # display a page (in editing mode if it doesn't exist)
-  def index
-    page_title = params[:page]
+  def show
+    page_title = params[:id]
     @page = @wiki.find_or_new_page(page_title)
     if @page.new_record?
       if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
@@ -63,7 +82,7 @@
   
   # edit an existing page or a new one
   def edit
-    @page = @wiki.find_or_new_page(params[:page])    
+    @page = @wiki.find_or_new_page(params[:id])    
     return render_403 unless editable?
     @page.content = WikiContent.new(:page => @page) if @page.new_record?
     
@@ -71,34 +90,48 @@
     @content.text = initial_page_content(@page) if @content.text.blank?
     # don't keep previous comment
     @content.comments = nil
-    if request.get?
-      # To prevent StaleObjectError exception when reverting to a previous version
-      @content.version = @page.content.version
-    else
-      if !@page.new_record? && @content.text == params[:content][:text]
-        attachments = Attachment.attach_files(@page, params[:attachments])
-        render_attachment_warning_if_needed(@page)
-        # don't save if text wasn't changed
-        redirect_to :action => 'index', :id => @project, :page => @page.title
-        return
-      end
-      #@content.text = params[:content][:text]
-      #@content.comments = params[:content][:comments]
-      @content.attributes = params[:content]
-      @content.author = User.current
-      # if page is new @page.save will also save content, but not if page isn't a new record
-      if (@page.new_record? ? @page.save : @content.save)
-        attachments = Attachment.attach_files(@page, params[:attachments])
-        render_attachment_warning_if_needed(@page)
-        call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
-        redirect_to :action => 'index', :id => @project, :page => @page.title
-      end
-    end
+
+    # To prevent StaleObjectError exception when reverting to a previous version
+    @content.version = @page.content.version
   rescue ActiveRecord::StaleObjectError
     # Optimistic locking exception
     flash[:error] = l(:notice_locking_conflict)
   end
-  
+
+  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
+  # Creates a new page or updates an existing one
+  def update
+    @page = @wiki.find_or_new_page(params[:id])    
+    return render_403 unless editable?
+    @page.content = WikiContent.new(:page => @page) if @page.new_record?
+    
+    @content = @page.content_for_version(params[:version])
+    @content.text = initial_page_content(@page) if @content.text.blank?
+    # don't keep previous comment
+    @content.comments = nil
+
+    if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
+      attachments = Attachment.attach_files(@page, params[:attachments])
+      render_attachment_warning_if_needed(@page)
+      # don't save if text wasn't changed
+      redirect_to :action => 'show', :project_id => @project, :id => @page.title
+      return
+    end
+    @content.attributes = params[:content]
+    @content.author = User.current
+    # if page is new @page.save will also save content, but not if page isn't a new record
+    if (@page.new_record? ? @page.save : @content.save)
+      attachments = Attachment.attach_files(@page, params[:attachments])
+      render_attachment_warning_if_needed(@page)
+      call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
+      redirect_to :action => 'show', :project_id => @project, :id => @page.title
+    end
+
+  rescue ActiveRecord::StaleObjectError
+    # Optimistic locking exception
+    flash[:error] = l(:notice_locking_conflict)
+  end
+
   # rename a page
   def rename
     return render_403 unless editable?
@@ -107,13 +140,13 @@
     @original_title = @page.pretty_title
     if request.post? && @page.update_attributes(params[:wiki_page])
       flash[:notice] = l(:notice_successful_update)
-      redirect_to :action => 'index', :id => @project, :page => @page.title
+      redirect_to :action => 'show', :project_id => @project, :id => @page.title
     end
   end
   
   def protect
     @page.update_attribute :protected, params[:protected]
-    redirect_to :action => 'index', :id => @project, :page => @page.title
+    redirect_to :action => 'show', :project_id => @project, :id => @page.title
   end
 
   # show page history
@@ -139,7 +172,8 @@
     @annotate = @page.annotate(params[:version])
     render_404 unless @annotate
   end
-  
+
+  verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
   # Removes a wiki page and its history
   # Children can be either set as root pages, removed or reassigned to another parent page
   def destroy
@@ -166,41 +200,26 @@
       end
     end
     @page.destroy
-    redirect_to :action => 'special', :id => @project, :page => 'Page_index'
+    redirect_to :action => 'index', :project_id => @project
   end
 
-  # display special pages
-  def special
-    page_title = params[:page].downcase
-    case page_title
-    # show pages index, sorted by title
-    when 'page_index', 'date_index'
-      # eager load information about last updates, without loading text
-      @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
-                                      :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
-                                      :order => 'title'
-      @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
-      @pages_by_parent_id = @pages.group_by(&:parent_id)
-    # export wiki to a single html file
-    when 'export'
-      if User.current.allowed_to?(:export_wiki_pages, @project)
-        @pages = @wiki.pages.find :all, :order => 'title'
-        export = render_to_string :action => 'export_multiple', :layout => false
-        send_data(export, :type => 'text/html', :filename => "wiki.html")
-      else
-        redirect_to :action => 'index', :id => @project, :page => nil
-      end
-      return      
+  # Export wiki to a single html file
+  def export
+    if User.current.allowed_to?(:export_wiki_pages, @project)
+      @pages = @wiki.pages.find :all, :order => 'title'
+      export = render_to_string :action => 'export_multiple', :layout => false
+      send_data(export, :type => 'text/html', :filename => "wiki.html")
     else
-      # requested special page doesn't exist, redirect to default page
-      redirect_to :action => 'index', :id => @project, :page => nil
-      return
+      redirect_to :action => 'show', :project_id => @project, :id => nil
     end
-    render :action => "special_#{page_title}"
+  end
+
+  def date_index
+    load_pages_grouped_by_date_without_content
   end
   
   def preview
-    page = @wiki.find_page(params[:page])
+    page = @wiki.find_page(params[:id])
     # page is nil when previewing a new page
     return render_403 unless page.nil? || editable?(page)
     if page
@@ -215,13 +234,13 @@
     return render_403 unless editable?
     attachments = Attachment.attach_files(@page, params[:attachments])
     render_attachment_warning_if_needed(@page)
-    redirect_to :action => 'index', :page => @page.title
+    redirect_to :action => 'show', :id => @page.title, :project_id => @project
   end
 
 private
   
   def find_wiki
-    @project = Project.find(params[:id])
+    @project = Project.find(params[:project_id])
     @wiki = @project.wiki
     render_404 unless @wiki
   rescue ActiveRecord::RecordNotFound
@@ -230,7 +249,7 @@
   
   # Finds the requested page and returns a 404 error if it doesn't exist
   def find_existing_page
-    @page = @wiki.find_page(params[:page])
+    @page = @wiki.find_page(params[:id])
     render_404 if @page.nil?
   end
   
@@ -245,4 +264,14 @@
     extend helper unless self.instance_of?(helper)
     helper.instance_method(:initial_page_content).bind(self).call(page)
   end
+
+  # eager load information about last updates, without loading text
+  def load_pages_grouped_by_date_without_content
+    @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
+                                    :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
+                                    :order => 'title'
+    @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
+    @pages_by_parent_id = @pages.group_by(&:parent_id)
+  end
+  
 end