Sha256: 4a4a0230a1f2f32c583bd81e476a5449718b80c2e242ee8df7f6b14ceabceaf7

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'nice_asset_carrierwave/version'

# NiceAssetCarrierwave
module NiceAssetCarrierwave
  # Takes an asset. Can be any model you like
  # as long as it has a Carrierwave uploader mounted
  # Default uploader name is attachment, but you can specify
  # this in the options

  def nice_asset(a, thumb = nil, options = nice_asset_options)
    return unless a
    @nice_asset_options = options
    uploader = nice_asset_uploader(a)
    return unless uploader && uploader.url
    if a.respond_to?(:is_image?) && !a.is_image?
      return uploader.url.split('/').last
    end
    nice_asset_image(a, thumb, options)
  end

  private

  def nice_asset_image(a, thumb = nil, options = {})
    options.merge!(nice_asset_options)
    image_method = nice_asset_image_method(a)
    image = thumb ? image_method.send(thumb) : image_method
    image_tag image.url, options.merge(alt: a.title ||= '')
  end

  def nice_asset_image_method(a)
    uploader = nice_asset_uploader(a)
    return uploader unless nice_asset_namespace
    uploader.send(nice_asset_namespace)
  end

  def nice_asset_namespace
    nice_asset_options[:namespace]
  end

  def nice_asset_options
    @nice_asset_options ||= {
      namespace: nil,
      uploader_name: :attachment
    }
  end

  def nice_asset_uploader(a)
    a.send(nice_asset_uploader_name)
  end

  def nice_asset_uploader_name
    nice_asset_options[:uploader_name]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nice_asset_carrierwave-0.1.1 lib/nice_asset_carrierwave.rb