Sha256: f47f18539e651b5dbf420dd82c354a1e680d97ca49c8c27c6bc13c29ae84a621

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 KB

Contents

module Tenon
  class ProxyAttachment
    attr_reader :attachment, :asset, :style_prefix

    def initialize(attachment, klass, asset_name)
      @attachment = attachment.try(:attachment) || attachment
      @asset = attachment if attachment.is_a?(Tenon::Asset)
      @style_prefix = "#{klass.to_s.underscore.gsub('/', '_')}_#{asset_name}"
    end

    def original_filename
      @attachment.original_filename
    end

    def display_name
      Tenon::AssetDecorator.new(@attachment.instance).display_name
    end

    # Prefix with an underscore to use base Asset styles
    def url(style = :original, *args)
      if style.to_sym == :original
        original_url(*args)
      else
        named_url(style, *args)
      end
    end

    private

    def original_url(*args)
      scoped_original = "#{@style_prefix}_original".to_sym
      styles = @attachment.styles.keys
      if styles.include?(scoped_original)
        @attachment.url(scoped_original, *args)
      else
        @attachment.url(:original, *args)
      end
    end

    def named_url(style, *args)
      if style.to_s.match(/^\_.*/)
        @attachment.url(style.to_s.gsub(/^\_(.*)/, '\1'), *args)
      else
        @attachment.url("#{@style_prefix}_#{style}", *args)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tenon-2.1.0 lib/tenon/proxy_attachment.rb
tenon-2.0.8 lib/tenon/proxy_attachment.rb
tenon-2.0.7 lib/tenon/proxy_attachment.rb
tenon-2.0.6 lib/tenon/proxy_attachment.rb
tenon-2.0.5 lib/tenon/proxy_attachment.rb
tenon-2.0.4 lib/tenon/proxy_attachment.rb
tenon-2.0.3 lib/tenon/proxy_attachment.rb
tenon-2.0.2 lib/tenon/proxy_attachment.rb
tenon-2.0.1 lib/tenon/proxy_attachment.rb
tenon-2.0.0 lib/tenon/proxy_attachment.rb