Module: CdnUrl

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/cdn_url.rb

Overview

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 original.amazon.com/account_report/id/report.xlsx

will be transposed to: 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.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This method is abstract.

Catch methods started with ‘cdn_` and respond to those requests if there is a

matching method ending in ‘_url`



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/app/models/concerns/cdn_url.rb', line 23

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

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/app/models/concerns/cdn_url.rb', line 51

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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/app/models/concerns/cdn_url.rb', line 47

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