-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
195 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
label: Fixture File | ||
description: Fixture Description | ||
categories: | ||
- category_a | ||
- category_b |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module ComfortableMexicanSofa::Fixture::File | ||
class Importer < ComfortableMexicanSofa::Fixture::Importer | ||
def import! | ||
Dir["#{self.path}[^_]*"].each do |file_path| | ||
filename = ::File.basename(file_path) | ||
file = self.site.files.where(:file_file_name => filename).first || self.site.files.new | ||
|
||
# setting attributes | ||
categories = [] | ||
if File.exists?(attrs_path = File.join(self.path, "_#{filename}.yml")) | ||
if fresh_fixture?(file, attrs_path) | ||
attrs = get_attributes(attrs_path) | ||
file.label = attrs['label'] | ||
file.description = attrs['description'] | ||
categories = attrs['categories'] | ||
end | ||
end | ||
|
||
# setting actual file | ||
if fresh_fixture?(file, file_path) | ||
file.file = open(file_path) | ||
end | ||
|
||
if file.changed? || self.force_import | ||
if file.save | ||
save_categorizations!(file, categories) | ||
ComfortableMexicanSofa.logger.warn("[FIXTURES] Imported File \t #{file.file_file_name}") | ||
else | ||
ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import File \n#{file.errors.inspect}") | ||
end | ||
end | ||
|
||
self.fixture_ids << file.id | ||
end | ||
|
||
# cleaning up | ||
self.site.files.where('id NOT IN (?)', fixture_ids).each{ |s| s.destroy } | ||
end | ||
end | ||
|
||
class Exporter < ComfortableMexicanSofa::Fixture::Exporter | ||
def export! | ||
prepare_folder!(self.path) | ||
|
||
self.site.files.each do |file| | ||
file_path = File.join(self.path, file.file_file_name) | ||
|
||
# writing attributes | ||
open(::File.join(self.path, "_#{file.file_file_name}.yml"), 'w') do |f| | ||
f.write({ | ||
'label' => file.label, | ||
'description' => file.description, | ||
'categories' => file.categories.map{|c| c.label} | ||
}.to_yaml) | ||
end | ||
|
||
# writing content | ||
data_path = file.file.options[:storage] == :filesystem ? | ||
file.file.path : | ||
file.file.url | ||
|
||
open(::File.join(self.path, ::File.basename(file_path)), 'w') do |f| | ||
f.write(open(data_path)) | ||
end | ||
|
||
ComfortableMexicanSofa.logger.warn("[FIXTURES] Exported File \t #{file.file_file_name}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
default: | ||
site: default | ||
block: | ||
label: Sample | ||
label: Default File | ||
file_file_name: sample.jpg | ||
file_content_type: image/jpeg | ||
file_file_size: 20099 | ||
description: Description | ||
description: Default Description | ||
position: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# encoding: utf-8 | ||
|
||
require_relative '../../test_helper' | ||
|
||
class FixtureFilesTest < ActiveSupport::TestCase | ||
|
||
def test_creation | ||
Cms::File.delete_all | ||
|
||
assert_difference 'Cms::File.count' do | ||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site').import! | ||
assert file = Cms::File.last | ||
|
||
assert_equal 'Fixture File', file.label | ||
assert_equal 'sample.jpg', file.file_file_name | ||
assert_equal 'Fixture Description', file.description | ||
|
||
assert_equal 2, file.categories.count | ||
assert_equal ['category_a', 'category_b'], file.categories.map{|c| c.label} | ||
end | ||
end | ||
|
||
def test_update | ||
file = cms_files(:default) | ||
file.update_column(:updated_at, 10.years.ago) | ||
assert_equal 'sample.jpg', file.file_file_name | ||
assert_equal 'Default File', file.label | ||
assert_equal 'Default Description', file.description | ||
|
||
assert_no_difference 'Cms::Snippet.count' do | ||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site').import! | ||
file.reload | ||
assert_equal 'sample.jpg', file.file_file_name | ||
assert_equal 'Fixture File', file.label | ||
assert_equal 'Fixture Description', file.description | ||
end | ||
end | ||
|
||
def test_update_ignore | ||
file = cms_files(:default) | ||
file_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'sample-site', 'files', 'sample.jpg') | ||
attr_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'sample-site', 'files', '_sample.jpg.yml') | ||
|
||
assert file.updated_at >= File.mtime(file_path) | ||
assert file.updated_at >= File.mtime(attr_path) | ||
|
||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site').import! | ||
file.reload | ||
assert_equal 'sample.jpg', file.file_file_name | ||
assert_equal 'Default File', file.label | ||
assert_equal 'Default Description', file.description | ||
end | ||
|
||
def test_update_force | ||
file = cms_files(:default) | ||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site').import! | ||
file.reload | ||
assert_equal 'Default File', file.label | ||
|
||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site', :forced).import! | ||
file.reload | ||
assert_equal 'Fixture File', file.label | ||
end | ||
|
||
def test_delete | ||
old_file = cms_files(:default) | ||
old_file.update_column(:file_file_name, 'old') | ||
|
||
assert_no_difference 'Cms::File.count' do | ||
ComfortableMexicanSofa::Fixture::File::Importer.new('sample-site', 'default-site').import! | ||
assert file = Cms::File.last | ||
assert_equal 'sample.jpg', file.file_file_name | ||
assert_equal 'Fixture File', file.label | ||
assert_equal 'Fixture Description', file.description | ||
|
||
assert Cms::File.where(:id => old_file.id).blank? | ||
end | ||
end | ||
|
||
def test_export | ||
host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test-site') | ||
attr_path = File.join(host_path, 'files/_sample.jpg.yml') | ||
file_path = File.join(host_path, 'files/sample.jpg') | ||
|
||
Paperclip::Attachment.any_instance.stubs(:path). | ||
returns(File.join(Rails.root, 'db/cms_fixtures/sample-site/files/sample.jpg')) | ||
ComfortableMexicanSofa::Fixture::File::Exporter.new('default-site', 'test-site').export! | ||
|
||
assert File.exists?(attr_path) | ||
assert File.exists?(file_path) | ||
assert_equal ({ | ||
'label' => 'Default File', | ||
'description' => 'Default Description', | ||
'categories' => ['Default'] | ||
}), YAML.load_file(attr_path) | ||
|
||
FileUtils.rm_rf(host_path) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters