Sha256: 6d5c51a051538ffa412b841c58eafd0d823bc44024ef1d6ef1204fe9389a8ea6

Contents?: true

Size: 1.64 KB

Versions: 27

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

#
# Take an existing either paperclip object or database field and return
# a fully qualified cdn url for that file.
#
# If the system configuration parameter for the url is not set, then simply return the value.
#
# If it is set, then replace the prefix of the URL defined by the boundry of class name, so account_report will be
# https://original.amazon.com/account_report/id/report.xlsx
#
# will be transposed to:
# https://cnd.apazon.com/account_report/id/report.xlsx
#
# To use this, first include CdnUrl in your class and then when you want the URL of an object call
# report.cdn_file_url instead of report.file_url.
#
module CdnUrl
  extend ActiveSupport::Concern

  # @abstract Catch methods started with `cdn_` and respond to those requests if there is a 
  # matching method ending in `_url`
  def method_missing(method, *args)
    if method.to_s.start_with? 'cdn_'
      url = if args.blank?
              send method.to_s.sub(/^cdn_/, '').to_sym
            else
              send method.to_s.sub(/^cdn_/, '').to_sym, *args
            end

      cdn_url = SystemConfiguration.cdn_url
      if [cdn_url.present?, url.present?].all?
        model_name = "#{self.class.to_s.underscore}s"
        if url.include? "/#{model_name}/"
          "#{cdn_url}/#{model_name}/#{url.split("/#{model_name}/").last}"
        else
          url
        end
      else
        url
      end
    else
      super
    end
  end

  def respond_to_missing?(method_name, include_private = false)
    super || method_name.to_s.start_with?('cdn_')
  end

  def respond_to?(method, include_private = false)
    super || method.to_s.start_with?('cdn_')
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
web47core-3.2.20 lib/app/models/concerns/cdn_url.rb
web47core-3.2.19 lib/app/models/concerns/cdn_url.rb
web47core-3.2.18 lib/app/models/concerns/cdn_url.rb
web47core-3.2.17 lib/app/models/concerns/cdn_url.rb
web47core-3.2.16 lib/app/models/concerns/cdn_url.rb
web47core-3.2.15 lib/app/models/concerns/cdn_url.rb
web47core-3.2.14 lib/app/models/concerns/cdn_url.rb
web47core-3.2.13 lib/app/models/concerns/cdn_url.rb
web47core-3.2.12 lib/app/models/concerns/cdn_url.rb
web47core-3.2.9 lib/app/models/concerns/cdn_url.rb
web47core-3.2.8 lib/app/models/concerns/cdn_url.rb
web47core-3.2.7 lib/app/models/concerns/cdn_url.rb
web47core-3.2.6 lib/app/models/concerns/cdn_url.rb
web47core-3.2.5 lib/app/models/concerns/cdn_url.rb
web47core-3.2.4 lib/app/models/concerns/cdn_url.rb
web47core-2.2.20 lib/app/models/concerns/cdn_url.rb
web47core-2.2.19 lib/app/models/concerns/cdn_url.rb
web47core-3.2.3 lib/app/models/concerns/cdn_url.rb
web47core-3.2.2 lib/app/models/concerns/cdn_url.rb
web47core-3.0.6 lib/app/models/concerns/cdn_url.rb