https://svn.lrde.epita.fr/svn/ranch/trunk
Index: ChangeLog from Nicolas Despr�s nicolas.despres@gmail.com
Add the diff view to the system controller.
* web/ranch/test/functional/systems_controller_test.rb: Test it. * web/ranch/app/helpers/systems_helper.rb: Add the diff_tag which help to render the differences table. * web/ranch/app/models/system.rb: Store the field ignored by the diff algorithm in a class variable. * web/ranch/app/controllers/systems_controller.rb: Add the diff request. * web/ranch/app/views/systems/_menubar.rhtml: Add the diff item menu. * web/ranch/app/views/systems/diff.rhtml: New. The view for the diff request.
app/controllers/systems_controller.rb | 21 ++++++++++--- app/helpers/systems_helper.rb | 45 +++++++++++++++++++++++++++++ app/models/system.rb | 7 +++- app/views/systems/_menubar.rhtml | 13 ++++---- app/views/systems/diff.rhtml | 15 +++++++++ test/functional/systems_controller_test.rb | 18 +++++++++++ 6 files changed, 107 insertions(+), 12 deletions(-)
Index: web/ranch/test/functional/systems_controller_test.rb --- web/ranch/test/functional/systems_controller_test.rb (revision 66) +++ web/ranch/test/functional/systems_controller_test.rb (working copy) @@ -85,4 +85,22 @@ assert_nil assigns(:system) end
+ def test_diff_with_hostname + get :diff, { :system_id => 1 } + assert_response :success + assert_template "diff" + + assert_not_nil assigns(:system) + assert_not_nil assigns(:systems) + end + + def test_diff_without_hostname + get :diff + assert_response :success + assert_template "diff" + + assert_nil assigns(:system) + assert_nil assigns(:systems) + end + end Index: web/ranch/app/helpers/systems_helper.rb --- web/ranch/app/helpers/systems_helper.rb (revision 66) +++ web/ranch/app/helpers/systems_helper.rb (working copy) @@ -1,2 +1,47 @@ module SystemsHelper + + def diff_tag(systems) + ret = "<table>" + system = systems.shift + ret += %( + <tr><td> + #{system.created_at} + </td><td> + <table> + ) + for column in System.columns do + unless System.diff_exception.include?(column.name) + ret += %( + <tr> + #{content_tag "td", "#{column.name}:"} + #{content_tag "td", system.send(column.name)} + </tr> + ) + end + end + ret += "</table></td></tr>" + + prev = system + for system in systems do + diff = prev.diff(system) + ret += %( + <tr><td> + #{system.created_at} + </td><td> + <table> + ) + for field in diff do + ret += %( + <tr> + #{content_tag "td", "#{field}:"} + #{content_tag "td", system.send(field)} + </tr> + ) + end + ret += "</table></td></tr>" + prev = system + end + ret += "</table>" + end + end Index: web/ranch/app/models/system.rb --- web/ranch/app/models/system.rb (revision 66) +++ web/ranch/app/models/system.rb (working copy) @@ -31,11 +31,14 @@ systems end
+ @@diff_exception = %w( info id created_at ) + + cattr_reader :diff_exception + def diff(other) - except = %w( info id created_at ) ret = [] attributes.each do |k, v| - if not except.include? k and v != other.attributes[k] + if not System.diff_exception.include? k and v != other.attributes[k] ret << k end end Index: web/ranch/app/controllers/systems_controller.rb --- web/ranch/app/controllers/systems_controller.rb (revision 66) +++ web/ranch/app/controllers/systems_controller.rb (working copy) @@ -2,6 +2,8 @@
layout 'systems'
+ before_filter :current_system + def index redirect_to :action => 'list' end @@ -14,17 +16,28 @@
@ascendant = params[:ascendant] != "false"
- @systems = System.find :all, { - :order => "#@order_by #{@ascendant ? "" : "DESC"}" - } + @systems = System.find(:all, + :order => "#@order_by #{@ascendant ? "" : "DESC"}") end
def show - @system = System.find_by_id params[:system_id] + end + + def diff + unless @system.nil? + @systems = System.find(:all, + :conditions => [ "hostname = ?", + @system.hostname ], + :order => "created_at") + end end
protected
+ def current_system + @system = System.find_by_id params[:system_id] + end + def field_name_exists?(name) System.columns.each do |column| return true if column.name == name Index: web/ranch/app/views/systems/_menubar.rhtml --- web/ranch/app/views/systems/_menubar.rhtml (revision 66) +++ web/ranch/app/views/systems/_menubar.rhtml (working copy) @@ -2,15 +2,16 @@ <table> <tr> <td> - <%= link_to "List", :action => "list" %> + <%= link_to "List", :action => "list", + :system_id => @system.nil? ? nil : @system.id %> </td> <td> - <% if @system.nil? %> - <%= link_to "Information", :action => "show" %> - <% else %> <%= link_to "Information", :action => "show", - :system_id => @system.id %> - <% end %> + :system_id => @system.nil? ? nil : @system.id %> + </td> + <td> + <%= link_to "Differences", :action => "diff", + :system_id => @system.nil? ? nil : @system.id %> </td> </tr> </table> Index: web/ranch/app/views/systems/diff.rhtml --- web/ranch/app/views/systems/diff.rhtml (revision 0) +++ web/ranch/app/views/systems/diff.rhtml (revision 0) @@ -0,0 +1,15 @@ + <!-- -*- html -*- --> + +<p> +<% if @systems.nil? or @systems.empty? %> + Select a system hostname in the list first. +<% else %> + + <p> + <%= content_tag "h1", @systems.first.hostname %> + </p> + + <%= diff_tag @systems %> + +<% end %> +</p>