From 0ea527550433e53ae01126dc485f6dede20b8350 Mon Sep 17 00:00:00 2001 From: did Date: Fri, 8 Jul 2011 23:06:07 +0200 Subject: [PATCH] make the export more solid --- doc/TODO | 7 ++-- lib/locomotive/export.rb | 19 ++++++---- spec/lib/locomotive/export_spec.rb | 59 +++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/doc/TODO b/doc/TODO index a29ab506..2660346c 100644 --- a/doc/TODO +++ b/doc/TODO @@ -71,10 +71,8 @@ x Rights to set roles (ticket #104) x export: problems with templatized pages (source => multi levels pages) x do not rename files for fonts x icon for redirection page in the pages section (back-office) -- tooltip to explain the difference between 1.) Admin 2.) Author 3.) Designer? - test and/or convert existing templates (the 2 of the themes section) - -- [bushido] guiders / welcome page / devise cas authentication (SSO) +- installed on heroku with source BACKLOG: @@ -99,6 +97,9 @@ BACKLOG: - import only theme assets - endless pagination - overide sort for contents +- tooltip to explain the difference between 1.) Admin 2.) Author 3.) Designer? +- [bushido] guiders / welcome page / devise cas authentication (SSO) + REFACTORING: diff --git a/lib/locomotive/export.rb b/lib/locomotive/export.rb index e82332a8..7d41f3a4 100644 --- a/lib/locomotive/export.rb +++ b/lib/locomotive/export.rb @@ -11,7 +11,7 @@ module Locomotive def initialize(site, filename = nil) @site = site @filename = filename || Time.now.to_i.to_s - @target_folder = File.join(Rails.root, 'tmp', 'export', filename) + @target_folder = File.join(Rails.root, 'tmp', 'export', @filename) @site_hash = {} # used to generate the site.yml and compiled_site.yml files self.create_target_folder @@ -259,7 +259,7 @@ module Locomotive content.custom_fields.each do |field| next if field._name == highlighted_field_name - case field.kind + value = (case field.kind when 'file' uploader = content.send(field._name) unless uploader.blank? @@ -268,13 +268,18 @@ module Locomotive else filepath = nil end - hash[field._alias] = filepath + filepath when 'text' - value = self.replace_asset_urls_in(content.send(field._name.to_sym) || '') - hash[field._alias] = value + self.replace_asset_urls_in(content.send(field._name.to_sym) || '') + when 'has_one' + content.send(field.safe_alias.to_sym).highlighted_field_value + when 'has_many' + content.send(field.safe_alias.to_sym).collect(&:highlighted_field_value) else - hash[field._alias] = content.send(field._name.to_sym) - end + content.send(field.safe_alias.to_sym) + end) + + hash[field._alias] = value end data << { content.highlighted_field_value => hash } diff --git a/spec/lib/locomotive/export_spec.rb b/spec/lib/locomotive/export_spec.rb index ed0432ef..d7856a89 100644 --- a/spec/lib/locomotive/export_spec.rb +++ b/spec/lib/locomotive/export_spec.rb @@ -2,7 +2,64 @@ require 'spec_helper' describe Locomotive::Export do - context 'when successful' do + context '#content_type' do + + before(:each) do + site = Factory.build(:site) + Site.stubs(:find).returns(site) + project_type = build_project_type(site) + project_type.contents.build(:title => 'Project #1', :description => 'Lorem ipsum', :active => true) + project_type.contents.build(:title => 'Project #2', :description => 'More Lorem ipsum', :active => false) + + team_type = build_team_type(site, project_type) + team_type.contents.build(:name => 'Ben', :projects => project_type.contents, :current_project => project_type.contents.first) + team_type.contents.build(:name => 'Zach', :current_project => project_type.contents.last) + + @project_data = ::Locomotive::Export.new(site).send(:extract_contents, project_type) + @team_data = ::Locomotive::Export.new(site).send(:extract_contents, team_type) + end + + it 'includes the exact number of contents' do + @project_data.size.should == 2 + @project_data.collect { |n| n.keys.first }.should == ['Project #1', 'Project #2'] + end + + it 'deals with real booleans' do + @project_data.first.values.first['active'].should be_true + end + + it 'stores the list of highlighted values in a has_many relationship' do + @team_data.first.values.first['projects'].size.should == 2 + @team_data.first.values.first['projects'].should == ['Project #1', 'Project #2'] + @team_data.last.values.first['projects'].should == [] + end + + it 'stores a highlighted value in a has_one relationship' do + @team_data.collect { |n| n.values.first['current_project'] }.should == ['Project #1', 'Project #2'] + end + + def build_project_type(site) + Factory.build(:content_type, :site => site, :highlighted_field_name => 'custom_field_1').tap do |content_type| + content_type.content_custom_fields.build :label => 'Title', :_alias => 'title', :kind => 'string' + content_type.content_custom_fields.build :label => 'My Description', :_alias => 'description', :kind => 'text' + content_type.content_custom_fields.build :label => 'Active', :kind => 'boolean' + end + end + + def build_team_type(site, project_type) + Object.send(:remove_const, 'TestProject') rescue nil + klass = Object.const_set('TestProject', Class.new { def self.embedded?; false; end }) + content_type = Factory.build(:content_type, :site => site, :name => 'team', :highlighted_field_name => 'custom_field_1') + content_type.content_custom_fields.build :label => 'Name', :_alias => 'name', :kind => 'string' + content_type.content_custom_fields.build :label => 'Projects', :kind => 'has_many', :_alias => 'projects', :target => 'TestProject' + content_type.content_custom_fields.build :label => 'Bio', :_alias => 'bio', :kind => 'text' + content_type.content_custom_fields.build :label => 'Current Project', :kind => 'has_one', :_alias => 'current_project', :target => 'TestProject' + content_type + end + + end + + context '#zipfile' do before(:all) do @site = Factory('another site')