Module: DmCore::UrlHelper

Defined in:
app/helpers/dm_core/url_helper.rb

Constant Summary collapse

@@asset_timestamps_cache =

Following code pulled from the Rails 3.0 source.

action_pack/lib/action_view/helpers/asset_tag_helper.rb

Generates an asset id to be used for any assets we don’t want in the asset pipeline. Asset id gets appeneded as a query string to url.

Use case: all themes are precompiled, but a single stylesheet per theme lives outside the pipeline, so that it can be updated without having to recompile all assets of all sites/themes.

Simplify the task: create the asset_id and append to the url - don’t currently take into account asset hosts, etc.


{}
@@asset_timestamps_cache_guard =
Mutex.new
@@cache_asset_timestamps =
true

Instance Method Summary collapse

Instance Method Details

#expand_url(url, path) ⇒ Object

if a relative url path is given, then expand it by prepending the supplied path.




67
68
69
# File 'app/helpers/dm_core/url_helper.rb', line 67

def expand_url(url, path)
  url.expand_url(path)
end

#file_url(file_name, options = {}) ⇒ Object

Given a file name (relative or absolute), generate a full url path (usually will not include the protocol)




19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/dm_core/url_helper.rb', line 19

def file_url(file_name, options = {})
  options.reverse_merge!  default_folder: 'media', account_site_assets: 
  if file_name.blank?
    ''
  elsif file_name.start_with?('s3://', 's3s://')
    # amazon S3 url - generate a signed expiring link
    s3_generate_expiring_link(file_name)
  elsif file_name.absolute_url?
    # it's absolute, nothing to do
    file_name
  elsif options[:protected]
    # a protected asset, append our protected asset name (which will trigger a
    # special route to handle the file)
    file_name.expand_url("/protected_asset/")
  else
    # append our site's asset folder and default folder
    folder = options[:default_folder].blank? ? '' : "#{options[:default_folder]}/"
    file_name.expand_url("#{options[:account_site_assets]}/#{folder}")
  end
end

#rails_asset_id(source) ⇒ Object

Use the RAILS_ASSET_ID environment variable or the source’s modification time as its cache-busting asset id.




148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/dm_core/url_helper.rb', line 148

def rails_asset_id(source)
  if asset_id = ENV["RAILS_ASSET_ID"]
    asset_id
  else
    if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
      asset_id
    else
      path = File.join(Rails.root, 'public', source)
      asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''

      if @@cache_asset_timestamps
        @@asset_timestamps_cache_guard.synchronize do
          @@asset_timestamps_cache[source] = asset_id
        end
      end

      asset_id
    end
  end
end

#rewrite_asset_path(source, path = nil) ⇒ Object

Break out the asset path rewrite in case plugins wish to put the asset id someplace other than the query string.




172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/helpers/dm_core/url_helper.rb', line 172

def rewrite_asset_path(source, path = nil)
  if path && path.respond_to?(:call)
    return path.call(source)
  elsif path && path.is_a?(String)
    return path % [source]
  end

  asset_id = rails_asset_id(source)
  if asset_id.blank?
    source
  else
    source + "?#{asset_id}"
  end
end

Generate an AWS S3 expiring link, using a special formatted url

s3://bucket_name/object_name?expires=120     => non-SSL, expires in 120 minutes
s3s://bucket_name/object_name?expires=20     => SSL, expires in 20 minutes
s3s://bucket_name/object_name?expires=public => links directly to file (it must
   be a World readable file, and the link will never expire)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/dm_core/url_helper.rb', line 46

def s3_generate_expiring_link(url)
  access_key  = Account.current.theme_data['AWS_ACCESS_KEY_ID']
  secret_key  = Account.current.theme_data['AWS_SECRET_ACCESS_KEY']
  uri         = URI.parse(url)
  secure      = (uri.scheme == 's3s')
  bucket      = uri.host
  object_name = uri.path.gsub(/^\//, '')
  expire_mins = (uri.query.blank? ? nil : CGI::parse(uri.query)['expires'][0]) || '10'
  
  if expire_mins == "public"
    "#{secure ? 'https' : 'http'}://#{bucket}.s3.amazonaws.com/#{object_name}"
  else
    s3 = ::AWS::S3.new(access_key_id: access_key, secret_access_key: secret_key)
    object = s3.buckets[bucket].objects[object_name]
    object.url_for(:get, {expires: expire_mins.to_i.minutes.from_now, secure: secure}).to_s
  end
end

#site_asset_media_path(src) ⇒ Object

Returns a path to a site assets, relative to the site_assets folder Supports both relative paths and explicit url




116
117
118
# File 'app/helpers/dm_core/url_helper.rb', line 116

def site_asset_media_path(src)
  rewrite_asset_path(src.expand_url("#{}/"))
end

#site_asset_media_url(src) ⇒ Object

Returns a path to a site assets, relative to the site_assets folder Supports both relative paths and explicit url




123
124
125
# File 'app/helpers/dm_core/url_helper.rb', line 123

def site_asset_media_url(src)
  rewrite_asset_path(src.expand_url("#{}/"))
end

#site_asset_path(src) ⇒ Object

Returns a path to a site assets, relative to the site_assets folder Supports both relative paths and explicit url




102
103
104
# File 'app/helpers/dm_core/url_helper.rb', line 102

def site_asset_path(src)
  rewrite_asset_path(src.expand_url("#{}/"))
end

#site_asset_url(src) ⇒ Object

Returns a path to a site assets, relative to the site_assets folder Supports both relative paths and explicit url




109
110
111
# File 'app/helpers/dm_core/url_helper.rb', line 109

def site_asset_url(src)
  rewrite_asset_path(src.expand_url("#{}/"))
end

#site_image_path(src) ⇒ Object

Returns a path to a site image, relative to the site_assets folder Supports both relative paths and explicit url




88
89
90
# File 'app/helpers/dm_core/url_helper.rb', line 88

def site_image_path(src)
  rewrite_asset_path(src.expand_url("#{}/images/"))
end

#site_image_tag(src, options = {}) ⇒ Object

Returns an image tag, where the src defaults to the site_assets image folder Supports both relative paths and explicit url




81
82
83
# File 'app/helpers/dm_core/url_helper.rb', line 81

def site_image_tag(src, options = {})
  image_tag(site_image_path(src),  options)
end

#site_image_url(src) ⇒ Object

Returns a url to a site image, relative to the site_assets folder Supports both relative paths and explicit url




95
96
97
# File 'app/helpers/dm_core/url_helper.rb', line 95

def site_image_url(src)
  rewrite_asset_path(src.expand_url("#{}/images/"))
end

#site_media_image_tag(src, options = {}) ⇒ Object

Returns an image tag, where the src defaults to the site_assets image folder Supports both relative paths and explicit url




74
75
76
# File 'app/helpers/dm_core/url_helper.rb', line 74

def site_media_image_tag(src, options = {})
  image_tag(site_asset_media_path(src),  options)
end

#url_translate(locale) ⇒ Object

Takes the full url path and converts to another locale




6
7
8
# File 'app/helpers/dm_core/url_helper.rb', line 6

def url_translate(locale)
  DmCore::Language.translate_url(request.url, locale)
end