Skip to content
Browse files

Merge branch 'with_improved_versions'

* with_improved_versions:
  Improve VersionsHelper#title_for to support Source and truncate its title.
  Renamed ChangesController to clearer VersionsController.
  Improve recent_changes.
  Improve changes#rollback_to redirection.
  Increase changes#show to display 50 changes per page, not 20.
  • Loading branch information...
2 parents d466d05 + 4e4e0f3 commit 25e72686bdec91dbff06386571c13daf0cc303ea @igal igal committed Mar 24, 2011
View
33 app/controllers/changes_controller.rb
@@ -1,33 +0,0 @@
-class ChangesController < ApplicationController
- def show
- @versions = Defer { ::Version.paginate(:page => params[:page], :order => 'created_at desc', :per_page => 20) }
- respond_to do |format|
- format.html # changes.html.erb
- format.atom # changes.atom.builder
- end
- end
-
- def rollback_to
- begin
- @version = Version.find(params[:version])
- rescue ActiveRecord::RecordNotFound
- flash[:failure] = "No such version."
- return(redirect_to(:action => :show))
- end
-
- if @version.event == "create"
- @record = @version.item_type.constantize.find(@version.item_id)
- @result = @record.destroy
- else
- @record = @version.reify
- @result = @record.save
- end
-
- if @result
- redirect_to url_for(:controller => @version.item_type.tableize, :action => "show", :id => @version.item_id)
- else
- flash[:failure] = "Couldn't rollback. Sorry."
- return(redirect_to(:action => :show))
- end
- end
-end
View
48 app/controllers/versions_controller.rb
@@ -0,0 +1,48 @@
+class VersionsController < ApplicationController
+ def index
+ @versions = Defer { ::Version.paginate(:page => params[:page], :order => 'created_at desc', :per_page => 50) }
+ respond_to do |format|
+ format.html # changes.html.erb
+ format.atom # changes.atom.builder
+ end
+ end
+
+ def show
+ begin
+ @version = Version.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ flash[:failure] = "No such version."
+ redirect_to(:action => :index)
+ end
+ end
+
+ def update
+ begin
+ @version = Version.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ flash[:failure] = "No such version."
+ return(redirect_to(:action => :index))
+ end
+
+ if @version.event == "create"
+ @record = @version.item_type.constantize.find(@version.item_id)
+ @result = @record.destroy
+ else
+ @record = @version.reify
+ @result = @record.save
+ end
+
+ if @result
+ if @version.event == "create"
+ flash[:success] = "Rolled back to destroy record."
+ redirect_to :action => :index
+ else
+ flash[:success] = "Rolled back record to earlier state."
+ redirect_to url_for(:controller => @version.item_type.tableize, :action => "show", :id => @version.item_id)
+ end
+ else
+ flash[:failure] = "Couldn't rollback. Sorry."
+ redirect_to :action => :index
+ end
+ end
+end
View
9 app/helpers/recent_changes_helper.rb → app/helpers/versions_helper.rb
@@ -1,4 +1,4 @@
-module RecentChangesHelper
+module VersionsHelper
# Return HTML representing the +object+, which is either its text or a stylized "nil".
def text_or_nil(object)
if object.nil?
@@ -72,7 +72,12 @@ def title_for(version)
previous = version.reify rescue nil
record = version.item_type.constantize.find(version.item_id) rescue nil
- title = previous.try(:title) || current.try(:title) || record.try(:title)
+ object = [previous, current, record].find { |o| o.present? }
+ method = case object
+ when Source then :url
+ else :title
+ end
+ title = truncate(object.send(method), :length => 100)
return h(title)
end
end
View
61 app/views/changes/show.html.erb
@@ -1,61 +0,0 @@
-<% cache "changes-show,page_#{params[:page] || 0}" do %>
-<h1>Changes</h1>
-
-<%= link_to "Changes feed", recent_changes_path(:format => "atom") %>
-
-<table cellspacing="0" id="changes_table">
- <tr>
- <th class="change_time">Time</th>
- <th class="change_details">Details with previous and current values</th>
- </tr>
- <% @versions.each do |version| %>
- <tr class="change_row">
- <td class="change_time">
- Version #<%= version.id %>
- <br>
- <%= version.created_at.strftime('%Y-%m-%d') %>
- <br>
- <%= version.created_at.strftime('%H:%M:%S') %>
- </td>
- <td class="change_details">
- <p class="change_detail_description">
- <a name="<%= version.id %>">
- <b><%= version.event.titleize %></b>
- <%= version.item_type %>
- <%= link_to "&laquo;#{title_for(version)}&raquo;", url_for(:controller => version.item_type.tableize, :action => "show", :id => version.item_id) %>
- &mdash;
- <%= link_to "(Rollback)", rollback_to_path(:version => version.id), :method => :post, :confirm => 'Are you sure?' %>
- <% if version.whodunnit %>
- <% if user = Member.find(version.whodunnit) rescue nil %>
- by <%= link_to h(user.name), member_path(user) %>
- <% end %>
- <% end %>
- </a>
- </p>
-
- <% changes = changes_for(version) %>
-
- <table cellspacing="0" class="changes_details_table">
- <% changes.keys.sort.each do |key| %>
- <tr class="<%= cycle('odd', 'even', :name => 'change_detail') %>">
- <td class="change_detail_key">
- <b><%= key %></b>
- </td>
- <td class="change_detail_value">
- <%= text_or_nil(changes[key][:previous]) %>
- </td>
- <td class="change_detail_spacer">&rarr;</td>
- <td class="change_detail_value">
- <%= text_or_nil(changes[key][:current]) %>
- </td>
- </tr>
- <% end %>
- <% reset_cycle('change_detail') %>
- </table>
- </td>
- </tr>
- <% end %>
-</table>
-
-<%= will_paginate @versions %>
-<% end %>
View
4 app/views/site/recent_changes.html.erb
@@ -1,5 +1,5 @@
<% content_for :discovery_insert do %>
- <%= auto_discovery_link_tag(:atom, recent_changes_url(:format => 'atom'), :title => "Atom: Recent Changes" )%>
+ <%= auto_discovery_link_tag(:atom, changes_url(:format => 'atom'), :title => "Atom: Recent Changes" )%>
<% end %>
<h2>Recent Changes</h2>
@@ -11,4 +11,4 @@
<%= render :partial => 'change', :locals => {:item => item, :object => object }%>
</li>
<% end %>
-</ul>
+</ul>
View
45 app/views/versions/_version.html.erb
@@ -0,0 +1,45 @@
+<tr class="change_row">
+ <td class="change_time">
+ Version #<%= version.id %>
+ <br>
+ <%= version.created_at.strftime('%Y-%m-%d') %>
+ <br>
+ <%= version.created_at.strftime('%H:%M:%S') %>
+ </td>
+ <td class="change_details">
+ <p class="change_detail_description">
+ <a name="<%= version.id %>">
+ <b><%= version.event.titleize %></b>
+ <%= version.item_type %>
+ <%= link_to "&laquo;#{title_for(version)}&raquo;", url_for(:controller => version.item_type.tableize, :action => "show", :id => version.item_id) %>
+ &mdash;
+ <%= link_to "(Rollback)", version, :method => :put, :confirm => 'Are you sure?' %>
+ <% if version.whodunnit %>
+ <% if user = Member.find(version.whodunnit) rescue nil %>
+ by <%= link_to h(user.name), member_path(user) %>
+ <% end %>
+ <% end %>
+ </a>
+ </p>
+
+ <% changes = changes_for(version) %>
+
+ <table cellspacing="0" class="changes_details_table">
+ <% changes.keys.sort.each do |key| %>
+ <tr class="<%= cycle('odd', 'even', :name => 'change_detail') %>">
+ <td class="change_detail_key">
+ <b><%= key %></b>
+ </td>
+ <td class="change_detail_value">
+ <%= text_or_nil(changes[key][:previous]) %>
+ </td>
+ <td class="change_detail_spacer">&rarr;</td>
+ <td class="change_detail_value">
+ <%= text_or_nil(changes[key][:current]) %>
+ </td>
+ </tr>
+ <% end %>
+ <% reset_cycle('change_detail') %>
+ </table>
+ </td>
+</tr>
View
2 app/views/changes/show.atom.builder → app/views/versions/index.atom.builder
@@ -8,7 +8,7 @@ atom_feed do |feed|
feed.updated(date)
@versions.each do |version|
- feed.entry(version, :url => recent_changes_path(:anchor => "#{version.id}")) do |entry|
+ feed.entry(version, :url => version_url(version)) do |entry|
changes = changes_for(version)
user = Member.find(version.whodunnit) rescue nil
View
15 app/views/versions/index.html.erb
@@ -0,0 +1,15 @@
+<% cache "changes-index,page_#{params[:page] || 0}" do %>
+<h1>Changes</h1>
+
+<%= link_to "Changes feed", versions_url(:format => "atom") %>
+
+<table cellspacing="0" id="changes_table">
+ <tr>
+ <th class="change_time">Time</th>
+ <th class="change_details">Details with previous and current values</th>
+ </tr>
+ <%= render :partial => 'version', :collection => @versions %>
+</table>
+
+<%= will_paginate @versions %>
+<% end %>
View
13 app/views/versions/show.html.erb
@@ -0,0 +1,13 @@
+<% cache "changes-show,#{@version.id}" do %>
+<h1>Changes</h1>
+
+<%= link_to "Changes feed", versions_url(:format => "atom") %>
+
+<table cellspacing="0" id="changes_table">
+ <tr>
+ <th class="change_time">Time</th>
+ <th class="change_details">Details with previous and current values</th>
+ </tr>
+ <%= render :partial => 'version', :object => @version %>
+</table>
+<% end %>
View
3 config/routes.rb
@@ -35,13 +35,12 @@
map.connect 'hello', :controller => 'site', :action => 'hello'
map.connect 'about', :controller => 'site', :action => 'about'
- map.recent_changes 'recent_changes.:format', :controller => 'changes', :action => 'show'
- map.rollback_to 'recent_changes/rollback_to/:version', :controller => 'changes', :action => 'rollback_to', :conditions => { :method => :post }
# Normal controllers
map.resources :events, :collection => {'duplicates' => :get, 'squash_multiple_duplicates' => :post, 'search' => :get}, :member => {'clone' => :get}
map.resources :sources, :collection => { :import => :put }
map.resources :venues, :collection => {'duplicates' => :get, 'squash_multiple_duplicates' => :post, 'map' => :get}
+ map.resources :versions, :as => 'recent_changes'
# Export action
map.connect 'export', :controller => 'site', :action => 'export'
View
29 spec/controllers/changes_controller_spec.rb → spec/controllers/versions_controller_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'
-describe ChangesController do
+describe VersionsController do
integrate_views
describe "history" do
@@ -17,9 +17,9 @@
@venue.destroy
end
- describe "show" do
+ describe "index" do
before(:each) do
- get :show
+ get :index
@versions = assigns[:versions].__value # Use #__value to extract data from DeferProxy
@create_version = @versions.find{|version| version.item_id == @venue.id && version.event == "create"}
@update_version = @versions.find{|version| version.item_id == @venue.id && version.event == "update"}
@@ -49,7 +49,10 @@
end
end
- describe "rollback_to" do
+ describe "show" do
+ end
+
+ describe "update" do
before(:each) do
@versions = Version.all
@create_version = @versions.find{|version| version.item_id == @venue.id && version.event == "create"}
@@ -61,41 +64,45 @@
Venue.should_receive(:find).and_return(@venue)
@venue.should_receive(:destroy).and_return(true)
- post :rollback_to, :version => @create_version.id
+ put :update, :id => @create_version.id
+
+ response.should redirect_to(versions_path)
end
it "should rollback an update" do
lambda { Venue.find(@venue.id) }.should raise_error(ActiveRecord::RecordNotFound)
- post :rollback_to, :version => @update_version.id
+ put :update, :id => @update_version.id
Venue.find(@venue.id).title.should == "Venue"
+ response.should redirect_to(venue_path @venue)
end
it "should rollback a destroy" do
lambda { Venue.find(@venue.id) }.should raise_error(ActiveRecord::RecordNotFound)
- post :rollback_to, :version => @destroy_version.id
+ put :update, :id => @destroy_version.id
Venue.find(@venue.id).title.should == "My Venue"
+ response.should redirect_to(venue_path @venue)
end
it "should fail on invalid version" do
- post :rollback_to, :version => "-1"
+ put :update, :id => "-1"
flash[:failure].should_not be_blank
- response.should redirect_to(recent_changes_path)
+ response.should redirect_to(versions_path)
end
it "should fail on invalid rollback" do
venue = stub_model(Venue)
Venue.stub!(:find).and_return(venue)
venue.should_receive(:save).and_return(false)
- post :rollback_to, :version => @destroy_version.id
+ put :update, :id => @destroy_version.id
flash[:failure].should_not be_blank
- response.should redirect_to(recent_changes_path)
+ response.should redirect_to(versions_path)
end
end
end
View
4 spec/helpers/recent_changes_helper_spec.rb → spec/helpers/versions_helper_spec.rb
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../spec_helper'
-include RecentChangesHelper
+include VersionsHelper
-describe RecentChangesHelper do
+describe VersionsHelper do
describe "when creating item" do
before(:each) do
@item = Venue.create!(:title => "Venue")

0 comments on commit 25e7268

Please sign in to comment.
Something went wrong with that request. Please try again.