js-asset_paths 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0634b5efe0f04adce99e7a8214985004dcf43eff
4
+ data.tar.gz: 0c0913d6501909e21830cc712e12cea98178cc7f
5
+ SHA512:
6
+ metadata.gz: dc925fe525271aeab99de6a8b7bb2045eac33374c88f215142ce75b47d1ef023227272ad56343e2ec6f7ed8899aaa11cbc88795de3d0225e8421b9b795edd4c4
7
+ data.tar.gz: bb1a26f2a9b9b8569d6fcb8e9415cf353663776f210c5ee354d1daa0d63cad8d4d04d43f6794217004f85ef71868a261b88821b472975253e8934cebe955941b
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in js-asset_paths.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sonnym
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # JS Asset Paths
2
+
3
+ Inspired by [js-routes](https://github.com/railsware/js-routes), JS Asset Paths makes the helper methods found in [ActionView::Helpers::AssetUrlHelper](http://rubydoc.info/docs/rails/ActionView/Helpers/AssetUrlHelper) available to the client.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'js-asset_paths'
11
+ ```
12
+
13
+ Then require the js-asset_paths file in your `application.js`.
14
+
15
+ ``` js
16
+ /*
17
+ = require js-asset_paths
18
+ */
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ A global object called `PathHelper` will be created with the following methods:
24
+
25
+ - assetPath(source[, options])
26
+ - fontPath(source)
27
+ - imagePath(source)
28
+ - javascriptPath(source)
29
+ - stylesheetPath(source)
30
+
31
+ These functions operate in the same manner as their server-side counterparts.
32
+
33
+ ## Explanation
34
+
35
+ JS Asset Paths inspects the configuration options for the current environment and determines whether or not a digest will be appended to the pathname of asset files. An object correlating the truncated asset path with the actual path to the asset on the server is stored in the generated javascript and referenced during each call.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib/js_asset_paths'
8
+ t.test_files = FileList['test/lib/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,38 @@
1
+ <%# encoding: utf-8 %>
2
+
3
+ var PathHelper = (function(definitions) {
4
+ var assetExtensions = <%= ActionView::Helpers::AssetUrlHelper::ASSET_EXTENSIONS.to_json %>;
5
+ var assetPublicDirectories = <%= ActionView::Helpers::AssetUrlHelper::ASSET_PUBLIC_DIRECTORIES.to_json %>;
6
+
7
+ return {
8
+ assetPath: function(source, opts) {
9
+ var key = source;
10
+
11
+ if (opts && opts.type) {
12
+ key = [assetPublicDirectories[opts.type], key].join("/");
13
+
14
+ if (source.split(".").length === 1) {
15
+ key = [key, assetExtensions[opts.type]].join(".");
16
+ }
17
+ }
18
+
19
+ return ["assets", definitions[key] || key].join("/");
20
+ },
21
+
22
+ fontPath: function(source) {
23
+ return this.assetPath(source, { type: "font" });
24
+ },
25
+
26
+ imagePath: function(source) {
27
+ return this.assetPath(source, { type: "image" });
28
+ },
29
+
30
+ javascriptPath: function(source) {
31
+ return this.assetPath(source, { type: "javascript" });
32
+ },
33
+
34
+ stylesheetPath: function(source) {
35
+ return this.assetPath(source, { type: "stylesheet" });
36
+ },
37
+ };
38
+ })(<%= JsAssetPaths::Generator.generate! %>);
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js_asset_paths/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'js-asset_paths'
8
+ spec.version = JsAssetPaths::VERSION
9
+ spec.authors = ['sonnym']
10
+ spec.email = ['michaud.sonny@gmail.com']
11
+ spec.description = 'Access paths to compiled assets from in javascript.'
12
+ spec.summary = spec.description
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency(%q<rails>, ['>= 4.0'])
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'pry'
25
+ end
@@ -0,0 +1,3 @@
1
+ require 'js_asset_paths/version'
2
+ require 'js_asset_paths/engine'
3
+ require 'js_asset_paths/generator'
@@ -0,0 +1,18 @@
1
+ module JsAssetPaths
2
+ class Engine < Rails::Engine
3
+ JS_ASSET_PATHS_FILE = 'js-asset_paths'
4
+
5
+ isolate_namespace(JsAssetPaths)
6
+
7
+ initializer('js-assets_paths.compile', after: 'sprockets.environment') do |application|
8
+ application.assets.register_preprocessor('application/javascript', :'js-assets_path.compile') do |context, data|
9
+ if context.logical_path == JS_ASSET_PATHS_FILE
10
+ JsAssetPaths::Generator.environment = application
11
+ JsAssetPaths::Generator.context = context
12
+ end
13
+
14
+ data
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module JsAssetPaths
2
+ class Generator
3
+ cattr_accessor :environment, :context
4
+
5
+ def self.generate!
6
+ asset_hash.to_json
7
+ end
8
+
9
+ private
10
+
11
+ def self.asset_hash
12
+ manifest.files.reduce({}) do |memo, (key, value)|
13
+ memo.merge(value['logical_path'] => (context.digest_assets? ? key : value['logical_path']))
14
+ end
15
+ end
16
+
17
+ def self.manifest
18
+ Sprockets::Manifest.new(environment.assets.index, './public/assets')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module JsAssetPaths
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'js_asset_paths/version'
2
+ require 'js_asset_paths/engine'
3
+ require 'js_asset_paths/generator'
@@ -0,0 +1,8 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe JsAssetPaths::Engine do
4
+ it 'should do something' do
5
+ App.initialize!
6
+ end
7
+ end
8
+
@@ -0,0 +1,17 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'pry'
4
+
5
+ ENV['RAILS_ENV'] = 'test'
6
+ #require File.expand_path('../dummy/config/environment.rb', __FILE__)
7
+ #require 'rails/test_help'
8
+ require 'rails/all'
9
+
10
+ require File.expand_path('../../lib/js_asset_paths.rb', __FILE__)
11
+
12
+ class App < Rails::Application
13
+ config.eager_load = true
14
+
15
+ config.assets.enabled = true
16
+ config.assets.initialize_on_precompile = true
17
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-asset_paths
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sonnym
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Access paths to compiled assets from in javascript.
70
+ email:
71
+ - michaud.sonny@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-version
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - app/assets/javascripts/js-asset_paths.js.erb
83
+ - js_asset_paths.gemspec
84
+ - lib/js-asset_paths.rb
85
+ - lib/js_asset_paths.rb
86
+ - lib/js_asset_paths/engine.rb
87
+ - lib/js_asset_paths/generator.rb
88
+ - lib/js_asset_paths/version.rb
89
+ - test/lib/engine_test.rb
90
+ - test/test_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Access paths to compiled assets from in javascript.
115
+ test_files:
116
+ - test/lib/engine_test.rb
117
+ - test/test_helper.rb