Browse files

unbreak config import/export and tests

  • Loading branch information...
1 parent 8c0d32f commit 2dba2eccf8f75b079ff7e1aaccc582f96c949571 @will-r will-r committed Oct 14, 2010
View
6 lib/radiant/config/definition.rb
@@ -93,16 +93,16 @@ def validate(setting)
if allow_blank?
return if setting.value.blank?
else
- setting.errors.add :value, "must not be blank" if setting.value.blank?
+ setting.errors.add :value, "of #{setting.key} must not be blank" if setting.value.blank?
end
if validate_with.is_a? Proc
validate_with.call(setting)
end
if selector?
- setting.errors.add :value, "must choose one of the permitted values" unless selectable?(setting.value)
+ setting.errors.add :value, "of #{setting.key} is not one of the permitted values" unless selectable?(setting.value)
end
if integer?
- Integer(setting.value) rescue setting.errors.add :value, "must be a number"
+ Integer(setting.value) rescue setting.errors.add :value, "of #{setting.key} must be a number"
end
end
View
22 lib/task_support.rb
@@ -16,15 +16,21 @@ def config_export(path = "#{Rails.root}/config/radiant_config.yml")
end
def config_import(path = "#{Rails.root}/config/radiant_config.yml", clear = nil)
self.establish_connection
- Radiant::Config.delete_all if clear
if File.exist?(path)
- configs = YAML.load(YAML.load_file(path))
- configs.each do |key, value|
- c = Radiant::Config.find_or_initialize_by_key(key)
- c.value = value
- c.save
+ begin
+ Radiant::Config.transaction do
+ Radiant::Config.delete_all if clear
+ configs = YAML.load(YAML.load_file(path))
+ configs.each do |key, value|
+ c = Radiant::Config.find_or_initialize_by_key(key)
+ c.value = value
+ c.save
+ end
end
- puts "Radiant::Config updated from #{path}"
+ puts "Radiant::Config updated from #{path}"
+ rescue ActiveRecord::RecordInvalid => e
+ puts "IMPORT FAILED and rolled back. #{e}"
+ end
else
puts "No file exists at #{path}"
end
@@ -37,7 +43,7 @@ def cache_files(dir, files, cache_file)
File.read(File.join(dir, f)) }.join("\n\n")
cache_path = File.join(dir, cache_file)
- rm(cache_path) if File.exists?(cache_path)
+ File.delete(cache_path) if File.exists?(cache_path)
File.open(cache_path, "w+") { |f| f.write(cache_content) }
end
View
10 spec/fixtures/invalid_config.yml
@@ -0,0 +1,10 @@
+--- |
+ ---
+ defaults.page.parts: body, extended
+ test: passes
+ admin.title: RadiantCMS
+ defaults.page.status: Nonexistent
+ admin.subtitle: Publishing for Small Teams
+ this: that
+ defaults.page.filter: ""
+ true?: true
View
10 spec/fixtures/radiant_config.yml
@@ -0,0 +1,10 @@
+--- |
+ ---
+ defaults.page.parts: body, extended
+ test: passes
+ admin.title: RadiantCMS
+ defaults.page.status: Draft
+ admin.subtitle: Publishing for Small Teams
+ this: that
+ defaults.page.filter: ""
+ true?: true
View
18 spec/lib/radiant/config/definition_spec.rb
@@ -4,60 +4,48 @@
before :each do
Radiant::Config.initialize_cache
@basic = Radiant::Config::Definition.new({
- :default => 'quite testy',
- :label => "testiness",
- :notes => "Irritability"
+ :default => 'quite testy'
})
@boolean = Radiant::Config::Definition.new({
:type => :boolean,
- :default => true,
- :label => "bool"
+ :default => true
})
@integer = Radiant::Config::Definition.new({
:type => :integer,
- :default => 50,
- :label => "int"
+ :default => 50
})
@validating = Radiant::Config::Definition.new({
- :label => "validating",
:default => "Monkey",
:validate_with => lambda {|s| s.errors.add(:value, "That's no monkey") unless s.value == "Monkey" }
})
@selecting = Radiant::Config::Definition.new({
- :label => "selecting",
:default => "Monkey",
:select_from => [["m", "Monkey"], ["g", "Goat"]]
})
@selecting_from_hash = Radiant::Config::Definition.new({
- :label => "selecting from hash",
:default => "Non-monkey",
:allow_blank => true,
:select_from => {"monkey" => "Definitely a monkey", "goat" => "No fingers", "Bear" => "Angry, huge", "Donkey" => "Non-monkey"}
})
@selecting_required = Radiant::Config::Definition.new({
- :label => "selecting non-blank",
:default => "other",
:allow_blank => false,
:select_from => lambda { ['recent', 'other', 'misc'] }
})
@enclosed = "something"
@selecting_at_runtime = Radiant::Config::Definition.new({
- :label => "selecting at runtime",
:default => "something",
:select_from => lambda { [@enclosed] }
})
@protected = Radiant::Config::Definition.new({
- :label => "frozen",
:default => "Monkey",
:allow_change => false
})
@hiding = Radiant::Config::Definition.new({
- :label => "hidden",
:default => "Secret Monkey",
:allow_display => false
})
@present = Radiant::Config::Definition.new({
- :label => "present",
:default => "Hola",
:allow_blank => false
})
View
6 spec/lib/task_support_spec.rb
@@ -20,6 +20,7 @@
describe "self.config_import" do
before do
@yaml_file = "#{RADIANT_ROOT}/spec/fixtures/radiant_config.yml"
+ @bad_yaml_file = "#{RADIANT_ROOT}/spec/fixtures/invalid_config.yml"
end
it "should delete all Radiant::Config when the clear parameter is set to true" do
Radiant::Config['testing_clear'] = 'true'
@@ -38,6 +39,11 @@
TaskSupport.config_import(@yaml_file)
Radiant::Config.to_hash.should == YAML.load(YAML.load_file(@yaml_file))
end
+ it "should roll back if an invalid config setting is imported" do
+ Radiant::Config['defaults.page.status'] = "Draft"
+ lambda{TaskSupport.config_import(@bad_yaml_file)}.should_not raise_error
+ Radiant::Config['defaults.page.status'].should == "Draft"
+ end
end
describe "self.cache_files" do

0 comments on commit 2dba2ec

Please sign in to comment.