generated-assets 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4cf81eebd83b1acc69275bd1fe965a32898c536
4
- data.tar.gz: 51fdabfa4a74c4a655af7065cb6b7d0f4198422a
3
+ metadata.gz: 71e9c2a099eb83446024318fc648d1f2a57ef50b
4
+ data.tar.gz: 7ac07e9534630ac83a82e98827a3c3c2cd13bca4
5
5
  SHA512:
6
- metadata.gz: 3909ae41e89c248431a372d19ccc8e7208cf05180423a75f141af0859d42e3935fc0d75fdd1841d8671bf6b5b1bd1888af6fb68121db9e3552a62e6b69322254
7
- data.tar.gz: 664275e1ea404113a537dcff559b430943664171f415c6be11c49d398f4986c91e694c927274c012851f0936a03c34a5fcd0c63b6e4d0aa71a595a93041d0476
6
+ metadata.gz: a6b189e44deb8ce5769fd3ea675ffe6c83c7c6d97162797508672228f6ee0ad2703bc83b276e6c89bde82c3b306c9f68930ec508ffffd21db4288642886e2a1a
7
+ data.tar.gz: d1fd3b855001ab803d6c92489db691cf1845af61df1e474be98b50ee15e6aa4441dbaca6a4fcba6bc5d375f2ee342bfa5b1a66c14e61f0d0b2fc43990806cc43
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ ## generated-assets
2
+ Programmatically generate assets for the Rails asset pipeline.
3
+
4
+ ## Installation
5
+
6
+ `gem install generated-assets`
7
+
8
+ or put it in your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'generated-assets'
12
+ ```
13
+
14
+ This gem is designed to work with Rails. Adding it to your Gemfile should be enough to add it to your project - you shouldn't need to manually require it.
15
+
16
+ ### Rationale
17
+
18
+ The Rails asset pipeline is great, but not super extensible. Generally speaking, any file you want it to recognize has to be on disk somewhere. That's pretty limiting, especially if you have a bunch of similar resources you'd like to add, but don't want to create individual files for. A great example is translations in Javascript. You probably don't want to include the translations for every locale in your Javascript bundle. Wouldn't it be nice if you could iterate over a list of locales and add translation files programmatically? This gem can help.
19
+
20
+ ### Getting Started
21
+
22
+ The generated-assets gem adds an attribute called `generated` to the Rails configuration object. Adding assets is easy:
23
+
24
+ ```ruby
25
+ # config/application.rb
26
+
27
+ I18n.available_locales.each do |locale|
28
+ config.assets.generated.add("translations-#{locale}.js") do
29
+ "window.translations ||= {}; window.translations.#{locale} = { ... }"
30
+ end
31
+ end
32
+ ```
33
+
34
+ The `add` method expects to be given the path of the new asset and a block. The return value of the block gets written inside the new asset file.
35
+
36
+ ### Asset Precompilation
37
+
38
+ The gem also supports adding any generated assets to your application's precompile list. Just include `precompile: true`:
39
+
40
+ ```ruby
41
+ config.assets.generated.add("my_asset.css", precompile: true) do
42
+ ...
43
+ end
44
+ ```
45
+
46
+ ### How Does it Work?
47
+
48
+ Under the covers, generated-assets creates a temporary directory that it adds to the asset pipeline's load path. It then writes each file you've requested to the temporary directory. As far as the asset pipeline is concerned, those files are accessible and available as if they lived in your `app/assets` directory.
49
+
50
+ ## License
51
+
52
+ Licensed under the MIT license. See LICENSE for details.
53
+
54
+ ## Authors
55
+
56
+ * Cameron C. Dutro: http://github.com/camertron
@@ -1,6 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'fileutils'
4
3
  require 'generated-assets/railtie'
5
4
  require 'securerandom'
6
5
  require 'tmpdir'
@@ -14,9 +13,7 @@ module GeneratedAssets
14
13
  class << self
15
14
  def asset_dir
16
15
  @asset_dir ||= begin
17
- dir = File.join(Dir.tmpdir, SecureRandom.hex)
18
- FileUtils.mkdir_p(dir)
19
- dir
16
+ File.join(Dir.tmpdir, SecureRandom.hex)
20
17
  end
21
18
  end
22
19
  end
@@ -1,5 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ require 'fileutils'
4
+
3
5
  module GeneratedAssets
4
6
  class Manifest
5
7
  attr_reader :app, :prefix, :entries
@@ -37,11 +39,17 @@ module GeneratedAssets
37
39
  private
38
40
 
39
41
  def write_files
42
+ ensure_prefix_dir_exists unless entries.empty?
43
+
40
44
  entries.each do |entry|
41
45
  entry.write_to(prefix)
42
46
  end
43
47
  end
44
48
 
49
+ def ensure_prefix_dir_exists
50
+ FileUtils.mkdir_p(prefix)
51
+ end
52
+
45
53
  def add_precompile_paths
46
54
  entries.each do |entry|
47
55
  if entry.precompile?
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module GeneratedAssets
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
data/spec/log/test.log CHANGED
@@ -75,3 +75,6 @@ Compiled application.js (0ms) (pid 48920)
75
75
  Compiled application.js (0ms) (pid 68040)
76
76
  Compiled application.js (0ms) (pid 68194)
77
77
  Compiled application.js (0ms) (pid 68208)
78
+ Compiled application.js (0ms) (pid 8591)
79
+ Compiled application.js (0ms) (pid 8597)
80
+ Compiled application.js (0ms) (pid 8602)
data/spec/precomp_spec.rb CHANGED
@@ -16,19 +16,29 @@ describe 'precompilation' do
16
16
  )
17
17
  end
18
18
 
19
- it 'precompiles generated assets correctly' do
20
- assets_config.generated.add('foo/bar.txt', precompile: true) do
21
- 'bar text'
22
- end
23
-
24
- assets_config.generated.apply!
19
+ let(:prefix) do
20
+ Pathname(assets_config.generated.prefix)
21
+ end
25
22
 
23
+ def run_precompile_task
26
24
  begin
27
25
  Rake::Task['assets:precompile:primary'].invoke
28
26
  Rake::Task['assets:precompile:nondigest'].invoke
29
27
  rescue RuntimeError
30
28
  Rake::Task['assets:precompile'].invoke
31
29
  end
30
+ end
31
+
32
+ it 'precompiles generated assets correctly' do
33
+ assets_config.generated.add('foo/bar.txt', precompile: true) do
34
+ 'bar text'
35
+ end
36
+
37
+ expect(prefix).to_not exist
38
+ assets_config.generated.apply!
39
+ expect(prefix).to exist
40
+
41
+ run_precompile_task
32
42
 
33
43
  rails_manifest = RailsManifest.load_for(app)
34
44
  digest_file = rails_manifest.find_by_logical('foo/bar.txt')
@@ -37,4 +47,12 @@ describe 'precompilation' do
37
47
  contents = asset_path.join(digest_file).read
38
48
  expect(contents).to eq('bar text')
39
49
  end
50
+
51
+ it 'does not create a temp directory if no assets have been added' do
52
+ expect(prefix).to_not exist
53
+ assets_config.generated.apply!
54
+ expect(prefix).to_not exist
55
+ run_precompile_task
56
+ expect(prefix).to_not exist
57
+ end
40
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generated-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-07 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -66,6 +66,7 @@ extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
68
  - Gemfile
69
+ - README.md
69
70
  - Rakefile
70
71
  - generated-assets.gemspec
71
72
  - lib/generated-assets.rb