Skip to content
This repository
Browse code

unbreak config import/export and tests

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

0 comments on commit 2dba2ec

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