diff options
| author | hukl <contact@smyck.org> | 2009-10-07 23:36:03 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-10-07 23:36:03 +0200 |
| commit | 262f4e0c2abfefbd6965358fdfd9a8c332a38d57 (patch) | |
| tree | 91055edbbbec337ffd8745239b9d3ccdd8aebb9a | |
| parent | d3271f55b56e0fb5f73cf5691f90be9370d85d80 (diff) | |
refactored revsions controller to act as a nested resource of nodes. boy that cleaned up some stuff quite a bit. also having tests for that is just feeling great
| -rw-r--r-- | app/controllers/revisions_controller.rb | 21 | ||||
| -rw-r--r-- | app/views/nodes/edit.html.erb | 2 | ||||
| -rw-r--r-- | app/views/nodes/index.html.erb | 2 | ||||
| -rw-r--r-- | app/views/nodes/show.html.erb | 2 | ||||
| -rw-r--r-- | app/views/revisions/diff.html.erb | 6 | ||||
| -rw-r--r-- | app/views/revisions/index.html.erb | 45 | ||||
| -rw-r--r-- | app/views/revisions/show.html.erb | 88 | ||||
| -rw-r--r-- | config/routes.rb | 5 | ||||
| -rw-r--r-- | test/functional/nodes_controller_test.rb | 2 | ||||
| -rw-r--r-- | test/functional/revisions_controller_test.rb | 59 |
10 files changed, 164 insertions, 68 deletions
diff --git a/app/controllers/revisions_controller.rb b/app/controllers/revisions_controller.rb index 32110ae..05e8acc 100644 --- a/app/controllers/revisions_controller.rb +++ b/app/controllers/revisions_controller.rb | |||
| @@ -7,12 +7,12 @@ class RevisionsController < ApplicationController | |||
| 7 | layout 'admin' | 7 | layout 'admin' |
| 8 | 8 | ||
| 9 | def index | 9 | def index |
| 10 | @node = Node.find(params[:node_id]) | ||
| 10 | end | 11 | end |
| 11 | 12 | ||
| 12 | def diff | 13 | def diff |
| 13 | @node = Node.find(params[:id]) | 14 | @node = Node.find(params[:node_id]) |
| 14 | 15 | ||
| 15 | puts @node.pages.length | ||
| 16 | if @node.pages.length > 1 | 16 | if @node.pages.length > 1 |
| 17 | params[:start_revision] ||= @node.pages.all[-2].revision | 17 | params[:start_revision] ||= @node.pages.all[-2].revision |
| 18 | params[:end_revision] ||= @node.pages.all[-1].revision | 18 | params[:end_revision] ||= @node.pages.all[-1].revision |
| @@ -20,26 +20,19 @@ class RevisionsController < ApplicationController | |||
| 20 | params[:start], params[:end] = 1, 1 | 20 | params[:start], params[:end] = 1, 1 |
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | @start = Page.find( :first, :conditions => { | 23 | @start = @node.pages.find_by_revision( params[:start_revision] ) |
| 24 | :node_id => params[:id], | 24 | @end = @node.pages.find_by_revision( params[:end_revision] ) |
| 25 | :revision => params[:start_revision] | ||
| 26 | }) | ||
| 27 | |||
| 28 | @end = Page.find( :first, :conditions => { | ||
| 29 | :node_id => params[:id], | ||
| 30 | :revision => params[:end_revision] | ||
| 31 | }) | ||
| 32 | |||
| 33 | end | 25 | end |
| 34 | 26 | ||
| 35 | def show | 27 | def show |
| 36 | @node = Node.find(params[:id]) | 28 | @node = Node.find(params[:node_id]) |
| 29 | @page = @node.pages.find(params[:id]) | ||
| 37 | end | 30 | end |
| 38 | 31 | ||
| 39 | def restore | 32 | def restore |
| 40 | page = Page.find(params[:id]) | 33 | page = Page.find(params[:id]) |
| 41 | page.node.restore_revision! page.revision | 34 | page.node.restore_revision! page.revision |
| 42 | flash[:notice] = "Revision #{page.revision} restored" | 35 | flash[:notice] = "Revision #{page.revision} restored" |
| 43 | redirect_to :back | 36 | redirect_to node_path(page.node) |
| 44 | end | 37 | end |
| 45 | end | 38 | end |
diff --git a/app/views/nodes/edit.html.erb b/app/views/nodes/edit.html.erb index 0635dac..0063dd9 100644 --- a/app/views/nodes/edit.html.erb +++ b/app/views/nodes/edit.html.erb | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | <%= link_to 'Show', @node %> | 3 | <%= link_to 'Show', @node %> |
| 4 | <%= link_to 'Preview', preview_page_path(@draft) %> | 4 | <%= link_to 'Preview', preview_page_path(@draft) %> |
| 5 | <%= link_to 'Publish', publish_node_path, :method => :put, :confirm => "Publish this draft?" %> | 5 | <%= link_to 'Publish', publish_node_path, :method => :put, :confirm => "Publish this draft?" %> |
| 6 | <%= link_to 'Revisions', revision_path(params[:id]) %> | 6 | <%= link_to 'Revisions', node_revisions_path(@node) %> |
| 7 | <% end %> | 7 | <% end %> |
| 8 | 8 | ||
| 9 | <div id="page_editor"> | 9 | <div id="page_editor"> |
diff --git a/app/views/nodes/index.html.erb b/app/views/nodes/index.html.erb index 0603c08..d78a997 100644 --- a/app/views/nodes/index.html.erb +++ b/app/views/nodes/index.html.erb | |||
| @@ -21,7 +21,7 @@ | |||
| 21 | </td> | 21 | </td> |
| 22 | <td class="actions"> | 22 | <td class="actions"> |
| 23 | <%= link_to 'show', node_path(node) %> | 23 | <%= link_to 'show', node_path(node) %> |
| 24 | <%= link_to 'Revisions', :controller => :revisions, :action => :show, :id => node.id %> | 24 | <%= link_to 'Revisions', node_revisions_path(node) %> |
| 25 | <%# link_to 'Destroy', node, :method => :delete, :confirm => "Are you sure you want to delete this node?" %> | 25 | <%# link_to 'Destroy', node, :method => :delete, :confirm => "Are you sure you want to delete this node?" %> |
| 26 | <%= link_to 'Unlock', unlock_node_path(node), :method => :put, :confirm => "Are you sure you want to unlock?" %> | 26 | <%= link_to 'Unlock', unlock_node_path(node), :method => :put, :confirm => "Are you sure you want to unlock?" %> |
| 27 | </td> | 27 | </td> |
diff --git a/app/views/nodes/show.html.erb b/app/views/nodes/show.html.erb index 8d8a985..8f56c7c 100644 --- a/app/views/nodes/show.html.erb +++ b/app/views/nodes/show.html.erb | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | <% content_for :subnavigation do %> | 1 | <% content_for :subnavigation do %> |
| 2 | <%= link_to 'Edit', edit_node_path(@node), :class => "unselected" %> | 2 | <%= link_to 'Edit', edit_node_path(@node), :class => "unselected" %> |
| 3 | <%= link_to 'Preview', preview_page_path(@page) %> | 3 | <%= link_to 'Preview', preview_page_path(@page) %> |
| 4 | <%= link_to 'Revisions', revision_path(params[:id]) %> | 4 | <%= link_to 'Revisions', node_revisions_path(@node) %> |
| 5 | <%= link_to 'Unlock', unlock_node_path(@node), :method => :put, :confirm => "Are you sure you want to unlock?" %> | 5 | <%= link_to 'Unlock', unlock_node_path(@node), :method => :put, :confirm => "Are you sure you want to unlock?" %> |
| 6 | <% end %> | 6 | <% end %> |
| 7 | 7 | ||
diff --git a/app/views/revisions/diff.html.erb b/app/views/revisions/diff.html.erb index 9428b7a..f7ad79c 100644 --- a/app/views/revisions/diff.html.erb +++ b/app/views/revisions/diff.html.erb | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | <% content_for :subnavigation do %> | 1 | <% content_for :subnavigation do %> |
| 2 | <%= link_to 'Edit', edit_node_path(@node) %> | 2 | <%= link_to 'Edit', edit_node_path(@node) %> |
| 3 | <%= link_to 'Revisions', revision_path(params[:id]) %> | 3 | <%= link_to 'Revisions', node_revisions_path(@node) %> |
| 4 | <% end %> | 4 | <% end %> |
| 5 | 5 | ||
| 6 | <h1>Revisions#diff</h1> | 6 | <h1>Revisions#diff</h1> |
| 7 | 7 | ||
| 8 | <% form_tag diff_revision_path do %> | 8 | <% form_tag diff_node_revisions_path do %> |
| 9 | <%= select_tag :start_revision, options_for_select(@node.pages.map{|x| x.revision}, params[:start_revision].to_i) %> | 9 | <%= select_tag :start_revision, options_for_select(@node.pages.map{|x| x.revision}, params[:start_revision].to_i) %> |
| 10 | <%= select_tag :end_revision, options_for_select(@node.pages.map{|x| x.revision}, params[:end_revision].to_i) %> | 10 | <%= select_tag :end_revision, options_for_select(@node.pages.map{|x| x.revision}, params[:end_revision].to_i) %> |
| 11 | <%= submit_tag 'Diff' %> | 11 | <%= submit_tag 'Diff' %> |
diff --git a/app/views/revisions/index.html.erb b/app/views/revisions/index.html.erb index e3134d7..dbb612b 100644 --- a/app/views/revisions/index.html.erb +++ b/app/views/revisions/index.html.erb | |||
| @@ -1 +1,44 @@ | |||
| 1 | <h1>Revisions#index</h1> | 1 | <% content_for :subnavigation do %> |
| 2 | <%= link_to 'Edit', edit_node_path(@node) %> | ||
| 3 | <% end %> | ||
| 4 | |||
| 5 | <h2>Revisions for Node: <%= @node.unique_name %></h2> | ||
| 6 | |||
| 7 | <% form_tag diff_node_revisions_path(@node) do %> | ||
| 8 | <table id="revisions"> | ||
| 9 | <tr class="header"> | ||
| 10 | <th>First</th> | ||
| 11 | <th>Last</th> | ||
| 12 | <th>Rev.</th> | ||
| 13 | <th>Title</th> | ||
| 14 | <th>User</th> | ||
| 15 | <th>Date</th> | ||
| 16 | <th></th> | ||
| 17 | </tr> | ||
| 18 | <% @node.pages.reverse.each do |page| %> | ||
| 19 | <tr> | ||
| 20 | <td><%= radio_button_tag :start_revision, page.revision %></td> | ||
| 21 | <td><%= radio_button_tag :end_revision, page.revision %></td> | ||
| 22 | <td class="revision"><%= page.revision %></td> | ||
| 23 | <td class="title"><%= page.title %></td> | ||
| 24 | <td class="user"><%= page.user.try(:login) %></td> | ||
| 25 | <td class="date"><%= page.updated_at %></td> | ||
| 26 | <td> | ||
| 27 | <%= link_to 'show', node_revision_path(@node, page) %> | ||
| 28 | </td> | ||
| 29 | <td> | ||
| 30 | <%= link_to( | ||
| 31 | 'restore', | ||
| 32 | restore_node_revision_path(@node, page), | ||
| 33 | :method => :put, | ||
| 34 | :confirm => "Restore this revision?" | ||
| 35 | ) %> | ||
| 36 | </td> | ||
| 37 | </tr> | ||
| 38 | <% end %> | ||
| 39 | <tr class="no_hover"> | ||
| 40 | <td colspan="8" class="right"><%= submit_tag 'Diff revisions' %></td> | ||
| 41 | </tr> | ||
| 42 | </table> | ||
| 43 | |||
| 44 | <% end %> \ No newline at end of file | ||
diff --git a/app/views/revisions/show.html.erb b/app/views/revisions/show.html.erb index ebda18d..154d31b 100644 --- a/app/views/revisions/show.html.erb +++ b/app/views/revisions/show.html.erb | |||
| @@ -1,44 +1,50 @@ | |||
| 1 | <% content_for :subnavigation do %> | 1 | <% content_for :subnavigation do %> |
| 2 | <%= link_to 'Edit', edit_node_path(@node) %> | 2 | <%= link_to 'show all revisions', node_revisions_path(@node) %> |
| 3 | <% end %> | 3 | <% end %> |
| 4 | 4 | ||
| 5 | <h2>Revisions for Node: <%= @node.unique_name %></h2> | 5 | <div id="page_editor" class="show_node"> |
| 6 | 6 | <table id="content"> | |
| 7 | <% form_tag diff_revision_path do %> | 7 | <tr> |
| 8 | <table id="revisions"> | 8 | <td class="description">Path</td> |
| 9 | <tr class="header"> | 9 | <td><%= @page.public_link %></td> |
| 10 | <th>First</th> | 10 | </tr> |
| 11 | <th>Last</th> | 11 | <tr> |
| 12 | <th>Rev.</th> | 12 | <td class="description">Author</td> |
| 13 | <th>Title</th> | 13 | <td><%= @page.user.try(:login) %></td> |
| 14 | <th>User</th> | 14 | </tr> |
| 15 | <th>Date</th> | 15 | <% if @page.node.locked? %> |
| 16 | <th></th> | 16 | <tr> |
| 17 | </tr> | 17 | <td class="description">Locked by</td> |
| 18 | <% @node.pages.reverse.each do |page| %> | 18 | <td><%= @page.node.lock_owner.login %></td> |
| 19 | <tr> | 19 | </tr> |
| 20 | <td><%= radio_button_tag :start_revision, page.revision %></td> | 20 | <% end %> |
| 21 | <td><%= radio_button_tag :end_revision, page.revision %></td> | 21 | <tr> |
| 22 | <td class="revision"><%= page.revision %></td> | 22 | <td class="description">Last updated</td> |
| 23 | <td class="title"><%= page.title %></td> | 23 | <td><%= @page.updated_at %></td> |
| 24 | <td class="user"><%= page.user.try(:login) %></td> | 24 | </tr> |
| 25 | <td class="date"><%= page.updated_at %></td> | 25 | <tr> |
| 26 | <td> | 26 | <td class="description">Published at</td> |
| 27 | <%= link_to 'show', node_path(page.node) %> | 27 | <td><%= @page.published_at %></td> |
| 28 | </td> | 28 | </tr> |
| 29 | <td> | 29 | <tr> |
| 30 | <%= link_to( | 30 | <td class="description">Revision</td> |
| 31 | 'restore', | 31 | <td><%= @page.revision %></td> |
| 32 | restore_revision_path(page), | 32 | </tr> |
| 33 | :method => :put, | 33 | <tr> |
| 34 | :confirm => "Restore this revision?" | 34 | <td class="description"><strong>Title</strong></td> |
| 35 | ) %> | 35 | <td><%= @page.title %></td> |
| 36 | </td> | 36 | </tr> |
| 37 | </tr> | 37 | <tr> |
| 38 | <% end %> | 38 | <td class="description"><strong>Abstract</strong></td> |
| 39 | <tr class="no_hover"> | 39 | <td><%= @page.abstract %></td> |
| 40 | <td colspan="8" class="right"><%= submit_tag 'Diff revisions' %></td> | 40 | </tr> |
| 41 | </tr> | 41 | <tr> |
| 42 | </table> | 42 | <td class="description"><strong>Body</strong></td> |
| 43 | 43 | <td><%= @page.body %></td> | |
| 44 | <% end %> \ No newline at end of file | 44 | </tr> |
| 45 | <tr> | ||
| 46 | <td></td> | ||
| 47 | <td class="right"></td> | ||
| 48 | </tr> | ||
| 49 | </table> | ||
| 50 | </div> \ No newline at end of file | ||
diff --git a/config/routes.rb b/config/routes.rb index d591619..9b5980b 100644 --- a/config/routes.rb +++ b/config/routes.rb | |||
| @@ -12,9 +12,10 @@ ActionController::Routing::Routes.draw do |map| | |||
| 12 | map.resources :tags | 12 | map.resources :tags |
| 13 | map.resources :occurrences | 13 | map.resources :occurrences |
| 14 | map.resources :events | 14 | map.resources :events |
| 15 | map.resources :revisions, :member => {:diff => :post, :restore => :put} | ||
| 16 | map.resources :pages, :member => {:preview => :get, :sort_images => :put} | 15 | map.resources :pages, :member => {:preview => :get, :sort_images => :put} |
| 17 | map.resources :nodes, :member => {:publish => :put, :unlock => :put} | 16 | map.resources :nodes, :member => {:publish => :put, :unlock => :put} do |node| |
| 17 | node.resources :revisions, :member => {:restore => :put}, :collection => {:diff => :post} | ||
| 18 | end | ||
| 18 | map.logout '/logout', :controller => 'sessions', :action => 'destroy' | 19 | map.logout '/logout', :controller => 'sessions', :action => 'destroy' |
| 19 | map.login '/login', :controller => 'sessions', :action => 'new' | 20 | map.login '/login', :controller => 'sessions', :action => 'new' |
| 20 | map.admin_search 'admin/search', :controller => 'admin', :action => 'search' | 21 | map.admin_search 'admin/search', :controller => 'admin', :action => 'search' |
diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb index 3dae9db..eca7d93 100644 --- a/test/functional/nodes_controller_test.rb +++ b/test/functional/nodes_controller_test.rb | |||
| @@ -84,7 +84,7 @@ class NodesControllerTest < ActionController::TestCase | |||
| 84 | end | 84 | end |
| 85 | end | 85 | end |
| 86 | 86 | ||
| 87 | def test_editing_a_node | 87 | test "editing a node" do |
| 88 | login_as :quentin | 88 | login_as :quentin |
| 89 | 89 | ||
| 90 | node = Node.find_by_unique_name("fourth_child") | 90 | node = Node.find_by_unique_name("fourth_child") |
diff --git a/test/functional/revisions_controller_test.rb b/test/functional/revisions_controller_test.rb index d752b0e..43001df 100644 --- a/test/functional/revisions_controller_test.rb +++ b/test/functional/revisions_controller_test.rb | |||
| @@ -1,8 +1,61 @@ | |||
| 1 | require 'test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | class RevisionsControllerTest < ActionController::TestCase | 3 | class RevisionsControllerTest < ActionController::TestCase |
| 4 | # Replace this with your real tests. | 4 | |
| 5 | test "the truth" do | 5 | def setup |
| 6 | assert true | 6 | Node.root.descendants.destroy_all |
| 7 | @user = User.find_by_login("aaron") | ||
| 8 | @node = Node.root.children.create!( :slug => "version_me" ) | ||
| 9 | |||
| 10 | draft = @node.draft | ||
| 11 | draft.body = "first" | ||
| 12 | @node.publish_draft! | ||
| 13 | @node.find_or_create_draft @user | ||
| 14 | draft = @node.draft | ||
| 15 | draft.update_attributes(:body => "second") | ||
| 16 | @node.publish_draft! | ||
| 17 | end | ||
| 18 | |||
| 19 | test "setup" do | ||
| 20 | assert_equal 2, Node.count | ||
| 21 | assert_equal 2, @node.pages.count | ||
| 22 | assert_equal ["first", "second"], @node.pages.map {|p| p.body} | ||
| 23 | end | ||
| 24 | |||
| 25 | test "get list of revisions for a given node" do | ||
| 26 | login_as :quentin | ||
| 27 | get :index, :node_id => @node.id | ||
| 28 | assert_response :success | ||
| 29 | assert_select ".revision", 2 | ||
| 30 | end | ||
| 31 | |||
| 32 | test "showing one revision" do | ||
| 33 | login_as :quentin | ||
| 34 | get :show, :node_id => @node.id, :id => @node.pages.last.id | ||
| 35 | assert_response :success | ||
| 36 | assert_select "strong", "Body" | ||
| 37 | assert_select "td", {:count => 1, :text => "second"} | ||
| 38 | end | ||
| 39 | |||
| 40 | test "diffing two revisions" do | ||
| 41 | login_as :quentin | ||
| 42 | post( | ||
| 43 | :diff, | ||
| 44 | :node_id => @node.id, | ||
| 45 | :start_revision => @node.pages.first.revision, | ||
| 46 | :end_revision => @node.pages.last.revision | ||
| 47 | ) | ||
| 48 | assert_response :success | ||
| 49 | end | ||
| 50 | |||
| 51 | test "restoring a revision" do | ||
| 52 | assert_equal "second", @node.head.body | ||
| 53 | |||
| 54 | login_as :aaron | ||
| 55 | put( :restore, :node_id => @node.id, :id => @node.pages.first.id ) | ||
| 56 | |||
| 57 | @node.reload | ||
| 58 | assert_equal @node.head, @node.pages.first | ||
| 59 | assert_equal "first", @node.head.reload.body | ||
| 7 | end | 60 | end |
| 8 | end | 61 | end |
