From 1cb0df38e6c6610474566916916b8ca9600ebac2 Mon Sep 17 00:00:00 2001 From: Doug Youch Date: Wed, 4 Aug 2010 19:13:32 +0000 Subject: [PATCH] On bundle import can opt to replace same bundle items. Added a method to site template to applay the template to the site. --- app/models/page_paragraph.rb | 4 ++- app/models/site_feature.rb | 15 ++++++++++ app/models/site_template.rb | 33 ++++++++++++++++++---- app/models/webiva_bundler.rb | 30 +++++++++++++------- app/views/templates/import_bundle.html.erb | 2 ++ 5 files changed, 67 insertions(+), 17 deletions(-) diff --git a/app/models/page_paragraph.rb b/app/models/page_paragraph.rb index e7caf853..4d41c4f5 100644 --- a/app/models/page_paragraph.rb +++ b/app/models/page_paragraph.rb @@ -27,7 +27,9 @@ class PageParagraph < DomainModel belongs_to :content_publication - + named_scope :live_paragraphs, {:joins => :page_revision, :conditions => 'page_revisions.active=1 AND page_revisions.revision_type="real"'} + named_scope :with_feature, lambda { |display_module, display_type| {:conditions => ['display_module = ? and display_type = ?', display_module, display_type]} } + # PageParagraph file instance support is in PageRevisions # process_file_instance :display_body, :display_body_html apply_content_filter(:display_body => :display_body_html) do |para| diff --git a/app/models/site_feature.rb b/app/models/site_feature.rb index 2674e097..03376846 100644 --- a/app/models/site_feature.rb +++ b/app/models/site_feature.rb @@ -125,4 +125,19 @@ def export_to_bundle(bundler) bundler.add_folder(self.image_folder) if self.image_folder self.attributes.slice('name', 'description', 'feature_type', 'body', 'options', 'css', 'category', 'archived', 'image_folder_id', 'preprocessor') end + + def self.feature_hash + paragraph_list = ParagraphController.get_editor_paragraphs + SiteModule.get_module_paragraphs.values + output = {} + + paragraph_list.each do |para_list| + para_list[2].each do |paragraph| + next unless paragraph[4][0] + output[paragraph[4][0]] ||= [] + output[paragraph[4][0]] << [ paragraph[3], paragraph[1] ] + end + end + + output + end end diff --git a/app/models/site_template.rb b/app/models/site_template.rb index ba17aace..c40cc4ce 100644 --- a/app/models/site_template.rb +++ b/app/models/site_template.rb @@ -751,15 +751,19 @@ def self.import_bundle(bundler, data, opts={}) domain_file_id = data['domain_file_id'] ? bundler.get_new_input_id(DomainFile, data['domain_file_id']) : nil # Create the site template - site_template = SiteTemplate.find_by_parent_id_and_name(data['parent_id'], data['name']) || SiteTemplate.new(:parent_id => data['parent_id'], :name => data['name']) - site_template.update_attribute data.slice('description', 'template_html', 'options', 'style_struct', 'style_design', 'template_type', 'head', 'doctype', 'partial', 'lightweight', 'preprocessor').merge('domain_file_id' => domain_file_id) + site_template = nil + site_template = SiteTemplate.find_by_parent_id_and_name(data['parent_id'], data['name']) if opts[:replace_same] + site_template ||= SiteTemplate.new(:parent_id => data['parent_id'], :name => data['name']) + site_template.update_attributes data.slice('description', 'template_html', 'options', 'style_struct', 'style_design', 'template_type', 'head', 'doctype', 'partial', 'lightweight', 'preprocessor').merge('domain_file_id' => domain_file_id) # Create the zones + site_template.site_template_zones.clear data['zones'].each_with_index do |name, idx| site_template.site_template_zones.create :name => name, :position => (idx+1) end # Create the features + site_template.site_features.clear data['features'].each do |feature| image_folder_id = feature['image_folder_id'] ? bundler.get_new_input_id(DomainFile, feature['image_folder_id']) : nil site_template.site_features.create feature.merge('image_folder_id' => image_folder_id) @@ -781,12 +785,29 @@ def apply_to_site(version, opts={}) mod.save end - # reset all paragraphs to use default feature - if opts[:reset] - end - # Apply theme features to existing paragraphs if opts[:features] + feature_hash = SiteFeature.feature_hash + + revisions = {} + self.site_features.each do |feature| + next unless feature_hash[feature.feature_type] + feature_hash[feature.feature_type].each do |info| + PageParagraph.live_paragraphs.with_feature(*info).group_by(&:page_revision_id).each do |page_revision_id, paragraphs| + revisions[page_revision_id] ||= [] + revisions[page_revision_id] += paragraphs.map { |para| [para.identity_hash, feature.id] } + end + end + end + + revisions.each do |page_revision_id, paragraphs| + rv = PageRevision.find(page_revision_id).create_temporary + paragraphs.each do |info| + para = rv.page_paragraphs.detect { |p| p.identity_hash == info[0] } + para.update_attribute :site_feature_id, info[1] + end + rv.make_real + end end end end diff --git a/app/models/webiva_bundler.rb b/app/models/webiva_bundler.rb index 188cb5a9..f5aff85a 100644 --- a/app/models/webiva_bundler.rb +++ b/app/models/webiva_bundler.rb @@ -5,7 +5,9 @@ class WebivaBundler < HashModel attr_accessor :importing - attributes :version => nil, :name => nil, :thumb_id => nil, :domain_files => nil, :modules => nil, :inputs => nil, :creator_id => nil, :bundle_file_id => nil + attributes :version => nil, :name => nil, :thumb_id => nil, :domain_files => nil, :modules => nil, :inputs => nil, :creator_id => nil, :bundle_file_id => nil, :replace_same => true + + boolean_options :replace_same domain_file_options :thumb_id, :bundle_file_id @@ -56,7 +58,7 @@ def export_object(obj) def import_object(info, opts={}) handler = info['handler'].camelcase.constantize - handler.import_bundle(self, info['data'], opts[info['handler']] || {}) + handler.import_bundle(self, info['data'], opts) end def export @@ -103,7 +105,7 @@ def export self.bundle_file end - def import(opts={}) + def import return nil unless self.bundle_file self.creator_id ||= bundle_file.creator_id @@ -119,22 +121,29 @@ def import(opts={}) self.modules = manifest['modules'] unless manifest['folders'].empty? - theme_folder = DomainFile.create(:name => self.name, :parent_id => DomainFile.themes_folder.id, :file_type => 'fld', :creator_id => self.creator_id) + theme_folder = self.get_folder(self.name, DomainFile.themes_folder.id) self.import_folder(dir, manifest['folders'].shift, theme_folder) manifest['folders'].each do |info| - folder = DomainFile.create(:name => info['name'], :parent_id => theme_folder.id, :file_type => 'fld', :creator_id => self.creator_id) + folder = self.get_folder(info['name'], theme_folder.id) self.import_folder(dir, info, folder) end end @data = YAML.load_file("#{dir}/#{manifest['data']}")['data'] - self.data.each { |info| self.import_object(info, opts) } + self.data.each { |info| self.import_object(info, :replace_same => self.replace_same) } FileUtils.rm_rf(dir) end + def get_folder(name, parent_id) + folder = nil + folder = DomainFile.find_by_file_type_and_parent_id_and_name('fld', parent_id, name) if self.replace_same + folder ||= DomainFile.create(:name => name, :parent_id => parent_id, :file_type => 'fld', :creator_id => self.creator_id) + folder + end + def export_folder(dir, folder) return nil unless folder.file_type == 'fld' @@ -155,6 +164,7 @@ def import_folder(dir, info, theme_folder) `cd #{dir}; unzip ../#{info['filename']}` file_ids = DomainFile.new(:creator_id => self.creator_id).extract_directory(dir, theme_folder.id) DomainFile.find(file_ids).each { |file| file.post_process!(false) } + DomainFile.find(file_ids).map(&:replace_same) if self.replace_same FileUtils.rm_rf(dir) end @@ -175,11 +185,11 @@ def get_bundle_info self.inputs = manifest['inputs'] self.modules = manifest['modules'] - unless manifest['thumb'].blank? || self.thumb + if ! manifest['thumb'].blank? && self.thumb.nil? thumbnail_folder = DomainFile.find(:first,:conditions => "name = 'Thumbnails' and parent_id = #{DomainFile.themes_folder.id}") || DomainFile.create(:name => 'Thumbnails', :parent_id => DomainFile.themes_folder.id, :file_type => 'fld') `cd #{dir}; tar zxf #{self.bundle_file.name} #{manifest['thumb']}` File.open("#{dir}/#{manifest['thumb']}", "r") do |fd| - self.thumb_id = DomainFile.create :filename => fd, :parent_id => thumbnail_folder.id, :process_immediately => true + self.thumb_id = DomainFile.create(:filename => fd, :parent_id => thumbnail_folder.id, :process_immediately => true).id end end @@ -189,11 +199,11 @@ def get_bundle_info end def run_worker - DomainModel.run_worker(self.class.to_s, nil, :import_bundle, :bundle_file_id => self.bundle_file_id, :inputs => self.inputs) + DomainModel.run_worker(self.class.to_s, nil, :import_bundle, :bundle_file_id => self.bundle_file_id, :inputs => self.inputs, :replace_same => self.replace_same) end def self.import_bundle(opts={}) - bundler = WebivaBundler.new :bundle_file_id => opts[:bundle_file_id], :inputs => opts[:inputs] + bundler = WebivaBundler.new :bundle_file_id => opts[:bundle_file_id], :inputs => opts[:inputs], :replace_same => opts[:replace_same] bundler.import end end diff --git a/app/views/templates/import_bundle.html.erb b/app/views/templates/import_bundle.html.erb index 0a5bf443..82be8eb2 100644 --- a/app/views/templates/import_bundle.html.erb +++ b/app/views/templates/import_bundle.html.erb @@ -24,6 +24,8 @@ <% admin_form_for :bundler, @bundler do |f| -%> <%= f.hidden_field :bundle_file_id %> <%= f.hidden_field :thumb_id %> + <%= f.check_boxes :replace_same, [['Replace matching bundle elements', true]], :single => true, :label => '' %> + <%= f.spacer %> <%= f.cancel_submit_buttons 'Cancel', 'Import' %> <% end -%> <% else -%>