lib/middleman/dragonfly_thumbnailer/extension.rb in middleman-dragonfly_thumbnailer-1.0.2 vs lib/middleman/dragonfly_thumbnailer/extension.rb in middleman-dragonfly_thumbnailer-1.1.0

- old
+ new

@@ -1,15 +1,13 @@ +require 'middleman-core/logger' require 'dragonfly' module Middleman module DragonflyThumbnailer class Extension < Middleman::Extension - attr_accessor :images - def initialize(app, options_hash = {}, &block) super - @images = [] configure_dragonfly end def absolute_source_path(path) File.join(app.config[:source], app.config[:images_dir], path) @@ -24,41 +22,57 @@ def absolute_build_path(image) File.join(app.config[:build_dir], app.config[:images_dir], build_path(image)) end - def thumb(path, geometry) + def thumb(path, geometry, image_options = {}) absolute_path = absolute_source_path path return unless File.exist?(absolute_path) image = ::Dragonfly.app.fetch_file(absolute_path) image.meta['original_path'] = path image.meta['geometry'] = geometry - image = image.thumb(geometry) - images << image - image - end + image = image.thumb(geometry, image_options) - def after_build(builder) - images.each do |image| - builder.say_status :create, build_path(image) - path = absolute_build_path(image) - image.to_file(path).close + if app.build? + persist_file(image) + build_path(image) + else + image.b64_data end end + def extract_image_options(options) + image_options = { + 'format' => options.delete(:format), + 'frame' => options.delete(:frame) + }.delete_if { |_k, v| v.nil? } + + return options, image_options + end + + def persist_file(image) + path = absolute_build_path(image) + image.to_file(path).close + end + helpers do def thumb_tag(path, geometry, options = {}) - image = extensions[:dragonfly_thumbnailer].thumb(path, geometry) - return unless image + extension = extensions[:dragonfly_thumbnailer] - if environment == :development - url = image.b64_data - else - url = extensions[:dragonfly_thumbnailer].build_path(image) - end + options, image_options = extension.extract_image_options(options) + url = extension.thumb(path, geometry, image_options) - image_tag(url, options) + image_tag(url, options) if url + end + + def thumb_path(path, geometry, options = {}) + extension = extensions[:dragonfly_thumbnailer] + + _options, image_options = extension.extract_image_options(options) + url = extension.thumb(path, geometry, image_options) + + image_path(url) if url end end private