From 9a722c4268ee389fdf31cd62c1ce0596b8f22c00 Mon Sep 17 00:00:00 2001 From: dinedine Date: Fri, 24 Sep 2010 01:00:13 +0200 Subject: [PATCH] working on importing pages --- Gemfile | 6 +- Gemfile.lock | 8 --- app/models/theme_asset.rb | 52 +++++++++------- app/uploaders/theme_asset_uploader.rb | 3 +- lib/core_ext.rb | 4 ++ lib/locomotive/import.rb | 5 +- lib/locomotive/import/assets.rb | 53 +++++++++++++++++ lib/locomotive/import/content_types.rb | 6 +- lib/locomotive/import/job.rb | 24 +++++--- lib/locomotive/import/pages.rb | 82 ++++++++++++++++++++++++++ lib/locomotive/import/site.rb | 4 +- lib/locomotive/import/snippets.rb | 20 +++++++ 12 files changed, 223 insertions(+), 44 deletions(-) create mode 100644 lib/locomotive/import/assets.rb create mode 100644 lib/locomotive/import/pages.rb create mode 100644 lib/locomotive/import/snippets.rb diff --git a/Gemfile b/Gemfile index 12cd6b78..70092740 100644 --- a/Gemfile +++ b/Gemfile @@ -36,9 +36,9 @@ group :development do gem 'fastthread' end -group :test, :development do - gem 'ruby-debug' -end +# group :test, :development do +# gem 'ruby-debug' +# end # group :test do # gem 'autotest' diff --git a/Gemfile.lock b/Gemfile.lock index 0d35c9a9..42094073 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,7 +56,6 @@ GEM carrierwave (0.5.0.beta2) activesupport (>= 3.0.0.beta4) cgi_multipart_eof_fix (2.5.0) - columnize (0.3.1) configuration (1.1.0) crack (0.1.8) daemons (1.1.0) @@ -99,7 +98,6 @@ GEM launchy (0.3.7) configuration (>= 0.0.5) rake (>= 0.8.1) - linecache (0.43) mail (2.2.6.1) activesupport (>= 2.3.6) mime-types @@ -148,11 +146,6 @@ GEM rest-client (1.4.2) mime-types (>= 1.16) rmagick (2.12.2) - ruby-debug (0.10.3) - columnize (>= 0.1) - ruby-debug-base (~> 0.10.3.0) - ruby-debug-base (0.10.3) - linecache (>= 0.3) rubyforge (2.0.4) json_pure (>= 1.1.7) rubyzip (0.9.4) @@ -195,6 +188,5 @@ DEPENDENCIES mongrel rails (= 3.0.0) rmagick (= 2.12.2) - ruby-debug rubyzip warden diff --git a/app/models/theme_asset.rb b/app/models/theme_asset.rb index dadf93f9..d67cba66 100644 --- a/app/models/theme_asset.rb +++ b/app/models/theme_asset.rb @@ -24,7 +24,8 @@ class ThemeAsset ## callbacks ## before_validation :sanitize_slug before_validation :store_plain_text - before_save :set_slug + # before_validation :escape_shortcut_urls + before_save :set_slug ## validations ## validate :extname_can_not_be_changed @@ -69,30 +70,22 @@ class ThemeAsset end def store_plain_text - return if self.plain_text.blank? + return if !self.stylesheet_or_javascript? || self.plain_text.blank? - # replace /theme// occurences by the real amazon S3 url or local files - sanitized_source = self.plain_text.gsub(/(\/theme\/([a-z]+)\/([a-z_\-0-9]+)\.[a-z]{2,3})/) do |url| - content_type, slug = url.split('/')[2..-1] + sanitized_source = self.escape_shortcut_urls(self.plain_text) - content_type = content_type.singularize - slug = slug.split('.').first - - if asset = self.site.theme_assets.where(:content_type => content_type, :slug => slug).first - asset.source.url - else - url - end + if self.source.nil? + self.source = CarrierWave::SanitizedFile.new({ + :tempfile => StringIO.new(sanitized_source), + :filename => "#{self.slug}.#{self.stylesheet? ? 'css' : 'js'}" + }) + else + self.source.file.instance_variable_set(:@file, StringIO.new(sanitized_source)) end - - self.source = CarrierWave::SanitizedFile.new({ - :tempfile => StringIO.new(sanitized_source), - :filename => "#{self.slug}.#{self.stylesheet? ? 'css' : 'js'}" - }) end - def shortcut_url # ex: /theme/stylesheets/application.css is a shortcut for a theme asset (content_type => stylesheet, slug => 'application') - File.join('/theme', self.content_type.pluralize, "#{self.slug}#{File.extname(self.source_filename)}") + def shortcut_url # ex: /stylesheets/application.css is a shortcut for a theme asset (content_type => stylesheet, slug => 'application') + File.join(self.content_type.pluralize, "#{self.slug}#{File.extname(self.source_filename)}") rescue '' end @@ -103,8 +96,25 @@ class ThemeAsset protected + def escape_shortcut_urls(text) # replace // occurences by the real amazon S3 url or local files + return if text.blank? + + text.gsub(/(\/(stylesheets|javascripts|images)\/([a-z_\-0-9]+)\.[a-z]{2,3})/) do |url| + content_type, slug = url.split('/')[1..-1] + + content_type = content_type.singularize + slug = slug.split('.').first + + if asset = self.site.theme_assets.where(:content_type => content_type, :slug => slug).first + asset.source.url + else + url + end + end + end + def sanitize_slug - self.slug.slugify!(:underscore => true) if self.slug.present? + self.slug.parameterize! if self.slug.present? end def set_slug diff --git a/app/uploaders/theme_asset_uploader.rb b/app/uploaders/theme_asset_uploader.rb index 30a7025b..346e0146 100644 --- a/app/uploaders/theme_asset_uploader.rb +++ b/app/uploaders/theme_asset_uploader.rb @@ -22,7 +22,8 @@ class ThemeAssetUploader < AssetUploader end def store_dir - "sites/#{model.site_id}/themes/#{model.id}" + # "sites/#{model.site_id}/themes/#{model.id}" + "sites/#{model.site_id}/theme/" end def extension_white_list diff --git a/lib/core_ext.rb b/lib/core_ext.rb index 4553236a..32daa09e 100644 --- a/lib/core_ext.rb +++ b/lib/core_ext.rb @@ -29,6 +29,10 @@ class String replace(self.slugify(options)) end + def parameterize!(sep = '_') + replace(self.parameterize(sep)) + end + end ## Hash diff --git a/lib/locomotive/import.rb b/lib/locomotive/import.rb index ae5a73cb..19f1b3a5 100644 --- a/lib/locomotive/import.rb +++ b/lib/locomotive/import.rb @@ -1,3 +1,6 @@ require 'locomotive/import/job' require 'locomotive/import/site' -require 'locomotive/import/content_types' \ No newline at end of file +require 'locomotive/import/assets' +require 'locomotive/import/content_types' +require 'locomotive/import/snippets' +require 'locomotive/import/pages' \ No newline at end of file diff --git a/lib/locomotive/import/assets.rb b/lib/locomotive/import/assets.rb new file mode 100644 index 00000000..0cb3e95e --- /dev/null +++ b/lib/locomotive/import/assets.rb @@ -0,0 +1,53 @@ +module Locomotive + module Import + module Assets + + def self.process(context) + site, theme_path = context[:site], context[:theme_path] + + self.add_theme_assets(site, theme_path) + + self.add_other_assets(site, theme_path) + end + + def self.add_theme_assets(site, theme_path) + %w(images stylesheets javascripts).each do |kind| + Dir[File.join(theme_path, 'public', kind, '*')].each do |asset_path| + + next if File.directory?(asset_path) + + slug = File.basename(asset_path, File.extname(asset_path)).parameterize('_') + + asset = site.theme_assets.where(:content_type => kind.singularize, :slug => slug).first + asset ||= site.theme_assets.build + + asset.attributes = { :source => File.open(asset_path), :performing_plain_text => false } + asset.save! + + site.reload + # asset.reload + # + # puts "asset.url = #{asset.source.url}" + # + # # asset = site.theme_assets.create! :source => File.open(asset_path), :performing_plain_text => false + # # puts "#{asset.source.inspect} / #{asset.inspect}\n--------\n" + end + end + end + + def self.add_other_assets(site, theme_path) + collection = AssetCollection.find_or_create_internal(current_site) + + Dir[File.join(theme_path, 'public', 'samples', '*')].each do |asset_path| + + next if File.directory?(asset_path) + + name = File.basename(asset_path, File.extname(asset_path)).parameterize('_') + + collection.assets.create! :name => name, :source => File.open(asset_path) + end + end + + end + end +end \ No newline at end of file diff --git a/lib/locomotive/import/content_types.rb b/lib/locomotive/import/content_types.rb index a810cd98..3d21b5af 100644 --- a/lib/locomotive/import/content_types.rb +++ b/lib/locomotive/import/content_types.rb @@ -2,7 +2,9 @@ module Locomotive module Import module ContentTypes - def self.process(database, site, theme_path) + def self.process(context) + site, database = context[:site], context[:database] + content_types = database['site']['content_types'] return if content_types.nil? @@ -16,7 +18,7 @@ module Locomotive content_type.save - puts "content_type = #{content_type.inspect}" + # puts "content_type = #{content_type.inspect}" site.reload end diff --git a/lib/locomotive/import/job.rb b/lib/locomotive/import/job.rb index d5ca8913..6f04dc07 100644 --- a/lib/locomotive/import/job.rb +++ b/lib/locomotive/import/job.rb @@ -17,9 +17,21 @@ module Locomotive raise "No database.yml found in the theme zipfile" if @database.nil? - Locomotive::Import::Site.process(@database, @site, @theme_path) + context = { + :database => @database, + :site => @site, + :theme_path => @theme_path + } - Locomotive::Import::ContentTypes.process(@database, @site, @theme_path) + Locomotive::Import::Site.process(context) + + Locomotive::Import::ContentTypes.process(context) + + # Locomotive::Import::Assets.process(context) + + # Locomotive::Import::Snippets.process(context) + + Locomotive::Import::Pages.process(context) end protected @@ -39,11 +51,9 @@ module Locomotive next end - puts entry.name - # - # FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name))) - # - # zipfile.extract(entry, File.join(destination_path, entry.name)) + FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name))) + + zipfile.extract(entry, File.join(destination_path, entry.name)) end end diff --git a/lib/locomotive/import/pages.rb b/lib/locomotive/import/pages.rb new file mode 100644 index 00000000..42c06b16 --- /dev/null +++ b/lib/locomotive/import/pages.rb @@ -0,0 +1,82 @@ +module Locomotive + module Import + module Pages + + def self.process(context) + site, pages, theme_path = context[:site], context[:database]['pages'], context[:theme_path] + + self.add_index_and_404(context) + + Dir[File.join(theme_path, 'templates', '**/*')].each do |template_path| + + fullpath = template_path.gsub(File.join(theme_path, 'templates'), '') + + puts "fullpath = #{fullpath}" + + next if %w(index 404).include?(fullpath) + + self.add_page(fullpath, context) + end + end + + def add_page(fullpath, context) + site = context[:site] + + parent = self.find_parent(fullpath, context) + + puts "adding #{fullpath}" + + page = site.pages.where(:fullpath => fullpath).first || site.pages.build + + attributes = { :fullpath => fullpath, :parent => parent }.merge(context[:database]['pages'][fullpath] || {}) + attributes.symbolize_keys! + + # templatized ? + if content_type_slug = attributes.delete(:content_type) + attributes[:content_type] = site.content_types.where(:slug => content_type_slug).first + end + + page.attributes = attributes + + # do not parse liquid templates now + page.instance_variable_set(:@template_changed, false) + + page.save! + + site.reload + end + + def find_parent(fullpath, context) + segments = fullpath.split('/') + + return site.pages.index.first if segments.empty? + + (segments.last == 'index' ? 2 : 1).times { segments.pop } + + parent_fullpath = File.join(segments.join('/'), 'index') + + # look for a local index page in db + parent = site.pages.where(:fullpath => parent_fullpath).first + + parent || self.add_page(parent_fullpath, context) + end + + def add_index_and_404(context) + site, database = context[:site], context[:database] + + %w(index 404).each do |slug| + page = site.pages.where({ :slug => slug, :depth => 0 }).first + + page ||= sites.pages.build(:slug => slug, :parent => nil) + + page.attributes = database['pages'][slug] + + page.save! + + site.reload + end + end + + end + end +end \ No newline at end of file diff --git a/lib/locomotive/import/site.rb b/lib/locomotive/import/site.rb index af3084c0..33286e71 100644 --- a/lib/locomotive/import/site.rb +++ b/lib/locomotive/import/site.rb @@ -2,7 +2,9 @@ module Locomotive module Import module Site - def self.process(database, site, theme_path) + def self.process(context) + site, database = context[:site], context[:database] + attributes = database['site'].clone.delete_if { |name, value| %w{pages content_types asset_collections}.include?(name) } site.attributes = attributes diff --git a/lib/locomotive/import/snippets.rb b/lib/locomotive/import/snippets.rb new file mode 100644 index 00000000..05a62014 --- /dev/null +++ b/lib/locomotive/import/snippets.rb @@ -0,0 +1,20 @@ +module Locomotive + module Import + module Snippets + + def self.process(context) + site, theme_path = context[:site], context[:theme_path] + + Dir[File.join(theme_path, 'snippets', '*')].each do |snippet_path| + + name = File.basename(snippet_path, File.extname(snippet_path)).parameterize('_') + + snippet = site.snippets.create! :name => name, :template => File.read(snippet_path) + + # puts "snippet = #{snippet.inspect}" + end + end + + end + end +end \ No newline at end of file