Sha256: 606d93c79bfe2347083212dbdab44f00ccbd5c7d2654e4f3f14d7056b814fa2f

Contents?: true

Size: 1.15 KB

Versions: 43

Compression:

Stored size: 1.15 KB

Contents

module Tenon
  class ProxyAttachment
    attr_reader :attachment

    def initialize(attachment, klass, asset_name)
      @attachment = attachment.try(:attachment) || attachment
      @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.match(/^_.*/)
        @attachment.url(style.to_s.gsub(/^_(.*)/, '\1'), *args)
      else
        @attachment.url("#{@style_prefix}_#{style}", *args)
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
tenon-1.0.43 lib/tenon/proxy_attachment.rb
tenon-1.0.42 lib/tenon/proxy_attachment.rb
tenon-1.0.41 lib/tenon/proxy_attachment.rb
tenon-1.0.40 lib/tenon/proxy_attachment.rb
tenon-1.0.39 lib/tenon/proxy_attachment.rb
tenon-1.0.38 lib/tenon/proxy_attachment.rb
tenon-1.0.37 lib/tenon/proxy_attachment.rb
tenon-1.0.36 lib/tenon/proxy_attachment.rb
tenon-1.0.35 lib/tenon/proxy_attachment.rb
tenon-1.0.33 lib/tenon/proxy_attachment.rb
tenon-1.0.32 lib/tenon/proxy_attachment.rb
tenon-1.0.31 lib/tenon/proxy_attachment.rb
tenon-1.0.30 lib/tenon/proxy_attachment.rb
tenon-1.0.29 lib/tenon/proxy_attachment.rb
tenon-1.0.28 lib/tenon/proxy_attachment.rb
tenon-1.0.27 lib/tenon/proxy_attachment.rb
tenon-1.0.26 lib/tenon/proxy_attachment.rb
tenon-1.0.25 lib/tenon/proxy_attachment.rb
tenon-1.0.24 lib/tenon/proxy_attachment.rb
tenon-1.0.23 lib/tenon/proxy_attachment.rb