Module: Jets::AssetTagHelper

Extended by:
Memoist
Includes:
AwsServices, CommonMethods
Defined in:
lib/jets/overrides/rails/asset_tag_helper.rb

Overview

Override to prepend stage name when on AWS.

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from Jets::AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from Jets::AwsServices::GlobalMemoist

included

Methods included from CommonMethods

#add_stage_name, #on_aws?

Instance Method Details

#asset_path(source, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jets/overrides/rails/asset_tag_helper.rb', line 37

def asset_path(source, options = {})
  source = source.to_s # convert to String because passing a posts.photo object to results in failure to resolve the immage to a URL since we haven't defined polymorphic_url yet
  # mimic original behavior to get /images in source
  source = "/images/#{source}" unless source.starts_with?('/') || source.starts_with?('http')

  # Examples to help understand:
  #
  #   puts "AssetTagHelper#asset_path source #{source}"
  #   puts "AssetTagHelper#asset_path asset_folder?(source) #{asset_folder?(source).inspect}"
  #   AssetTagHelper#asset_path source /packs/images/myimage-e5f675d1ba26865fd65e919beb5bb86b.png
  #   AssetTagHelper#asset_path asset_folder?(source) "images"
  #
  if on_aws? && asset_folder?(source) && !source.starts_with?('http')
    source = "#{s3_public}#{source}"
  end

  super
end

#favicon_path(path = 'favicon.ico') ⇒ Object

Serves favicon out of s3 when on API gateway.

Useful helper for API Gateway since serving binary data like images without an Accept header doesnt work well. You can changed Media Types to ‘/’ but then that messes up form data.



61
62
63
# File 'lib/jets/overrides/rails/asset_tag_helper.rb', line 61

def favicon_path(path='favicon.ico')
  on_aws? ? "#{s3_public}/#{path}" : "/#{path}"
end

#image_tag(source, options = {}) ⇒ Object

Locally:

image_tag("jets.png") => /images/jets.png
image_tag("/images/jets.png") => /images/jets.png

Remotely:

image_tag("jets.png") => https://s3-us-west-2.amazonaws.com/demo-dev-s3bucket-1kih4n2te0n66/jets/public/images/jets.png


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jets/overrides/rails/asset_tag_helper.rb', line 25

def image_tag(source, options = {})
  source = source.to_s # convert to String because passing a posts.photo object to results in failure to resolve the immage to a URL since we haven't defined polymorphic_url yet

  # mimic original behavior to get /images in source
  source = "/images/#{source}" unless source.starts_with?('/') || source.starts_with?('http')
  if on_aws? && !source.starts_with?('http')
    source = "#{s3_public}#{source}"
  end

  super
end

#javascript_include_tag(*sources, **options) ⇒ Object



7
8
9
10
# File 'lib/jets/overrides/rails/asset_tag_helper.rb', line 7

def javascript_include_tag(*sources, **options)
  sources = sources.map { |s| s3_public_url(s, :javascripts) }
  super
end


12
13
14
15
# File 'lib/jets/overrides/rails/asset_tag_helper.rb', line 12

def stylesheet_link_tag(*sources, **options)
  sources = sources.map { |s| s3_public_url(s, :stylesheets) }
  super
end