working on importing pages
This commit is contained in:
parent
39886427e0
commit
9a722c4268
6
Gemfile
6
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'
|
||||
|
@ -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
|
||||
|
@ -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/<content_type>/<slug> 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 /<content_type>/<slug> 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
|
||||
|
@ -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
|
||||
|
@ -29,6 +29,10 @@ class String
|
||||
replace(self.slugify(options))
|
||||
end
|
||||
|
||||
def parameterize!(sep = '_')
|
||||
replace(self.parameterize(sep))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
## Hash
|
||||
|
@ -1,3 +1,6 @@
|
||||
require 'locomotive/import/job'
|
||||
require 'locomotive/import/site'
|
||||
require 'locomotive/import/content_types'
|
||||
require 'locomotive/import/assets'
|
||||
require 'locomotive/import/content_types'
|
||||
require 'locomotive/import/snippets'
|
||||
require 'locomotive/import/pages'
|
53
lib/locomotive/import/assets.rb
Normal file
53
lib/locomotive/import/assets.rb
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
82
lib/locomotive/import/pages.rb
Normal file
82
lib/locomotive/import/pages.rb
Normal file
@ -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
|
@ -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
|
||||
|
20
lib/locomotive/import/snippets.rb
Normal file
20
lib/locomotive/import/snippets.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user