js-asset_paths 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4a990c2ac2c78bcbe04bca4bc04a5056becc18c
4
- data.tar.gz: 621df02066f1f8026a3be378851c8cfe0386808f
3
+ metadata.gz: 434846a6d6beec40ebd69ae9c0b8f7fa4299a2ee
4
+ data.tar.gz: ade5dc4f6bf8d7bf15e3b966c56d61cd2bbcab8f
5
5
  SHA512:
6
- metadata.gz: 1c7ec67f7dd7f49b88a641c34ca3e8e6563b2d7dc45f163a31eb33f4a31f4d4b9dc50b2f764a731f786b8378c08ee6ed4d8b7770c46eda46b5f39020ddce70a3
7
- data.tar.gz: 9b528a5a22029bdcc9d5b3439ba6356bad38656faadd732e2814f74a4d11669354f0fdb7229c0944fbc58b3ff05209f1d2e0f5e2bfbd74fb6dbe0a2792a99526
6
+ metadata.gz: c93f6aabf1d1290b54cf506b4bd8916d46b760e31ba9b0048fd623c524de152b54a63b3ac0f790b19312be0569d53879a5cf2dcbdad670894a61b8105b32e4c8
7
+ data.tar.gz: 4297496d9088efb1e6fd44a126aa284d98ca9d25ed77e4ab3498453f243b4b2de377a5776fd93f5e0c9626ec1016c04a6694073e4defcf6a1d84c5e4cd7fc7e5
@@ -0,0 +1,14 @@
1
+ require 'action_dispatch'
2
+ require 'rails/engine'
3
+
4
+ module JsAssetPaths
5
+ class Engine < Rails::Engine
6
+ JS_ASSET_PATHS_FILE = 'js-asset_paths'
7
+
8
+ isolate_namespace(JsAssetPaths)
9
+
10
+ initializer('js-assets_paths.compile', after: 'sprockets.environment') do |application|
11
+ JsAssetPaths::Generator.environment = application
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,52 @@
1
+ module JsAssetPaths
2
+ class Generator
3
+ cattr_accessor :environment
4
+
5
+ def self.generate!
6
+ asset_hash.to_json
7
+ end
8
+
9
+ private
10
+
11
+ # Note: In sprockets 2, `each_file` yields a `Pathname`. In 3,
12
+ # it yields a string. This method supports both.
13
+ def self.asset_hash
14
+ assets.each_file.each_with_object({}) do |filepath, memo|
15
+ path = ::Pathname.new(filepath.to_s)
16
+ relative_path = path.relative_path_from(base_asset_path)
17
+
18
+ memo[relative_path] = output_path(path) if local?(relative_path)
19
+ end
20
+ end
21
+
22
+ def self.base_asset_path
23
+ @base_asset_path ||= Rails.root.join('app', 'assets')
24
+ end
25
+
26
+ def self.output_path(filepath)
27
+ unless digest?.in?([true, false])
28
+ raise TypeError.new('Expected ::Rails.application.config.assets.digest to be boolean')
29
+ end
30
+
31
+ if digest?
32
+ # Convert the byte string into hexadecimal
33
+ dgst = assets.file_digest(filepath).unpack('H*')[0]
34
+ "#{filepath.basename(filepath.extname)}-#{dgst}#{filepath.extname}"
35
+ else
36
+ filepath.basename.to_s
37
+ end
38
+ end
39
+
40
+ def self.assets
41
+ environment.assets
42
+ end
43
+
44
+ def self.digest?
45
+ ::Rails.application.config.assets.digest
46
+ end
47
+
48
+ def self.local?(filepath)
49
+ !(filepath.to_s =~ /\.\.\//)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module JsAssetPaths
2
+ VERSION = '0.4.2'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-asset_paths
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sonny Michaud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-20 00:00:00.000000000 Z
11
+ date: 2017-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -137,11 +137,9 @@ extra_rdoc_files: []
137
137
  files:
138
138
  - lib/js-asset_paths.rb
139
139
  - lib/js_asset_paths.rb
140
- - test/unit/digest_values_test.rb
141
- - test/unit/engine_test.rb
142
- - test/unit/generator_test.rb
143
- - test/unit/js_asset_paths_test.rb
144
- - test/unit/output_test.rb
140
+ - lib/js_asset_paths/engine.rb
141
+ - lib/js_asset_paths/generator.rb
142
+ - lib/js_asset_paths/version.rb
145
143
  homepage: https://github.com/sonnym/js-asset_paths
146
144
  licenses:
147
145
  - MIT
@@ -166,9 +164,4 @@ rubygems_version: 2.6.11
166
164
  signing_key:
167
165
  specification_version: 4
168
166
  summary: Access paths to compiled assets from in javascript.
169
- test_files:
170
- - test/unit/engine_test.rb
171
- - test/unit/generator_test.rb
172
- - test/unit/digest_values_test.rb
173
- - test/unit/js_asset_paths_test.rb
174
- - test/unit/output_test.rb
167
+ test_files: []
@@ -1,39 +0,0 @@
1
- require 'execjs'
2
- require_relative '../test_helper'
3
-
4
- describe 'untrue values of config.assets.digest' do
5
- before do
6
- App.initialize! unless App.initialized?
7
- end
8
-
9
- it 'output has corresponding key and value pairs when digest is false' do
10
- begin
11
- Rails.application.config.assets.digest = false
12
-
13
- JsAssetPaths::Generator.environment = Rails.application
14
- @output = JsAssetPaths::Generator.generate!
15
-
16
- map = ExecJS.eval(@output)
17
-
18
- map.each do |key, value|
19
- file = key.split('/').second
20
- assert_equal file, value
21
- end
22
-
23
- ensure
24
- Rails.application.config.assets.digest = true
25
- end
26
- end
27
-
28
- it 'output has corresponding key and value pairs when digest is not boolean' do
29
- begin
30
- Rails.application.config.assets.digest = :foo
31
-
32
- JsAssetPaths::Generator.environment = Rails.application
33
-
34
- assert_raises(TypeError) { JsAssetPaths::Generator.generate! }
35
- ensure
36
- Rails.application.config.assets.digest = true
37
- end
38
- end
39
- end
@@ -1,7 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- describe JsAssetPaths::Engine do
4
- it 'permit initializing the application' do
5
- App.initialize! unless App.initialized?
6
- end
7
- end
@@ -1,40 +0,0 @@
1
- require 'execjs'
2
- require_relative '../test_helper'
3
-
4
- describe JsAssetPaths::Generator do
5
- before do
6
- App.initialize! unless App.initialized?
7
-
8
- JsAssetPaths::Generator.environment = Rails.application
9
- @output = JsAssetPaths::Generator.generate!
10
- end
11
-
12
- it 'is idempotent' do
13
- assert_equal @output, JsAssetPaths::Generator.generate!
14
- end
15
-
16
- it 'output is a string' do
17
- assert_kind_of String, @output
18
- end
19
-
20
- it 'output evaluates as valid javascript' do
21
- ExecJS.eval(@output)
22
- end
23
-
24
- it 'output evaluates to a hash' do
25
- assert_kind_of Hash, ExecJS.eval(@output)
26
- end
27
-
28
- it 'output has corresponding key and value pairs when digest' do
29
- assert App.config.assets.digest
30
-
31
- map = ExecJS.eval(@output)
32
-
33
- map.each do |key, value|
34
- file = key.split('/').second
35
- name, _, ext = file.rpartition('.')
36
-
37
- assert_match (/#{name}-[0-9,a-z]{64}.#{ext}/), value
38
- end
39
- end
40
- end
@@ -1,8 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- describe JsAssetPaths do
4
- it 'ASSET_METHOD constant has the correct values' do
5
- assert_equal %i(audio_path font_path image_path javascript_path stylesheet_path video_path),
6
- JsAssetPaths::ASSET_METHODS.sort
7
- end
8
- end
@@ -1,45 +0,0 @@
1
- require 'execjs'
2
- require_relative '../test_helper'
3
-
4
- describe 'generated javascript' do
5
- before do
6
- App.initialize! unless App.initialized?
7
- JsAssetPaths::Generator.environment = Rails.application
8
-
9
- js_file_contents = ERB.new(File.read('app/assets/javascripts/js-asset_paths.js.erb')).result(binding)
10
- @js_context = ExecJS.compile(js_file_contents)
11
- end
12
-
13
- it 'PathHelper is an object' do
14
- assert_equal 'object', @js_context.eval('typeof PathHelper')
15
- end
16
-
17
- it 'PathHelper.assetPath is a function' do
18
- assert_equal 'function', @js_context.eval('typeof PathHelper.assetPath')
19
- end
20
-
21
- it 'PathHelper.fontPath is a function' do
22
- assert_equal 'function', @js_context.eval('typeof PathHelper.fontPath')
23
- end
24
-
25
- it 'PathHelper.imagePath is a function' do
26
- assert_equal 'function', @js_context.eval('typeof PathHelper.imagePath')
27
- end
28
-
29
- it 'PathHelper.javascriptPath is a function' do
30
- assert_equal 'function', @js_context.eval('typeof PathHelper.javascriptPath')
31
- end
32
-
33
- it 'PathHelper.stylesheetPath is a function' do
34
- assert_equal 'function', @js_context.eval('typeof PathHelper.stylesheetPath')
35
- end
36
-
37
- it 'PathHelper.videoPath is a function' do
38
- assert_equal 'function', @js_context.eval('typeof PathHelper.videoPath')
39
- end
40
-
41
- it 'has the correct number of public functions' do
42
- assert_equal JsAssetPaths::ASSET_METHODS.length + 1,
43
- @js_context.eval('Object.keys(PathHelper).length')
44
- end
45
- end