Sha256: 258e1df4c73e08e9ded2e833e682c36f5cf831ed4877d903234eaab19215e744

Contents?: true

Size: 1.7 KB

Versions: 22

Compression:

Stored size: 1.7 KB

Contents

require 'uri'

module Paperclip
  class UrlGenerator
    def initialize(attachment, attachment_options)
      @attachment = attachment
      @attachment_options = attachment_options
    end

    def for(style_name, options)
      timestamp_as_needed(
        escape_url_as_needed(
          @attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
          options
      ), options)
    end

    private

    # This method is all over the place.
    def default_url
      if @attachment_options[:default_url].respond_to?(:call)
        @attachment_options[:default_url].call(@attachment)
      elsif @attachment_options[:default_url].is_a?(Symbol)
        @attachment.instance.send(@attachment_options[:default_url])
      else
        @attachment_options[:default_url]
      end
    end

    def most_appropriate_url
      if @attachment.original_filename.nil?
        default_url
      else
        @attachment_options[:url]
      end
    end

    def timestamp_as_needed(url, options)
      if options[:timestamp] && timestamp_possible?
        delimiter_char = url.match(/\?.+=/) ? '&' : '?'
        "#{url}#{delimiter_char}#{@attachment.updated_at.to_s}"
      else
        url
      end
    end

    def timestamp_possible?
      @attachment.respond_to?(:updated_at) && @attachment.updated_at.present?
    end

    def escape_url_as_needed(url, options)
      if options[:escape]
        escape_url(url)
      else
        url
      end
    end

    def escape_url(url)
      if url.respond_to?(:escape)
        url.escape
      else
        URI.escape(url).gsub(escape_regex){|m| "%#{m.ord.to_s(16).upcase}" }
      end
    end

    def escape_regex
      /[\?\(\)\[\]\+]/
    end
  end
end

Version data entries

22 entries across 20 versions & 4 rubygems

Version Path
paperclip-5.0.0 lib/paperclip/url_generator.rb
paperclip-4.3.7 lib/paperclip/url_generator.rb
paperclip_jk-5.0.0.beta2 lib/paperclip/url_generator.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/lib/paperclip/url_generator.rb
paperclip-5.0.0.beta2 lib/paperclip/url_generator.rb
paperclip-5.0.0.beta1 lib/paperclip/url_generator.rb
paperclip-4.3.6 lib/paperclip/url_generator.rb
paperclip-4.3.5 lib/paperclip/url_generator.rb
paperclip-4.3.4 lib/paperclip/url_generator.rb
paperclip-4.3.3 lib/paperclip/url_generator.rb
paperclip-4.3.2 lib/paperclip/url_generator.rb
paperclip-4.3.1 lib/paperclip/url_generator.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/url_generator.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/url_generator.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.2/lib/paperclip/url_generator.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.3/lib/paperclip/url_generator.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/url_generator.rb
paperclip-4.3.0 lib/paperclip/url_generator.rb
paperclip-4.2.4 lib/paperclip/url_generator.rb
paperclip-4.2.3 lib/paperclip/url_generator.rb