Sha256: 2e9809fe4a31545242aa11ad43d6d539f45ac22545b591f9a7855d3ec3e709fc

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

###
 # Include Spotlight::ImageDerivatives in a class or module to get
 # the derivative configurations in #spotlight_image_derivatives.
 # A new derivative could theoretically be added by putting the
 # following in an initializer.
 # Spotlight::ImageDerivatives.spotlight_image_derivatives << {
 #   version: :my_version,
 #   field: :my_field,
 #   lambda: lambda {|_|
 #     version :my_version do
 #       process :resize_to_fill => [30,30]
 #     end
 #   }
 # }
 #
 # This will then create that new CarrierWave version in any class that extends this module
 # and calls the apply_spotlight_image_derivative_versions class method described below.
###
module Spotlight
  module ImageDerivatives
    mattr_accessor :spotlight_image_derivatives

    # Extend Spotlight::ImageDerivatives in a CarrierWave uploader
    # then you can call this as a class method and all of the
    # configured versions will be available
    def apply_spotlight_image_derivative_versions
      spotlight_image_derivatives.each do |version_config|
        if(c = version_config[:lambda]).present?
          class_eval(&c)
        end
      end
    end

    # Set default derivative configurations
    self.spotlight_image_derivatives ||= [
      {
        field: Spotlight::Engine.config.try(:full_image_field)
      },
      {
        version: :thumb,
        field: Spotlight::Engine.config.try(:thumbnail_field),
        lambda: lambda {|_|
          version :thumb do
            process :resize_to_fit => [400,400]
          end
        }
      },
      {
        version: :square,
        field: Spotlight::Engine.config.try(:square_image_field),
        lambda: lambda {|_|
          version :square do
            process :resize_to_fill => [100,100]
          end
        }
      }
    ].reject{|v| v[:field].blank? }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blacklight-spotlight-0.4.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.3.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.3.0 app/models/concerns/spotlight/image_derivatives.rb