Sha256: c9644a339485b8272323a20a887dc697869ec4e77d00835c28b989ae32f66022

Contents?: true

Size: 1.22 KB

Versions: 13

Compression:

Stored size: 1.22 KB

Contents

module Tenon
  class ProxyAttachment
    attr_reader :attachment, :asset

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tenon-1.0.56 lib/tenon/proxy_attachment.rb
tenon-1.0.55 lib/tenon/proxy_attachment.rb
tenon-1.0.54 lib/tenon/proxy_attachment.rb
tenon-1.0.53 lib/tenon/proxy_attachment.rb
tenon-1.0.52 lib/tenon/proxy_attachment.rb
tenon-1.0.51 lib/tenon/proxy_attachment.rb
tenon-1.0.50 lib/tenon/proxy_attachment.rb
tenon-1.0.49 lib/tenon/proxy_attachment.rb
tenon-1.0.48 lib/tenon/proxy_attachment.rb
tenon-1.0.47 lib/tenon/proxy_attachment.rb
tenon-1.0.46 lib/tenon/proxy_attachment.rb
tenon-1.0.45 lib/tenon/proxy_attachment.rb
tenon-1.0.44 lib/tenon/proxy_attachment.rb