fix a lot of tiny bugs (not yet) + testing import tool

This commit is contained in:
Didier Lafforgue 2010-09-27 00:31:50 +02:00
parent 9a722c4268
commit e31012f861
13 changed files with 108 additions and 36 deletions

View File

@ -1,12 +1,12 @@
GIT
remote: git://github.com/locomotivecms/custom_fields.git
revision: 89c4d7d
revision: 89c4d7d21efa4c4715c0e34edcc9ce22c405dcd4
specs:
custom_fields (0.0.0.2)
GIT
remote: git://github.com/locomotivecms/liquid.git
revision: b03cdc2
revision: b03cdc289ac36c3395459e295c6bf90baa06d256
ref: b03cdc289ac36c339545
specs:
liquid (2.1.3)
@ -79,7 +79,7 @@ GEM
git (1.2.5)
haml (3.0.18)
has_scope (0.5.0)
heroku (1.10.5)
heroku (1.10.6)
json_pure (>= 1.2.0, < 1.5.0)
launchy (~> 0.3.2)
rest-client (~> 1.4.0)
@ -126,7 +126,7 @@ GEM
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.4)
rack-test (0.5.5)
rack (>= 1.0)
rails (3.0.0)
actionmailer (= 3.0.0)

View File

@ -61,7 +61,7 @@ module Models
context = default_context.merge(context)
@template = ::Liquid::Template.parse(self.raw_template, context)
self.template_dependencies = context[:templates]
self.snippet_dependencies = context[:snippets]

View File

@ -26,11 +26,14 @@ class Snippet
protected
def normalize_slug
# TODO: refactor it
self.slug = self.name.clone if self.slug.blank? && self.name.present?
self.slug.slugify!(:without_extension => true, :downcase => true) if self.slug.present?
end
def update_templates
return unless (self.site rescue nil) # not run if the site is being destroyed
pages = self.site.pages.any_in(:snippet_dependencies => [self.slug]).to_a
pages.each do |page|
@ -44,7 +47,7 @@ class Snippet
case node
when Locomotive::Liquid::Tags::Snippet
node.refresh(self) if node.slug == self.slug
when Locomotive::Liquid::Tags::Block
when Locomotive::Liquid::Tags::InheritedBlock
self._change_snippet_inside_template(node.parent) if node.parent
else
if node.respond_to?(:nodelist)

View File

@ -125,7 +125,9 @@ class ThemeAsset
end
def extname_can_not_be_changed
return if self.new_record?
return if self.new_record? || self.source.file.original_filename.nil?
puts "filename => #{self.source_filename} / source file => #{self.source.file.inspect}"
if File.extname(self.source.file.original_filename) != File.extname(self.source_filename)
self.errors.add(:source, :extname_changed)

View File

@ -23,6 +23,13 @@ x create a repo for a tool "a la" vision
- write my first tutorial about locomotive
- asset collections => liquid
- apply http://github.com/flori/json/commit/2c0f8d2c9b15a33b8d10ffcb1959aef54d320b57
- snippet dependencies => do not work correctly
- images tag to write
- import tool:
- disable sub tasks by passing options
- select field (see custom fields and nocoffee theme) ?
- exceptions
- page to import theme
- refactor slugify method (use parameterize + create a module)
- [content types] the "display column" selector should not include file types

View File

@ -19,6 +19,7 @@ module Locomotive
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 }
@ -36,7 +37,7 @@ module Locomotive
end
def self.add_other_assets(site, theme_path)
collection = AssetCollection.find_or_create_internal(current_site)
collection = AssetCollection.find_or_create_internal(site)
Dir[File.join(theme_path, 'public', 'samples', '*')].each do |asset_path|

View File

@ -16,9 +16,7 @@ module Locomotive
self.add_or_update_fields(content_type, attributes['fields'])
content_type.save
# puts "content_type = #{content_type.inspect}"
content_type.save!
site.reload
end
@ -34,7 +32,7 @@ module Locomotive
def self.add_or_update_fields(content_type, fields)
fields.each do |name, data|
attributes = { :_name => name, :kind => 'String' }.merge(data).symbolize_keys
attributes = { :label => name.humanize, :_name => name, :kind => 'String' }.merge(data).symbolize_keys
field = content_type.content_custom_fields.detect { |f| f._name == attributes[:_name] }

View File

@ -22,14 +22,14 @@ module Locomotive
:site => @site,
:theme_path => @theme_path
}
Locomotive::Import::Site.process(context)
Locomotive::Import::ContentTypes.process(context)
# Locomotive::Import::Assets.process(context)
# Locomotive::Import::Snippets.process(context)
Locomotive::Import::Snippets.process(context)
Locomotive::Import::Pages.process(context)
end
@ -39,6 +39,8 @@ module Locomotive
def unzip!
Zip::ZipFile.open(@theme_file) do |zipfile|
destination_path = File.join(Rails.root, 'tmp', 'themes', @site.id.to_s)
FileUtils.rm_r destination_path, :force => true
zipfile.each do |entry|
next if entry.name =~ /__MACOSX/

View File

