fixing up fixtures

Oleg committed Jan 03, 2012
commit 187271247bede50da2e5e6070d4123de1be2296f
Showing 4 changed files with 41 additions and 7 deletions
app/models/cms/site.rb +6 -1
@@ @@ -12,7 +12,8 @@ class Cms::Site < ActiveRecord::Base
has_many :categories, :dependent => :destroy
# -- Callbacks ------------------------------------------------------------
- before_validation :assign_label
+ before_validation :assign_identifier,
+ :assign_label
before_save :clean_path
# -- Validations ----------------------------------------------------------
@@ @@ -47,6 +48,10 @@ class Cms::Site < ActiveRecord::Base
end
protected
+
+ def assign_identifier
+ self.identifier = self.identifier.blank?? self.hostname.try(:idify) : self.identifier
+ end
def assign_label
self.label = self.label.blank?? self.identifier.try(:titleize) : self.label
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb +15 -6
@@ @@ -55,8 +55,11 @@ module ComfortableMexicanSofa::Fixtures
# saving
layout.parent = parent
if layout.changed?
- layout.save!
- Rails.logger.debug "[Fixtures] Saved Layout {#{layout.identifier}}"
+ if layout.save
+ $stdout.puts "[Fixtures] Saved Layout {#{layout.identifier}}"
+ else
+ $stderr.puts "[Fixtures] Failed to save Layout {#{layout.errors.inspect}}"
+ end
end
layout_ids << layout.id
@@ @@ -118,8 +121,11 @@ module ComfortableMexicanSofa::Fixtures
# saving
page.blocks_attributes = blocks_attributes if blocks_attributes.present?
if page.changed? || blocks_attributes.present?
- page.save!
- Rails.logger.debug "[Fixtures] Saved Page {#{page.full_path}}"
+ if page.save
+ $stdout.puts "[Fixtures] Saved Page {#{page.full_path}}"
+ else
+ $stderr.puts "[Fixtures] Failed to save Page {#{page.errors.inspect}}"
+ end
end
page_ids << page.id
@@ @@ -168,8 +174,11 @@ module ComfortableMexicanSofa::Fixtures
# saving
if snippet.changed?
- snippet.save!
- Rails.logger.debug "[Fixtures] Saved Snippet {#{snippet.identifier}}"
+ if snippet.save
+ $stdout.puts "[Fixtures] Saved Snippet {#{snippet.identifier}}"
+ else
+ $stderr.puts "[Fixtures] Failed to save Snippet {#{snippet.errors.inspect}}"
+ end
end
snippet_ids << snippet.id
end
test/unit/fixtures_test.rb +14 -0
@@ @@ -208,6 +208,20 @@ class FixturesTest < ActiveSupport::TestCase
end
end
+ def test_import_all_with_no_site
+ cms_sites(:default).destroy
+
+ assert_difference 'Cms::Site.count', 1 do
+ assert_difference 'Cms::Layout.count', 2 do
+ assert_difference 'Cms::Page.count', 2 do
+ assert_difference 'Cms::Snippet.count', 1 do
+ ComfortableMexicanSofa::Fixtures.import_all('test.host', 'example.com')
+ end
+ end
+ end
+ end
+ end
+
def test_export_layouts
host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test.test')
layout_1_attr_path = File.join(host_path, 'layouts/nested/_nested.yml')
test/unit/models/site_test.rb +6 -0
@@ @@ -43,6 +43,12 @@ class CmsSiteTest < ActiveSupport::TestCase
assert s2.valid?
end
+ def test_identifier_assignment
+ site = Cms::Site.new(:hostname => 'my-site.host')
+ assert site.valid?
+ assert_equal 'my_site_host', site.identifier
+ end
+
def test_label_assignment
site = Cms::Site.new(:identifier => 'test', :hostname => 'my-site.host')
assert site.valid?