external_asset_pipeline 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0c608c4887f1a6584bec569e7ea1bd31c1baf0617a1a71e2a33d5340a9a233d
4
+ data.tar.gz: 8acdec2d61b41b47644b1c66dbc99705fd615adeb03428b7d852f905e33b8f6c
5
+ SHA512:
6
+ metadata.gz: ca18e90fc4093746261daa6a02d778658d266f3ea2140f3096292f06753de7f7030548630ba38f011481a834c5849721082bacab4027ad66e490b237af4ed5da
7
+ data.tar.gz: 6e8f2be51af359e08821858e03c2004277ae5a1fcd00ba9c62ad6c7f4e0b368f74422e36e556a503e260868910a402791ab3da112f392ce39b707c7e393da95b
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExternalAssetPipeline
4
+ class Configuration
5
+ attr_accessor :assets_prefix,
6
+ :cache_manifest,
7
+ :fall_back_to_sprockets,
8
+ :manifest_filename,
9
+ :public_path
10
+
11
+ alias cache_manifest? cache_manifest
12
+ alias fall_back_to_sprockets? fall_back_to_sprockets
13
+
14
+ def initialize
15
+ @assets_prefix = '/packs'
16
+ @cache_manifest = true
17
+ @fall_back_to_sprockets = false
18
+ @manifest_filename = 'manifest.json'
19
+ end
20
+
21
+ def configure
22
+ yield self
23
+ self
24
+ end
25
+
26
+ def manifest_path
27
+ @public_path.join(public_subdirectory, @manifest_filename)
28
+ end
29
+
30
+ private
31
+
32
+ def public_subdirectory
33
+ @assets_prefix[1..-1]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'external_asset_pipeline'
4
+
5
+ module ExternalAssetPipeline
6
+ module Helper
7
+ # Overrides the built-in
8
+ # `ActionView::Helpers::AssetUrlHelper#compute_asset_path` to use the
9
+ # external asset pipeline, in the same manner that sprockets-rails does:
10
+ # https://github.com/rails/sprockets-rails/blob/v3.2.1/lib/sprockets/rails/helper.rb#L74-L96
11
+ def compute_asset_path(source, _options = {})
12
+ value_in_asset_manifest = ExternalAssetPipeline.manifest.find(source)
13
+
14
+ return value_in_asset_manifest if value_in_asset_manifest
15
+ return super if ExternalAssetPipeline.manifest.fall_back_to_sprockets?
16
+
17
+ raise AssetNotFound,
18
+ "The asset #{source.inspect} is not present in the asset manifest"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module ExternalAssetPipeline
6
+ class Manifest
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def find(name)
12
+ value = data[name.to_s]
13
+ "#{@config.assets_prefix}/#{value}" if value
14
+ end
15
+
16
+ def fall_back_to_sprockets?
17
+ @config.fall_back_to_sprockets?
18
+ end
19
+
20
+ private
21
+
22
+ def data
23
+ if @config.cache_manifest?
24
+ @data ||= load
25
+ else
26
+ load
27
+ end
28
+ end
29
+
30
+ def load
31
+ JSON.parse(@config.manifest_path.read)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ # rubocop:disable all
6
+ module ExternalAssetPipeline
7
+ class Origin
8
+ def initialize(server:, fallback:)
9
+ @server = server
10
+ @fallback = fallback
11
+ end
12
+
13
+ # USE THIS VERSION IF IT WORKS!
14
+ # Run some tests in the demo app with different asset_host procs
15
+ # _Might_ need to define this proc inside a module that includes
16
+ # ActionView::Helpers::AssetUrlHelper in order for
17
+ # compute_asset_host to be available - not sure.
18
+ def call(source, request = nil)
19
+ if @server.running?
20
+ @server.origin
21
+ else
22
+ ComputeAssetHostProxy.new(request).call(source, host: @fallback)
23
+ end
24
+ end
25
+
26
+ class ComputeAssetHostProxy
27
+ include ActionView::Helpers::AssetUrlHelper
28
+
29
+ attr_reader :request
30
+
31
+ def initialize(request:)
32
+ @request = request
33
+ end
34
+
35
+ def call(source, host:)
36
+ compute_asset_host(source, host: host)
37
+ end
38
+ end
39
+
40
+ def ugly_call(source, request)
41
+ # Ugh, this is so ugly
42
+ # the logic in the elsif branch is copied from
43
+ # https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/actionview/lib/action_view/helpers/asset_url_helper.rb#L280-L284
44
+ if server.running?
45
+ server.origin
46
+ elsif fallback.respond_to?(:call)
47
+ arity =
48
+ if fallback.respond_to?(:arity)
49
+ host.arity
50
+ else
51
+ host.method(:call).arity
52
+ end
53
+ args = [source]
54
+ args << request if request && (arity > 1 || arity < 0)
55
+ fallback.call(*args)
56
+ else
57
+ fallback
58
+ end
59
+ end
60
+ end
61
+ end
62
+ # rubocop:enable all
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ require 'external_asset_pipeline'
6
+ require 'external_asset_pipeline/configuration'
7
+ require 'external_asset_pipeline/helper'
8
+ require 'external_asset_pipeline/manifest'
9
+
10
+ module ExternalAssetPipeline
11
+ class Railtie < ::Rails::Railtie
12
+ config.before_configuration do
13
+ config.external_asset_pipeline = Configuration.new.configure do |c|
14
+ c.public_path = ::Rails.root.join('public')
15
+ end
16
+ end
17
+
18
+ config.after_initialize do
19
+ ExternalAssetPipeline.manifest =
20
+ Manifest.new(config.external_asset_pipeline)
21
+
22
+ ActiveSupport.on_load(:action_view) do
23
+ include ExternalAssetPipeline::Helper
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExternalAssetPipeline
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'external_asset_pipeline/version'
4
+
5
+ module ExternalAssetPipeline
6
+ class Error < StandardError; end
7
+ class AssetNotFound < Error; end
8
+
9
+ class << self
10
+ attr_accessor :manifest
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: external_asset_pipeline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Macklin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ description: |
34
+ This gem provides a thin layer to connect an externally-managed asset
35
+ pipeline to ActionView's AssetUrlHelper and AssetTagHelper methods. This is
36
+ useful if you are comfortable leveraging other programs to produce an asset
37
+ manifest and you just want it to integrate with ActionView's helper methods
38
+ (e.g. `asset_path`, `javascript_include_tag`, etc.)
39
+ email:
40
+ - 1863540+rmacklin@users.noreply.github.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/external_asset_pipeline.rb
46
+ - lib/external_asset_pipeline/configuration.rb
47
+ - lib/external_asset_pipeline/helper.rb
48
+ - lib/external_asset_pipeline/manifest.rb
49
+ - lib/external_asset_pipeline/origin.rb
50
+ - lib/external_asset_pipeline/railtie.rb
51
+ - lib/external_asset_pipeline/version.rb
52
+ homepage: https://github.com/rails-front-end/external_asset_pipeline
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/rails-front-end/external_asset_pipeline
57
+ source_code_uri: https://github.com/rails-front-end/external_asset_pipeline/tree/v0.1.0
58
+ changelog_uri: https://github.com/rails-front-end/external_asset_pipeline/blob/v0.1.0/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.0.1
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Integrate an externally-managed asset pipeline with ActionView
78
+ test_files: []