fix a misbehaviour about Dragonfly when ImageMagick is missing + better code coverage

did committed Feb 05, 2015
commit fcdebcd7b5562800cad787d9d17143beddb6dd6d
Showing 10 changed files with 115 additions and 45 deletions
Gemfile +1 -1
@@ @@ -15,7 +15,7 @@ group :test do
gem 'json_spec', '~> 1.1.4'
gem 'i18n-spec', '~> 0.6.0'
- gem 'pry'
+ gem 'pry-byebug'
gem 'codeclimate-test-reporter', require: false
end
Gemfile.lock +10 -1
@@ @@ -47,6 +47,10 @@ GEM
tzinfo (~> 1.1)
addressable (2.3.6)
builder (3.2.2)
+ byebug (3.5.1)
+ columnize (~> 0.8)
+ debugger-linecache (~> 1.2)
+ slop (~> 3.6)
chunky_png (1.3.3)
codeclimate-test-reporter (0.4.6)
simplecov (>= 0.7.1, < 1.0.0)
@@ @@ -56,6 +60,7 @@ GEM
execjs
coffee-script-source (1.9.0)
colorize (0.7.5)
+ columnize (0.9.0)
compass (1.0.3)
chunky_png (~> 1.2)
compass-core (~> 1.0.2)
@@ @@ -68,6 +73,7 @@ GEM
sass (>= 3.3.0, < 3.5)
compass-import-once (1.0.5)
sass (>= 3.2, < 3.5)
+ debugger-linecache (1.2.0)
diff-lcs (1.2.5)
docile (1.1.5)
dragonfly (1.0.7)
@@ @@ -118,6 +124,9 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
+ pry-byebug (2.0.0)
+ byebug (~> 3.4)
+ pry (~> 0.10)
rack (1.6.0)
rack-cache (1.2)
rack (>= 0.4)
@@ @@ -178,6 +187,6 @@ DEPENDENCIES
i18n-spec (~> 0.6.0)
json_spec (~> 1.1.4)
locomotivecms_steam!
- pry
+ pry-byebug
rake (~> 10.4.2)
rspec (~> 3.1.0)
locomotive/steam.rb b/lib/locomotive/steam.rb +8 -18
@@ @@ -1,10 +1,7 @@
- # require 'locomotive/models'
- # require 'locomotive/decorators'
require 'locomotive/common'
require_relative 'steam/core_ext'
require_relative 'steam/exceptions'
- # require_relative 'steam/decorators'
require_relative 'steam/configuration'
require_relative 'steam/liquid'
@@ @@ -25,19 +22,11 @@ require 'active_support/concern'
require 'active_support/deprecation'
require 'active_support/core_ext'
- #require 'httmultiparty'
require 'mime/types'
module Locomotive
module Steam
- # Locomotive::Steam.repositories.get(:site)
- # Locomotive::Steam.repositories.get(:theme_assets, site)
-
- # a la fin de chaque requete => on clean les repositories
-
- # Locomotive::Steam.repositories[:theme_assets](site)
-
# TEMPLATE_EXTENSIONS = %w(liquid haml)
class << self
@@ @@ -56,13 +45,14 @@ module Locomotive
yield(configuration)
end
- class << self
- def method_missing(name, *args, &block)
- Locomotive::Steam.configuration.public_send(name)
- rescue
- super
- end
- end
+ # FIXME: not sure it will be ever needed
+ # class << self
+ # def method_missing(name, *args, &block)
+ # Locomotive::Steam.configuration.public_send(name)
+ # rescue
+ # super
+ # end
+ # end
end
end
locomotive/steam/core_ext/hash.rb b/lib/locomotive/steam/core_ext/hash.rb +8 -3
@@ @@ -2,12 +2,16 @@
# https://gist.github.com/timruffles/2780508
module HashConverter
class << self
+
def to_underscore hash
convert hash, :underscore
end
- def to_camel_case hash
- convert hash, :camelize, :lower
- end
+
+ # FIXME: not sure it will be ever needed
+ # def to_camel_case hash
+ # convert hash, :camelize, :lower
+ # end
+
def convert obj, *method
case obj
when Hash
@@ @@ -22,5 +26,6 @@ module HashConverter
obj
end
end
+
end
end
locomotive/steam/initializers/dragonfly.rb b/lib/locomotive/steam/initializers/dragonfly.rb +46 -17
@@ @@ -1,27 +1,56 @@
- begin
- require 'dragonfly'
+ module Locomotive
+ module Steam
+ module Initializers
- Dragonfly.app(:steam).configure do
- plugin :imagemagick,
- convert_command: `which convert`.strip.presence || '/usr/local/bin/convert',
- identify_command: `which identify`.strip.presence || '/usr/local/bin/identify'
+ class Dragonfly
- verify_urls true
+ def run
+ require 'dragonfly'
- secret Locomotive::Steam.configuration.image_resizer_secret
+ # need to be called outside of the configure method
+ imagemagick_commands = find_imagemagick_commands
- url_format '/images/dynamic/:job/:basename.:ext'
+ ::Dragonfly.app(:steam).configure do
+ if imagemagick_commands
+ plugin :imagemagick, imagemagick_commands
+ end
- fetch_file_whitelist /public/
+ verify_urls true
- fetch_url_whitelist /.+/
- end
+ secret Locomotive::Steam.configuration.image_resizer_secret
+
+ url_format '/images/dynamic/:job/:basename.:ext'
+
+ fetch_file_whitelist /public/
+
+ fetch_url_whitelist /.+/
+ end
+
+ ::Dragonfly.logger = Locomotive::Common::Logger.instance
+ end
- Dragonfly.logger = Locomotive::Common::Logger.instance
+ def find_imagemagick_commands
+ convert = `which convert`.strip.presence || '/usr/local/bin/convert'
+ identify = `which identify`.strip.presence || '/usr/local/bin/identify'
- rescue Exception => e
- Locomotive::Common::Logger.warn %{
+ if File.exists?(convert)
+ { convert_command: convert, identify_command: identify }
+ else
+ missing_image_magick
+ nil
+ end
+ end
+
+ def missing_image_magick
+ Locomotive::Common::Logger.warn <<-EOF
[Dragonfly] !disabled!
- [Dragonfly] If you want to take full benefits of all the features in the LocomotiveWagon, we recommend you to install ImageMagick and RMagick. Check out the documentation here: http://doc.locomotivecms.com/editor/installation.
- }
+ [Dragonfly] If you want to take full benefits of all the features in Locomotive Steam, we recommend you to install ImageMagick. Check out the documentation here: http://doc.locomotivecms.com.
+ EOF
+ end
+
+ end
+ end
+ end
end
+
+ Locomotive::Steam::Initializers::Dragonfly.new.run
locomotive/steam/services/image_resizer.rb b/lib/locomotive/steam/services/image_resizer.rb +1 -1
@@ @@ -18,7 +18,7 @@ module Locomotive
end
def disabled?
- resizer.nil?
+ resizer.nil? || resizer.plugins[:imagemagick].nil?
end
protected
spec/support.rb +1 -0
@@ @@ -3,3 +3,4 @@ require_relative 'support/liquid'
require_relative 'support/matchers/hash'
require_relative 'support/examples/matching_locale'
require_relative 'support/examples/locale_file'
+ require_relative 'support/pry'
spec/support/pry.rb +4 -4
@@ @@ -1,4 +1,4 @@
- # begin
- # require 'pry'
- # rescue LoadError
- # end
+ begin
+ require 'pry'
+ rescue LoadError
+ end
spec/unit/initializers/dragonfly_spec.rb +31 -0
@@ @@ -0,0 +1,31 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Initializers::Dragonfly do
+
+ let(:initializer) { Locomotive::Steam::Initializers::Dragonfly.new }
+
+ subject { ::Dragonfly.app(:steam).plugins[:imagemagick] }
+
+ describe 'with ImagickMagick' do
+
+ before { initializer.run }
+ it { is_expected.not_to eq nil }
+
+ end
+
+ describe 'missing ImagickMagick' do
+
+ before do
+ ::Dragonfly::App.destroy_apps
+ expect(File).to receive(:exists?).and_return(false)
+ initializer.run
+ end
+ it { is_expected.to eq nil }
+
+ after(:all) do
+ Locomotive::Steam::Initializers::Dragonfly.new.run
+ end
+
+ end
+
+ end
spec/unit/services/image_resizer_spec.rb +5 -0
@@ @@ -21,6 +21,11 @@ describe Locomotive::Steam::Services::ImageResizer do
let(:resizer) { Dragonfly.app(:steam) }
+ describe 'no imagemagick' do
+ before { expect(resizer.plugins).to receive(:[]).with(:imagemagick).and_return(nil) }
+ it { is_expected.to eq nil }
+ end
+
describe 'no geometry' do
let(:geometry) { '' }
it { is_expected.to eq nil }