From d30ba95ab43036acb952eb8f7cfa22a8ec9ac747 Mon Sep 17 00:00:00 2001 From: Eloy Duran Date: Wed, 18 Jun 2008 17:52:56 +0200 Subject: [PATCH] Made revert work. --- English.lproj/PassengerPref.xib | 24 ++++++++++++++++++++++-- PassengerApplication.rb | 17 +++++++++++++++++ test/passenger_application_test.rb | 27 +++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/English.lproj/PassengerPref.xib b/English.lproj/PassengerPref.xib index 236789f..db9d217 100644 --- a/English.lproj/PassengerPref.xib +++ b/English.lproj/PassengerPref.xib @@ -8,7 +8,7 @@ 352.00 YES - + YES @@ -1366,6 +1366,26 @@ dHRwOi8vd3d3Lm1vZHJhaWxzLmNvbSBmb3IgaW5zdGFsbGF0aW9uIGluc3RydWN0aW9ucy4 375 + + + target: selection + + + + + + target: selection + target + selection + + NSSelectorName + revert + + 2 + + + 377 + @@ -2004,7 +2024,7 @@ dHRwOi8vd3d3Lm1vZHJhaWxzLmNvbSBmb3IgaW5zdGFsbGF0aW9uIGluc3RydWN0aW9ucy4 - 375 + 377 diff --git a/PassengerApplication.rb b/PassengerApplication.rb index adf32c2..2c10415 100644 --- a/PassengerApplication.rb +++ b/PassengerApplication.rb @@ -41,6 +41,8 @@ def init @new_app = true @dirty = @valid = false @host, @path = '', '' + + @original_values = { 'host' => @host, 'path' => @path, 'environment' => @environment, 'allow_mod_rewrite' => @allow_mod_rewrite } self end end @@ -54,6 +56,8 @@ def initWithFile(file) @path = data.match(/DocumentRoot\s+"(.+)\/public"\n/)[1] @environment = (data.match(/RailsEnv\s+(development|production)\n/)[1] == 'development' ? DEVELOPMENT : PRODUCTION) @allow_mod_rewrite = (data.match(/RailsAllowModRewrite\s+(off|on)\n/)[1] == 'on') + + @original_values = { 'host' => @host, 'path' => @path, 'environment' => @environment, 'allow_mod_rewrite' => @allow_mod_rewrite } self end end @@ -63,6 +67,8 @@ def initWithPath(path) @dirty = true @path = path set_default_host_from_path(path) + + @original_values = { 'host' => @host, 'path' => @path, 'environment' => @environment, 'allow_mod_rewrite' => @allow_mod_rewrite } self end end @@ -75,6 +81,10 @@ def dirty? @dirty end + def valid? + @valid + end + def apply(sender = nil) p "apply" @new_app ? start : restart @@ -119,6 +129,13 @@ def to_hash } end + def revert(sender = nil) + @original_values.each do |key, value| + send "#{key}=", value + end + self.valid = self.dirty = false + end + private def set_default_host_from_path(path) diff --git a/test/passenger_application_test.rb b/test/passenger_application_test.rb index ea142ef..b899f51 100644 --- a/test/passenger_application_test.rb +++ b/test/passenger_application_test.rb @@ -1,6 +1,14 @@ require File.expand_path('../test_helper', __FILE__) require 'PassengerApplication' +class Hash + def except(key) + copy = dup + copy.delete(key) + copy + end +end + describe "PassengerApplication, with a new application" do tests PassengerApplication @@ -95,8 +103,6 @@ def after_setup end it "should mark the application as dirty if a value has changed" do - passenger_app.stubs(:restart) - assigns(:dirty).should.be false passenger_app.setValue_forKey('het-manfreds-blog.local', 'host') assigns(:dirty).should.be true @@ -169,4 +175,21 @@ def after_setup PassengerApplication.startApplications [app1, app2].to_ns end + + it "should remember all the original values for the case that the user wants to revert" do + passenger_app.setValue_forKey('foo.local', 'host') + passenger_app.setValue_forKey('/some/path', 'path') + passenger_app.setValue_forKey('production', 'environment') + passenger_app.setValue_forKey(true, 'allow_mod_rewrite') + + passenger_app.should.be.dirty + passenger_app.should.be.valid + passenger_app.to_hash.except('config_path').should == { 'host' => 'foo.local', 'path' => '/some/path', 'environment' => 'production', 'allow_mod_rewrite' => true } + + passenger_app.revert + + passenger_app.should.not.be.dirty + passenger_app.should.not.be.valid + passenger_app.to_hash.except('config_path').should == { 'host' => 'het-manfreds-blog.local', 'path' => '/Users/het-manfred/rails code/blog', 'environment' => 'development', 'allow_mod_rewrite' => false } + end end \ No newline at end of file