@ -4,14 +4,16 @@ module Locomotive
def self.process(context)
site, pages, theme_path = context[:site], context[:database]['pages'], context[:theme_path]
context[:done] = {} # initialize the hash storing pages already processed
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'), '')
fullpath = template_path.gsub(File.join(theme_path, 'templates'), '').gsub('.liquid', '').gsub(/^\//, '')
puts "fullpath = #{fullpath}"
puts "=========== #{fullpath} ================="
next if %w(index 404).include?(fullpath)
@ -19,17 +21,31 @@ module Locomotive
end
end
def add_page(fullpath, context)
site = context[:site]
def self.add_page(fullpath, context)
puts "....adding #{fullpath}"
page = context[:done][fullpath]
return page if page # already added, so skip it
site, pages, theme_path = context[:site], context[:database]['site']['pages'], context[:theme_path]
template = File.read(File.join(theme_path, 'templates', "#{fullpath}.liquid"))
self.build_parent_template(template, context)
parent = self.find_parent(fullpath, context)
puts "adding #{fullpath}"
puts "updating..... #{fullpath} / #{template}"
page = site.pages.where(:fullpath => fullpath).first || site.pages.build
attributes = { :fullpath => fullpath, :parent => parent }.merge(context[:database]['pages'][fullpath] || {})
attributes.symbolize_keys!
attributes = {
:title => fullpath.split('/').last.humanize,
:slug => fullpath.split('/').last,
:parent => parent,
:raw_template => template
}.merge(pages[fullpath] || {}).symbolize_keys
# templatized ?
if content_type_slug = attributes.delete(:content_type)
@ -39,21 +55,49 @@ module Locomotive
page.attributes = attributes
# do not parse liquid templates now
page.instance_variable_set(:@template_changed, false)
# page.instance_variable_set(:@template_changed, false)
page.save!
site.reload
context[:done][fullpath] = page
page
end
def self.build_parent_template(template, context)
puts "building parent_template #{template.blank?}"
# just check if the template contains the extends keyword
# template
fullpath = template.scan(/\{% extends (\w+) %\}/).flatten.first
if fullpath # inheritance detected
fullpath.gsub!("'", '')
puts "found parent_template #{fullpath}"
return if fullpath == 'parent'
self.add_page(fullpath, context)
else
puts "no parent_template found #{fullpath}"
end
end
def find_parent(fullpath, context)
def self.find_parent(fullpath, context)
puts "finding parent for #{fullpath}"
site = context[:site]
segments = fullpath.split('/')
return site.pages.index.first if segments.empty?
return site.pages.index.first if segments.size == 1
(segments.last == 'index' ? 2 : 1).times { segments.pop }
parent_fullpath = File.join(segments.join('/'), 'index')
parent_fullpath = File.join(segments.join('/'), 'index').gsub(/^\//, '')
# look for a local index page in db
parent = site.pages.where(:fullpath => parent_fullpath).first
@ -61,19 +105,25 @@ module Locomotive
parent || self.add_page(parent_fullpath, context)
end
def add_index_and_404(context)
site, database = context[:site], context[:database]
def self.add_index_and_404(context)
site, pages, theme_path = context[:site], context[:database]['site']['pages'], context[:theme_path]
%w(index 404).each do |slug|
page = site.pages.where({ :slug => slug, :depth => 0 }).first
# puts "building system page (#{slug}) => #{page.inspect}"
page ||= sites.pages.build(:slug => slug, :parent => nil)
template = File.read(File.join(theme_path, 'templates', "#{slug}.liquid"))
page.attributes = database['pages'][slug]
page.attributes = { :raw_template => template }.merge(pages[slug] || {})
page.save!
page.save! rescue nil # TODO better error handling
site.reload
context[:done][slug] = page
end
end

View File

@ -10,8 +10,6 @@ module Locomotive
site.attributes = attributes
site.save!
puts "site errors = #{site.errors.inspect}"
end
end

View File

@ -8,9 +8,12 @@ module Locomotive
Dir[File.join(theme_path, 'snippets', '*')].each do |snippet_path|
name = File.basename(snippet_path, File.extname(snippet_path)).parameterize('_')
snippet = site.snippets.where(:slug => name).first || site.snippets.build(:name => name)
snippet = site.snippets.create! :name => name, :template => File.read(snippet_path)
snippet.template = File.read(snippet_path) # = site.snippets.create! :name => name, :template =>
snippet.save!
# puts "snippet = #{snippet.inspect}"
end
end

View File

@ -5,7 +5,7 @@ module Locomotive
def end_tag
super
if !self.contains_super?(@nodelist) # then disable all editable_elements coming from the parent block too and not used
@context[:page].disable_parent_editable_elements(@name)
end
@ -17,7 +17,7 @@ module Locomotive
nodelist.any? do |node|
if node.is_a?(::Liquid::Variable) && node.name == 'block.super'
true
elsif node.respond_to?(:nodelist) && !node.is_a?(Locomotive::Liquid::Tags::InheritedBlock)
elsif node.respond_to?(:nodelist) && !node.nodelist.nil? && !node.is_a?(Locomotive::Liquid::Tags::InheritedBlock)
contains_super?(node.nodelist)
end
end

View File

@ -81,6 +81,9 @@ end
# # empty page (imac 27'): User System Total Real
# Rendering page 10k times 21.390000 1.820000 23.210000 ( 24.120529)
# # empty page (mac mini core 2 duo / 2Go): User System Total Real
# Rendering a simple page 10k times 17.130000 0.420000 17.550000 ( 19.459768)
# # page with inherited template (imac 27'): User System Total Real
# Rendering page 10k times 85.840000 7.600000 93.440000 ( 97.841248)
@ -89,3 +92,8 @@ end
# # with locomotive liquid (imac 27'): User System Total Real
# Rendering page 10k times 38.750000 3.050000 41.800000 ( 42.880022)
# # with locomotive liquid (mac mini core 2 duo / 2Go): User System Total Real
# Rendering a complex page 10k times 30.840000 0.530000 31.370000 ( 32.847565)