polish the import module + some code refactoring / cleaning

This commit is contained in:
dinedine 2010-10-12 02:12:18 +02:00
parent 54063d6b46
commit 1c86db05f8
16 changed files with 45 additions and 104 deletions

View File

@ -12,7 +12,10 @@ module Admin
format.html do format.html do
redirect_to new_admin_import_url if @job.nil? redirect_to new_admin_import_url if @job.nil?
end end
format.json { render :json => { :step => @job.nil? ? 'done' : @job.step, :failed => @job.try(:failed?) } } format.json { render :json => {
:step => @job.nil? ? 'done' : @job.step,
:failed => @job && @job.last_error.present?
} }
end end
end end
@ -22,7 +25,7 @@ module Admin
def create def create
if params[:zipfile].blank? if params[:zipfile].blank?
@error = t('errors.messages.blank') @error = t('errors.messages.blank')
flash[:alert] = t('flash.admin.imports.create.error') flash[:alert] = t('flash.admin.imports.create.alert')
render 'new' render 'new'
else else
path = self.store_zipfile! path = self.store_zipfile!
@ -30,7 +33,7 @@ module Admin
job = Locomotive::Import::Job.new(path, current_site) job = Locomotive::Import::Job.new(path, current_site)
Delayed::Job.enqueue job, { :site => current_site, :job_type => 'import' } Delayed::Job.enqueue job, { :site => current_site, :job_type => 'import' }
flash[:notice] = t('flash.admin.imports.create.alert') flash[:notice] = t('flash.admin.imports.create.notice')
redirect_to admin_import_url redirect_to admin_import_url
end end

View File

@ -8,8 +8,8 @@
%p!= t('.help') %p!= t('.help')
%ul{ :id => 'import-steps', :class => 'list', 'data-url' => admin_import_url(:json) } %ul{ :id => 'import-steps', :class => 'list', :'data-url' => admin_import_url(:json), :'data-success-message' => t('.message.success'), :'data-failure-message' => t('.message.failure') }
- %w(site content_types assets snippets pages).each do |step| - %w(site content_types assets asset_collections snippets pages).each do |step|
%li{ :id => "#{step}-step" } %li{ :id => "#{step}-step" }
%em %em
%strong %strong

View File

@ -48,6 +48,5 @@ module Locomotive
config.filter_parameters << :password config.filter_parameters << :password
config.middleware.insert_after ::ActionDispatch::Static, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts} config.middleware.insert_after ::ActionDispatch::Static, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts}
# config.middleware.insert_after 'Rack::Lock', 'Dragonfly::Middleware'
end end
end end

View File

@ -4,14 +4,14 @@ Locomotive::Application.configure do
# In the development environment your application's code is reloaded on # In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development # every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes. # since you don't have to restart the webserver when you make code changes.
config.cache_classes = true # false config.cache_classes = false
# Log error messages when you accidentally call methods on nil. # Log error messages when you accidentally call methods on nil.
config.whiny_nils = true config.whiny_nils = true
# Show full error reports and disable caching # Show full error reports and disable caching
config.consider_all_requests_local = true config.consider_all_requests_local = true
config.action_controller.perform_caching = true #false config.action_controller.perform_caching = false
# Don't care if the mailer can't send # Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false config.action_mailer.raise_delivery_errors = false

View File

@ -259,8 +259,12 @@ en:
site: Site information site: Site information
content_types: Custom content types content_types: Custom content types
assets: Theme files assets: Theme files
asset_collections: Asset collections
snippets: Snippets snippets: Snippets
pages: Pages pages: Pages
messages:
success: "Your site was successfully updated."
failure: "The import did not work."
formtastic: formtastic:
titles: titles:

View File

@ -258,8 +258,12 @@ fr:
site: Informations sur le site site: Informations sur le site
content_types: Modèles de données personnalisés content_types: Modèles de données personnalisés
assets: Fichiers du thème assets: Fichiers du thème
asset_collections: Collections de média
snippets: Snippets snippets: Snippets
pages: Pages pages: Pages
messages:
success: "Votre site a été mis à jour avec succès."
failure: "L'import n'a pas fonctionné."
formtastic: formtastic:
titles: titles:

View File

@ -111,5 +111,5 @@ en:
imports: imports:
create: create:
notice: "Your site is being updated" notice: "Your site is being updated."
alert: "Import was not done" alert: "The import was not done."

View File

