Sha256: a0dff69840f7ee793807a17a87594b9ef6ce2fd720d7695603729b12aa119099

Contents?: true

Size: 1.23 KB

Versions: 27

Compression:

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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
tenon-1.1.8 lib/tenon/proxy_attachment.rb
tenon-1.1.7 lib/tenon/proxy_attachment.rb
tenon-1.1.6 lib/tenon/proxy_attachment.rb
tenon-1.1.5 lib/tenon/proxy_attachment.rb
tenon-1.1.4 lib/tenon/proxy_attachment.rb
tenon-1.1.3 lib/tenon/proxy_attachment.rb
tenon-1.1.2 lib/tenon/proxy_attachment.rb
tenon-1.1.1 lib/tenon/proxy_attachment.rb
tenon-1.0.76 lib/tenon/proxy_attachment.rb
tenon-1.0.75 lib/tenon/proxy_attachment.rb
tenon-1.0.74 lib/tenon/proxy_attachment.rb
tenon-1.0.73 lib/tenon/proxy_attachment.rb
tenon-1.0.72 lib/tenon/proxy_attachment.rb
tenon-1.0.71 lib/tenon/proxy_attachment.rb
tenon-1.0.70 lib/tenon/proxy_attachment.rb
tenon-1.0.69 lib/tenon/proxy_attachment.rb
tenon-1.0.68 lib/tenon/proxy_attachment.rb
tenon-1.0.67 lib/tenon/proxy_attachment.rb
tenon-1.0.66 lib/tenon/proxy_attachment.rb
tenon-1.0.65 lib/tenon/proxy_attachment.rb