Sha256: f14ca6d0f75321bfc1ac6e9cc5d304fce29b3de3fb7d21759a81f14abf61d594

Contents?: true

Size: 1.8 KB

Versions: 25

Compression:

Stored size: 1.8 KB

Contents

module Spotlight
  ###
  # 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 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 do|_|
          version :thumb do
            process resize_to_fit: [400, 400]
          end
        end
      },
      {
        version: :square,
        field: Spotlight::Engine.config.try(:square_image_field),
        lambda: lambda do|_|
          version :square do
            process resize_to_fill: [100, 100]
          end
        end
      }
    ].reject { |v| v[:field].blank? }
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
blacklight-spotlight-0.17.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.16.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.15.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.14.2 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.14.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.14.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.13.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.12.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.12.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.11.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.10.3 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.10.2 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.10.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.10.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.9.2 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.9.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.9.0 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.8.2 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.8.1 app/models/concerns/spotlight/image_derivatives.rb
blacklight-spotlight-0.8.0 app/models/concerns/spotlight/image_derivatives.rb