@ -40,8 +40,8 @@ x refactor ui for the theme assets page
x fix assets liquid tags / filters x fix assets liquid tags / filters
x upload and insert new images in a css or js from the ui is broken x upload and insert new images in a css or js from the ui is broken
x proxy for fonts (http://markevans.github.com/dragonfly/file.Rails3.html) x proxy for fonts (http://markevans.github.com/dragonfly/file.Rails3.html)
- fix tests
- order yaml file (http://www.ruby-forum.com/topic/120295) - order yaml file (http://www.ruby-forum.com/topic/120295)
- fix tests
- global regions: keyword in editable element (http://www.mongodb.org/display/DOCS/Updating) - global regions: keyword in editable element (http://www.mongodb.org/display/DOCS/Updating)
- write my first tutorial about locomotive - write my first tutorial about locomotive

View File

@ -33,8 +33,10 @@ module Locomotive
end end
def self.add_or_update_fields(asset_collection, fields) def self.add_or_update_fields(asset_collection, fields)
fields.each do |name, data| fields.each_with_index do |data, position|
attributes = { :_alias => name, :label => name.humanize, :kind => 'string' }.merge(data).symbolize_keys name, data = data.keys.first, data.values.first
attributes = { :_alias => name, :label => name.humanize, :kind => 'string', :position => position }.merge(data).symbolize_keys
field = asset_collection.asset_custom_fields.detect { |f| f._alias == attributes[:_alias] } field = asset_collection.asset_custom_fields.detect { |f| f._alias == attributes[:_alias] }

View File

@ -7,12 +7,8 @@ module Locomotive
whitelist = self.build_regexps_in_withlist(context[:database]['site']['assets']['whitelist']) rescue nil whitelist = self.build_regexps_in_withlist(context[:database]['site']['assets']['whitelist']) rescue nil
puts "whitelist = #{whitelist.inspect}"
self.add_theme_assets(site, theme_path, whitelist) self.add_theme_assets(site, theme_path, whitelist)
# self.add_font_assets(site, theme_path)
self.add_other_assets(site, theme_path) self.add_other_assets(site, theme_path)
end end
@ -25,46 +21,19 @@ module Locomotive
visible = self.check_against_whitelist(whitelist, asset_path.gsub(File.join(theme_path, 'public'), '')) visible = self.check_against_whitelist(whitelist, asset_path.gsub(File.join(theme_path, 'public'), ''))
folder = asset_path.gsub(File.join(theme_path, 'public'), '').gsub(File.basename(asset_path), '').gsub(/^\//, '').gsub(/\/$/, '') folder = asset_path.gsub(File.join(theme_path, 'public'), '').gsub(File.basename(asset_path), '').gsub(/^\//, '').gsub(/\/$/, '')
# folder = nil if folder.blank? || folder == kind
puts "folder = #{folder} / #{visible.inspect} / #{asset_path.gsub(File.join(theme_path, 'public'), '')} / local_path = #{File.join(folder, File.basename(asset_path))}"
# slug = File.basename(asset_path, File.extname(asset_path)).parameterize('_')
# asset = site.theme_assets.where(:content_type => kind.singularize, :folder => folder, :slug => slug).first
asset = site.theme_assets.where(:local_path => File.join(folder, File.basename(asset_path))).first asset = site.theme_assets.where(:local_path => File.join(folder, File.basename(asset_path))).first
puts "found asset ! #{!asset.nil?} / #{File.join(folder, File.basename(asset_path))} / #{kind.singularize}"
asset ||= site.theme_assets.build(:folder => folder) asset ||= site.theme_assets.build(:folder => folder)
asset.attributes = { :source => File.open(asset_path), :performing_plain_text => false, :hidden => !visible } asset.attributes = { :source => File.open(asset_path), :performing_plain_text => false, :hidden => !visible }
asset.save! asset.save!
puts "--------------------"
site.reload 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 end
end end
# def self.add_font_assets(site, theme_path)
# uploader = FontUploader.new(site)
#
# Dir[File.join(theme_path, 'public', 'fonts', '*')].each do |asset_path|
# next if File.directory?(asset_path)
# puts "font file = #{asset_path}"
# uploader.store!(File.open(asset_path))
# end
# end
def self.add_other_assets(site, theme_path) def self.add_other_assets(site, theme_path)
collection = AssetCollection.find_or_create_internal(site) collection = AssetCollection.find_or_create_internal(site)

View File

@ -39,8 +39,10 @@ module Locomotive
end end
def self.add_or_update_fields(content_type, fields) def self.add_or_update_fields(content_type, fields)
fields.each do |name, data| fields.each_with_index do |data, position|
attributes = { :_alias => name, :label => name.humanize, :kind => 'string' }.merge(data).symbolize_keys name, data = data.keys.first, data.values.first
attributes = { :_alias => name, :label => name.humanize, :kind => 'string', :position => position }.merge(data).symbolize_keys
field = content_type.content_custom_fields.detect { |f| f._alias == attributes[:_alias] } field = content_type.content_custom_fields.detect { |f| f._alias == attributes[:_alias] }

View File

@ -33,29 +33,12 @@ module Locomotive
%w(site content_types assets asset_collections snippets pages).each do |step| %w(site content_types assets asset_collections snippets pages).each do |step|
if @enabled[step] != false if @enabled[step] != false
@worker.update_attributes :step => step if @worker
# puts "@worker...#{@worker.failed_at.inspect} / #{@worker.failed?.inspect}"
"Locomotive::Import::#{step.camelize}".constantize.process(context) "Locomotive::Import::#{step.camelize}".constantize.process(context)
@worker.update_attributes :step => step if @worker
else else
puts "skipping #{step}" puts "skipping #{step}"
end end
end end
# rescue Exception => e
# context[:error] = e.message
# end
#
# context
# # 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 end
protected protected

View File

@ -1,28 +0,0 @@
module Locomotive
module Liquid
module Drops
module ThemeAssets
class Base < ::Liquid::Drop
def before_method(meth)
content_type = self.class.name.demodulize.underscore #.singularize
asset = ThemeAsset.new(:site => @context.registers[:site], :folder => content_type)
'/' + ThemeAssetUploader.new(asset).store_path(meth.gsub('__', '.'))
end
end
class Images < Base; end
class Javascripts < Base; end
class Stylesheets < Base; end
end
end
end
end

View File

@ -54,9 +54,6 @@ module Locomotive
'site' => current_site, 'site' => current_site,
'page' => @page, 'page' => @page,
'asset_collections' => Locomotive::Liquid::Drops::AssetCollections.new, 'asset_collections' => Locomotive::Liquid::Drops::AssetCollections.new,
'stylesheets' => Locomotive::Liquid::Drops::ThemeAssets::Stylesheets.new,
'javascripts' => Locomotive::Liquid::Drops::ThemeAssets::Javascripts.new,
'theme_images' => Locomotive::Liquid::Drops::ThemeAssets::Images.new,
'contents' => Locomotive::Liquid::Drops::Contents.new, 'contents' => Locomotive::Liquid::Drops::Contents.new,
'current_page' => self.params[:page] 'current_page' => self.params[:page]
} }

View File

@ -1,4 +1,3 @@
var foo = null;
var I18nLocale = null; var I18nLocale = null;
var CodeMirrorEditors = []; var CodeMirrorEditors = [];

View File

@ -5,21 +5,28 @@ $(document).ready(function() {
dataType: 'json', dataType: 'json',
minTimeout: 100 minTimeout: 100
}, function(data) { }, function(data) {
var steps = ['site', 'content_types', 'assets', 'snippets', 'pages']; var steps = ['site', 'content_types', 'assets', 'asset_collections', 'snippets', 'pages'];
var currentIndex = data.step == 'done' ? steps.length - 1 : steps.indexOf(data.step); var currentIndex = data.step == 'done' ? steps.length - 1 : steps.indexOf(data.step);
// console.log('data.step = ' + data.step + ', ' + currentIndex + ', ' + data.failed); for (var i = 0; i < steps.length; i++) {
var state = null;
for (var i = 0; i <= currentIndex; i++) { if (i <= currentIndex) state = 'done';
var state = 'done'; if (i == currentIndex + 1 && data.failed) state = 'failed';
if (i == currentIndex && data.failed) state = 'failed'; if (state != null)
$('#import-steps li:eq(' + i + ')').addClass(state);
$('#import-steps li:eq(' + i + ')').addClass(state);
} }
// if (state == 'failed' || currentIndex == steps.length) # TODO if (data.step == 'done')
$.growl('notice', $('#import-steps').attr('data-success-message'));
if (data.failed)
$.growl('alert', $('#import-steps').attr('data-failure-message'));
if (data.step == 'done' || data.failed)
$('#import-steps').smartupdaterStop();
}); });
}); });