in the FS adapter, slugs of content entries were erased and replaced by ones from the _label field if localized + order_by in a relationship can be either a string OR an array if read from Engine + better error messages if compressing and minifying js/css fails

did committed Apr 19, 2016
commit fe7403ce999a62700bc512b1c0be52ca3658bc53
Showing 4 changed files with 78 additions and 10 deletions
locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/content_entry.rb +8 -6
@@ @@ -70,13 +70,15 @@ module Locomotive::Steam
end
def set_slug(entity, dataset)
- if entity._label.respond_to?(:translations) # localized?
- entity._label.each do |locale, label|
- entity[:_slug][locale] ||= slugify(entity._id, label, dataset, locale)
+ if entity._slug.blank?
+ if entity._label.respond_to?(:translations)
+ entity._label.each do |locale, label|
+ entity[:_slug][locale] = slugify(entity._id, label, dataset, locale)
+ end
+ else
+ # same value for any locale
+ entity[:_slug].translations = slugify(entity._id, entity._label, dataset)
end
- elsif entity[:_slug] && entity[:_slug][:anylocale].nil?
- # Note: replace the translations of the I18nField by a string
- entity[:_slug].translations = slugify(entity._id, entity._label, dataset)
end
end
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb +2 -1
@@ @@ -26,7 +26,8 @@ module Locomotive::Steam
def order_by
if (order_by = self[:order_by]).present?
- name, direction = order_by.split
+ # from Filesystem -> string, from MongoDB -> array (string transformed by Engine)
+ name, direction = order_by.respond_to?(:each) ? order_by : order_by.split
{ name.to_sym => direction || 'asc' }
else
type == :has_many ? { :"position_in_#{self[:inverse_of]}" => 'asc' } : nil
locomotive/steam/initializers/sprockets.rb b/lib/locomotive/steam/initializers/sprockets.rb +63 -2
@@ @@ -3,9 +3,66 @@ require 'sass'
require 'coffee_script'
require 'compass'
require 'autoprefixer-rails'
+ require 'open3'
module Locomotive::Steam
+ class YUICompressorRuntimeError < RuntimeError
+ attr_reader :errors
+ #:nocov:
+ def initialize(msg, errors)
+ super(msg)
+ @errors = errors
+ end
+ end
+
+ module YUICompressorErrors
+
+ #:nocov:
+ def compress(stream_or_string)
+ streamify(stream_or_string) do |stream|
+ tempfile = new_tempfile(stream)
+ full_command = "%s %s" % [command, tempfile.path]
+
+ output, errors, exit_status = _compress(full_command, tempfile)
+
+ if exit_status.exitstatus.zero?
+ output
+ else
+ # Bourne shells tend to blow up here when the command fails, usually
+ # because java is missing
+ raise YUICompressorRuntimeError.new("Command '%s' returned non-zero exit status" %
+ full_command, errors)
+ end
+ end
+ end
+
+ #:nocov:
+ def _compress(command, tempfile)
+ begin
+ # FIXME: catch only useful information from the stderr output
+ # output, errors, exit_status = '', [], nil
+ output, errors, exit_status = Open3.capture3(command)
+ errors = errors.split("\n").find_all { |l| l =~ /\s+[0-9]/ }
+ [output, errors, exit_status]
+ rescue Exception => e
+ # windows shells tend to blow up here when the command fails
+ raise RuntimeError, "compression failed: %s" % e.message
+ ensure
+ tempfile.close!
+ end
+ end
+
+ #:nocov:
+ def new_tempfile(stream)
+ Tempfile.new('yui_compress').tap do |tempfile|
+ tempfile.write stream.read
+ tempfile.flush
+ end
+ end
+
+ end
+
class SprocketsEnvironment < ::Sprockets::Environment
def initialize(root, options = {})
@@ @@ -33,9 +90,13 @@ module Locomotive::Steam
def install_yui_compressor(options)
return unless options[:minify]
- require 'yui/compressor'
-
if is_java_installed?
+ require 'yui/compressor'
+
+ [YUI::JavaScriptCompressor, YUI::CssCompressor].each do |klass|
+ klass.send(:include, YUICompressorErrors)
+ end
+
# minify javascripts and stylesheets
self.js_compressor = YUI::JavaScriptCompressor.new
self.css_compressor = YUI::CssCompressor.new
locomotive/steam/models/i18n_field.rb b/lib/locomotive/steam/models/i18n_field.rb +5 -1
@@ @@ -5,7 +5,7 @@ module Locomotive::Steam
extend Forwardable
- def_delegators :@translations, :values, :blank?, :default
+ def_delegators :@translations, :values, :default
attr_reader :name, :translations
@@ @@ -39,6 +39,10 @@ module Locomotive::Steam
@translations.each(&block)
end
+ def blank?
+ @translations.blank? && @translations[:anything].blank?
+ end
+
def apply(&block)
if default
@translations = Hash.new(yield(default))