angular-ngt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +174 -0
- data/lib/angular/ngt.rb +3 -0
- data/lib/angular/ngt/configuration.rb +49 -0
- data/lib/angular/ngt/engine.rb +16 -0
- data/lib/angular/ngt/haml.rb +10 -0
- data/lib/angular/ngt/template.rb +68 -0
- data/lib/angular/ngt/version.rb +5 -0
- data/spec/angular/html2js/engine_spec.rb +64 -0
- data/spec/angular/html2js/haml_spec.rb +11 -0
- data/spec/angular/html2js/railtie_spec.rb +52 -0
- data/spec/angular/html2js/template_spec.rb +89 -0
- data/spec/assets/normal.html +1 -0
- data/spec/assets/test.ngt +1 -0
- data/spec/assets/test_haml.ngt.haml +1 -0
- data/spec/assets/test_html.ngt.html +1 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/template_cache.coffee +31 -0
- data/spec/support/template_matchers.rb +70 -0
- metadata +230 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f380fff1215f99fa11efc01d91979d5233ff010b
|
4
|
+
data.tar.gz: 7601507672190c341bcff54eda5fc47346b23c72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 57b6ea12d403b8a51e7b1f8be05ff02057eefab8e6817695b449a774e76bcfb7101e32ae87d7902b1bab9a3feffe8406dd9d14da7db5f7d3a2eacc004b040eb8
|
7
|
+
data.tar.gz: 5b3706cb41aab2a3b8aad07b44823a3f567cfe015a918775ccb1d70a7afd04e7e367c35442041f715535a20ccc677077fd84cb090255c2b33cda925746a05169
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 K3 Integrations, LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# Angular-ngt <a href="http://badge.fury.io/rb/angular-ngt"><img src="https://badge.fury.io/rb/angular-ngt@2x.png" alt="Gem Version" height="18"></a>
|
2
|
+
|
3
|
+
Angular-ngt is based off the Karma [preprocessor](https://github.com/karma-runner/karma-ng-ngt-preprocessor)
|
4
|
+
that many Angular folks use. This gem makes your templates available through the Rails asset pipeline, Sprockets,
|
5
|
+
or Tilt, while still acting like Karma's ng-ngt. So it should feel familiar to those already
|
6
|
+
using ng-ngt and should be less confusing for those following instructions in online
|
7
|
+
tutorials.
|
8
|
+
|
9
|
+
Look in the usage section below for examples.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'angular-ngt'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install angular-ngt
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
This gem adds support for AngularJS templates. Those familiar with the Karma
|
28
|
+
test runner will be able to use the familiar `beforeEach(module('myTemplateModule'))`
|
29
|
+
or `beforeEach(module('/your/template/file.html'))`. Additionally, you can now leverage
|
30
|
+
sprockets to help you. In Rails, this means that you can also inline your templates in
|
31
|
+
production!
|
32
|
+
|
33
|
+
#### File naming
|
34
|
+
|
35
|
+
All source files need to have the extension `.ngt`.
|
36
|
+
|
37
|
+
```
|
38
|
+
myTemplate.ngt # <= plain html
|
39
|
+
myTemplate.ngt.haml # <= haml!
|
40
|
+
```
|
41
|
+
|
42
|
+
```coffeescript
|
43
|
+
# in your code, you can just reference the path without extensions
|
44
|
+
#=require myTemplate
|
45
|
+
|
46
|
+
//= require my_template
|
47
|
+
|
48
|
+
angular.module('myApp').directive('myDirective', function($injectable){
|
49
|
+
{ restrict: 'A'
|
50
|
+
templateUrl: 'my_template'
|
51
|
+
// Etc...
|
52
|
+
}
|
53
|
+
});
|
54
|
+
```
|
55
|
+
|
56
|
+
### Include in global module (recommended)
|
57
|
+
|
58
|
+
1. Configure a top level shared module
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# In Rails
|
62
|
+
MyApp::Application.configure do
|
63
|
+
config.angular_ngt.module_name = 'MyApp'
|
64
|
+
end
|
65
|
+
|
66
|
+
# or Any
|
67
|
+
Angular::NGT.configure do |config|
|
68
|
+
config.angular_ngt.module_name = 'MyApp'
|
69
|
+
end
|
70
|
+
```
|
71
|
+
2. Now just require the template before the code needing it (perhaps a directive)
|
72
|
+
|
73
|
+
```javascript
|
74
|
+
//= require my_template
|
75
|
+
|
76
|
+
angular.module('myApp').directive('myDirective', function($injectable){
|
77
|
+
{ restrict: 'A'
|
78
|
+
templateUrl: 'my_template'
|
79
|
+
// Etc...
|
80
|
+
}
|
81
|
+
});
|
82
|
+
```
|
83
|
+
|
84
|
+
### Using auto-generated modules (No configuration needed)
|
85
|
+
|
86
|
+
* Just require your template, depend on the automatically generated module
|
87
|
+
(named after the template), and use the template path as your templateURL
|
88
|
+
|
89
|
+
```javascript
|
90
|
+
// This first line is the only additional step needed!
|
91
|
+
//= require full/path/to/my_template
|
92
|
+
|
93
|
+
angular.module('myApp', ['/full/path/to/myTemplate.html']).directive('myDirective', function($injectable){
|
94
|
+
{ restrict: 'A'
|
95
|
+
templateUrl: 'full/path/to/myTemplate'
|
96
|
+
// Etc...
|
97
|
+
}
|
98
|
+
});
|
99
|
+
```
|
100
|
+
|
101
|
+
### Use custom module
|
102
|
+
|
103
|
+
1. Configure a top level shared module
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# In Rails
|
107
|
+
MyApp::Application.configure do
|
108
|
+
config.angular_ngt.module_name = 'MyTemplates'
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
2. Depend on that module
|
113
|
+
|
114
|
+
```javascript
|
115
|
+
//= require full/path/to/myTemplate
|
116
|
+
|
117
|
+
angular.module('myApp', ['MyTemplates']).directive('myDirective', function($injectable){
|
118
|
+
{ restrict: 'A'
|
119
|
+
templateUrl: 'full/path/to/myTemplate'
|
120
|
+
// Etc...
|
121
|
+
}
|
122
|
+
});
|
123
|
+
```
|
124
|
+
|
125
|
+
### Use custom module and template ID
|
126
|
+
|
127
|
+
1. Configure a top level shared module and custom cache ID
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
# In Rails
|
131
|
+
MyApp::Application.configure do
|
132
|
+
config.angular_ngt.module_name = 'MyTemplates'
|
133
|
+
|
134
|
+
# `file` is the full file path
|
135
|
+
# `scope` is the Sprockets scope. This has handy things like scope.logical_path
|
136
|
+
#
|
137
|
+
config.angular_ngt.cache_id {|file_path, scope|
|
138
|
+
"myTemplates-#{scope.logical_path}"
|
139
|
+
}
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
2. Depend on that module and use the cache ID you configured above for the templateUrl
|
144
|
+
|
145
|
+
```javascript
|
146
|
+
//= require templates/myTpl
|
147
|
+
|
148
|
+
angular.module('myApp', ['MyTemplates']).directive('myDirective', function($injectable){
|
149
|
+
{ restrict: 'A'
|
150
|
+
templateUrl: 'myTemplates-templates/myTpl'
|
151
|
+
// Etc...
|
152
|
+
}
|
153
|
+
});
|
154
|
+
```
|
155
|
+
|
156
|
+
## Important Note:
|
157
|
+
|
158
|
+
If you change anything in the configuration, be sure you clear your asset cache to ensure the templates get
|
159
|
+
updated. You can do this in a Rails app by running `rake tmp:clear` or `rm -rf tmp/cache/assets` from the app root.
|
160
|
+
|
161
|
+
This is necessary because Sprockets is only designed to watch the asset _source files_ for changes. It is unaware
|
162
|
+
that the template files have a dependency on the application configuration and therefore doesn't regenerate the templates.
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
1. Fork it
|
167
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
168
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
169
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
170
|
+
5. Create new Pull Request
|
171
|
+
|
172
|
+
|
173
|
+
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/k3integrations/angular-ngt/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|
174
|
+
|
data/lib/angular/ngt.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'sprockets'
|
2
|
+
require 'angular/ngt/haml'
|
3
|
+
|
4
|
+
module Angular
|
5
|
+
module NGT
|
6
|
+
def self.configure
|
7
|
+
yield config
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
@config ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def self.reset_config!
|
17
|
+
config.reset!
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
class Configuration
|
22
|
+
attr_accessor :module_name
|
23
|
+
|
24
|
+
def cache_id(&block)
|
25
|
+
if block
|
26
|
+
@cache_id = block
|
27
|
+
else
|
28
|
+
@cache_id
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset!
|
33
|
+
@cache_id = @module_name = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(config_name, *)
|
37
|
+
puts "Sorry, there is no such configuration option named #{config_name}"
|
38
|
+
super
|
39
|
+
end
|
40
|
+
|
41
|
+
def init_sprockets
|
42
|
+
# hack around bug in rails assets debug mode
|
43
|
+
# TODO: remove this once this bug is resolved: https://github.com/sstephenson/sprockets/issues/478
|
44
|
+
Sprockets.register_engine '.haml', Angular::NGT::Haml
|
45
|
+
Sprockets.register_engine '.ngt', Angular::NGT::Engine
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
require 'angular/ngt/configuration'
|
3
|
+
|
4
|
+
module Angular
|
5
|
+
module NGT
|
6
|
+
class Template < Tilt::Template
|
7
|
+
attr_accessor :file
|
8
|
+
|
9
|
+
def self.default_mime_type
|
10
|
+
'application/javascript'
|
11
|
+
end
|
12
|
+
|
13
|
+
TEMPLATE = <<-TEMPLATE
|
14
|
+
angular.module('/templates/%s.ngt', []).run(['$templateCache', function($templateCache) {
|
15
|
+
$templateCache.put('%s.ngt',
|
16
|
+
'%s');
|
17
|
+
}]);
|
18
|
+
TEMPLATE
|
19
|
+
|
20
|
+
|
21
|
+
SINGLE_MODULE_TPL = <<-SINGLE_MODULE_TPL
|
22
|
+
(function(module) {
|
23
|
+
try {
|
24
|
+
module = angular.module('%s');
|
25
|
+
} catch (e) {
|
26
|
+
module = angular.module('%s', []);
|
27
|
+
}
|
28
|
+
module.run(['$templateCache', function($templateCache) {
|
29
|
+
$templateCache.put('/templates/%s.ngt',
|
30
|
+
'%s');
|
31
|
+
}]);
|
32
|
+
})();
|
33
|
+
SINGLE_MODULE_TPL
|
34
|
+
|
35
|
+
def config
|
36
|
+
NGT.config
|
37
|
+
end
|
38
|
+
|
39
|
+
def prepare; end
|
40
|
+
|
41
|
+
def evaluate(scope, locals, &block)
|
42
|
+
@module_name = config.module_name || 'App'
|
43
|
+
@cache_id = config.cache_id || default_cache_id_proc
|
44
|
+
@scope = scope
|
45
|
+
if @module_name
|
46
|
+
SINGLE_MODULE_TPL % [@module_name, @module_name, cache_id, escapeContent(data)]
|
47
|
+
else
|
48
|
+
TEMPLATE % [cache_id, cache_id, escapeContent(data)]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def escapeContent(content)
|
55
|
+
content.gsub(/\\/, '\\\\\\').gsub("'", %q{\\\\'}).gsub(/\r?\n/, "\\\\n\' +\n \'")
|
56
|
+
end
|
57
|
+
|
58
|
+
def cache_id
|
59
|
+
@cache_id.call(file, @scope)
|
60
|
+
end
|
61
|
+
|
62
|
+
def default_cache_id_proc
|
63
|
+
Proc.new { file }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sprockets'
|
3
|
+
require 'angular/ngt/engine'
|
4
|
+
|
5
|
+
module Angular
|
6
|
+
module NGT
|
7
|
+
describe Engine do
|
8
|
+
let(:env) do
|
9
|
+
Sprockets::Environment.new do |env|
|
10
|
+
env.append_path 'spec/assets/'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:asset) { env.find_asset('test.ngt') }
|
15
|
+
|
16
|
+
it 'makes angular templates available' do
|
17
|
+
asset.to_s.should include("angular.module")
|
18
|
+
asset.to_s.should include("<html>")
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'recognizes them as javascript' do
|
22
|
+
asset.content_type.should == 'application/javascript'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'uses the logical path if no module_name is provided' do
|
26
|
+
asset.to_s.should include(".put('test'")
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'html file' do
|
30
|
+
let(:asset) { env.find_asset('test_html.js') }
|
31
|
+
|
32
|
+
it 'renders to an angular template ' do
|
33
|
+
pending 'html extension support' do
|
34
|
+
asset.to_s.should include("angular.module")
|
35
|
+
asset.to_s.should include("<h1>hello html</h1>")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the correct content type for html templates' do
|
40
|
+
pending 'html extension support' do
|
41
|
+
asset.content_type.should eq 'application/javascript'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "doesn't interfere with normal html files" do
|
46
|
+
env.find_asset('normal.html').to_s.should eq "<h1>hello normal html</h1>\n"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'haml file' do
|
51
|
+
let(:asset) { env.find_asset('test_haml.js') }
|
52
|
+
|
53
|
+
it 'renders to an angular template ' do
|
54
|
+
asset.to_s.should include("angular.module")
|
55
|
+
asset.to_s.should include("<h1>hello haml</h1>")
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'returns the correct content type for html templates' do
|
59
|
+
asset.content_type.should eq 'application/javascript'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ENV['RAILS_ENV'] = 'test'
|
4
|
+
|
5
|
+
require_relative '../../../app/config/environment'
|
6
|
+
|
7
|
+
require 'capybara/rspec'
|
8
|
+
require 'capybara/rails'
|
9
|
+
|
10
|
+
module Angular
|
11
|
+
module NGT
|
12
|
+
describe Railtie, type: :feature do
|
13
|
+
before(:each) { FileUtils.rm_rf Rails.root.join('tmp', 'cache', 'assets', 'test') }
|
14
|
+
|
15
|
+
it "sets up rails to serve ng templates" do
|
16
|
+
visit '/assets/templates/test.ngt'
|
17
|
+
page.should have_content "$templateCache"
|
18
|
+
page.should have_content "Hello World"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "enables Haml for js assets" do
|
22
|
+
visit '/assets/templates/test_haml.js'
|
23
|
+
page.should have_content "$templateCache"
|
24
|
+
page.body.should include "<h1>hello haml</h1>"
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'file with .html extension' do
|
28
|
+
it 'is processed by default' do
|
29
|
+
pending 'html extension support' do
|
30
|
+
visit '/assets/templates/test_html.js'
|
31
|
+
page.should have_content "$templateCache"
|
32
|
+
page.body.should include "<h1>hello html</h1>"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "Configuration" do
|
38
|
+
after { NGT.reset_config! }
|
39
|
+
|
40
|
+
it "allows you to configure the module" do
|
41
|
+
Rails.configuration.angular_ngt.module_name = 'myRailsModule'
|
42
|
+
NGT.config.module_name.should == 'myRailsModule'
|
43
|
+
end
|
44
|
+
|
45
|
+
it "allows you to set the cache key" do
|
46
|
+
Rails.configuration.angular_ngt.cache_id { |file| "rails-#{file}" }
|
47
|
+
NGT.config.cache_id.call('test').should == 'rails-test'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'angular/ngt/template'
|
3
|
+
|
4
|
+
module Angular
|
5
|
+
module NGT
|
6
|
+
describe Template do
|
7
|
+
|
8
|
+
it 'should convert html to js code' do
|
9
|
+
result = process '<h1>hello</h1>', 'tpl.html'
|
10
|
+
result.should define_module('tpl.html').
|
11
|
+
with_template_id('tpl.html').
|
12
|
+
and_content('<h1>hello</h1>')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should preserve new lines' do
|
16
|
+
result = process "first\nsecond", 'path/tpl.html'
|
17
|
+
result.should define_module('path/tpl.html').
|
18
|
+
with_template_id('path/tpl.html').
|
19
|
+
and_content("first\nsecond")
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should ignore Windows new lines' do
|
23
|
+
result = process "first\r\nsecond", 'path/tpl.html'
|
24
|
+
result.should_not include("\r")
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should preserve the backslash character' do
|
28
|
+
result = process "first\\second", 'path/tpl.html'
|
29
|
+
result.should define_module('path/tpl.html').
|
30
|
+
with_template_id('path/tpl.html').
|
31
|
+
and_content("first\\second")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should preserve single quotes' do
|
35
|
+
result = process "var h = 'hello';", 'path/tpl.html'
|
36
|
+
result.should define_module('path/tpl.html').
|
37
|
+
with_template_id('path/tpl.html').
|
38
|
+
and_content("var h = 'hello';")
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'configuration' do
|
42
|
+
after { NGT.reset_config! }
|
43
|
+
|
44
|
+
describe 'cache_id_from_scope ' do
|
45
|
+
before do
|
46
|
+
NGT.configure do |config|
|
47
|
+
config.cache_id { |file_path| "generated_id_for/#{file_path}" }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
it 'invokes custom transform block' do
|
53
|
+
result = process '<html></html>', 'path/tpl.html'
|
54
|
+
result.should define_module('generated_id_for/path/tpl.html').
|
55
|
+
with_template_id('generated_id_for/path/tpl.html').
|
56
|
+
and_content('<html></html>')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'module_name' do
|
61
|
+
before { NGT.configure { |c| c.module_name = 'foo' } }
|
62
|
+
|
63
|
+
it 'should generate code with a given module name' do
|
64
|
+
html1 = '<span>one</span>'
|
65
|
+
result1 = process html1, 'path/tpl-one.html'
|
66
|
+
html2 = '<span>two</span>'
|
67
|
+
result2 = process html2, 'path/tpl-two.html'
|
68
|
+
|
69
|
+
both_results = result1 + result2
|
70
|
+
both_results.should define_module('foo').
|
71
|
+
with_template_id('path/tpl-one.html').
|
72
|
+
and_content(html1)
|
73
|
+
|
74
|
+
both_results.should define_module('foo').
|
75
|
+
with_template_id('path/tpl-two.html').
|
76
|
+
and_content(html2)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def process(template_str, file_name, locals={})
|
82
|
+
template = Template.new { template_str }
|
83
|
+
template.file = file_name
|
84
|
+
template.render(self, locals)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>hello normal html</h1>
|
@@ -0,0 +1 @@
|
|
1
|
+
<html></html>
|
@@ -0,0 +1 @@
|
|
1
|
+
%h1 hello haml
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>hello html</h1>
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
5
|
+
config.run_all_when_everything_filtered = true
|
6
|
+
config.filter_run :focus
|
7
|
+
|
8
|
+
# Run specs in random order to surface order dependencies. If you find an
|
9
|
+
# order dependency and want to debug it, you can fix the order by providing
|
10
|
+
# the seed, which is printed after each run.
|
11
|
+
# --seed 1234
|
12
|
+
config.order = 'random'
|
13
|
+
|
14
|
+
require 'support/template_matchers'
|
15
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class AngularModule
|
2
|
+
constructor: (@name, @deps) ->
|
3
|
+
templates = @templates = {}
|
4
|
+
|
5
|
+
run: (injectable) ->
|
6
|
+
if injectable instanceof Array
|
7
|
+
[injected..., block] = injectable
|
8
|
+
else
|
9
|
+
block = injectable
|
10
|
+
block
|
11
|
+
put: (id, content) =>
|
12
|
+
@templates[id] = content
|
13
|
+
|
14
|
+
# Evaluates generated js code fot the template cache
|
15
|
+
# processedContent - The String to be evaluated
|
16
|
+
# Returns an object with the following fields
|
17
|
+
# moduleName - generated module name `angular.module('myApp')...`
|
18
|
+
# templateId - generated template id `$templateCache.put('id', ...)`
|
19
|
+
# templateContent - template content `$templateCache.put(..., <div>cache me!</div>')`
|
20
|
+
@evaluateTemplate = (processedContent) ->
|
21
|
+
modules = {}
|
22
|
+
|
23
|
+
angular =
|
24
|
+
module: (name, deps) ->
|
25
|
+
if deps? then return modules[name] = new AngularModule name, deps
|
26
|
+
if modules[name] then return modules[name]
|
27
|
+
throw new Error "Module #{name} does not exists!"
|
28
|
+
|
29
|
+
eval processedContent
|
30
|
+
|
31
|
+
modules
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'v8'
|
2
|
+
require 'coffee-script'
|
3
|
+
|
4
|
+
module Angular
|
5
|
+
module NGT
|
6
|
+
TEMPLATE_HELPER = CoffeeScript.compile(File.read("spec/support/template_cache.coffee"))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Matchers.define :define_module do |module_name|
|
11
|
+
|
12
|
+
match do |template|
|
13
|
+
@modules = modules_from(template)
|
14
|
+
@mod = @modules[module_name]
|
15
|
+
@content = @mod && @mod.templates[@template_id]
|
16
|
+
has_module && has_template_id && has_content
|
17
|
+
end
|
18
|
+
|
19
|
+
chain :with_template_id do |template_id|
|
20
|
+
@template_id = template_id
|
21
|
+
end
|
22
|
+
|
23
|
+
chain :and_content do |content|
|
24
|
+
@expected_content = content
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_module
|
28
|
+
!!@mod
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_template_id
|
32
|
+
return true unless @template_id
|
33
|
+
@template_id && @content
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_content
|
37
|
+
return true unless @expected_content
|
38
|
+
@expected_content && (@content == @expected_content)
|
39
|
+
end
|
40
|
+
|
41
|
+
failure_message_for_should do |template|
|
42
|
+
failure_message
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method :failure_message do |not_msg: nil|
|
46
|
+
msg = "Template expected"
|
47
|
+
msg += ' not' if not_msg
|
48
|
+
|
49
|
+
msg + if !has_module
|
50
|
+
" to define module '#{module_name}', found: #{@modules.keys}"
|
51
|
+
elsif !has_template_id
|
52
|
+
" to define template_id '#{@template_id}', found: #{@mod.templates.keys}"
|
53
|
+
elsif !has_content
|
54
|
+
" to have content #{@expected_content.inspect}, found: #{@content.inspect}"
|
55
|
+
else
|
56
|
+
"; Unknown reason"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
failure_message_for_should_not do |template|
|
61
|
+
failure_message('not')
|
62
|
+
end
|
63
|
+
|
64
|
+
def modules_from(template)
|
65
|
+
cxt = V8::Context.new
|
66
|
+
cxt.eval(Angular::NGT::TEMPLATE_HELPER, "spec/helpers/template_cache.coffee")
|
67
|
+
cxt['template'] = template
|
68
|
+
cxt.eval('evaluateTemplate(template)')
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: angular-ngt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Caleb Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tilt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sprockets
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 10.1.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 10.1.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: therubyracer
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.11.4
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.11.4
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coffee-script
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.2.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 2.2.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: capybara
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ~>
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 2.1.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ~>
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 2.1.0
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sqlite3
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ~>
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 1.3.7
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ~>
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.3.7
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: haml
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ~>
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 4.0.0
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ~>
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 4.0.0
|
167
|
+
description: Angular HTML2JS allows you to use ng templates as first class citizens
|
168
|
+
in the Rails/Sprockets world. Based on the karma-ng-ngt-preprocessor for Karma.
|
169
|
+
email:
|
170
|
+
- cclark@fanforce.com
|
171
|
+
executables: []
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- lib/angular/ngt/configuration.rb
|
176
|
+
- lib/angular/ngt/engine.rb
|
177
|
+
- lib/angular/ngt/haml.rb
|
178
|
+
- lib/angular/ngt/template.rb
|
179
|
+
- lib/angular/ngt/version.rb
|
180
|
+
- lib/angular/ngt.rb
|
181
|
+
- LICENSE
|
182
|
+
- README.md
|
183
|
+
- spec/angular/html2js/engine_spec.rb
|
184
|
+
- spec/angular/html2js/haml_spec.rb
|
185
|
+
- spec/angular/html2js/railtie_spec.rb
|
186
|
+
- spec/angular/html2js/template_spec.rb
|
187
|
+
- spec/assets/normal.html
|
188
|
+
- spec/assets/test.ngt
|
189
|
+
- spec/assets/test_haml.ngt.haml
|
190
|
+
- spec/assets/test_html.ngt.html
|
191
|
+
- spec/spec_helper.rb
|
192
|
+
- spec/support/template_cache.coffee
|
193
|
+
- spec/support/template_matchers.rb
|
194
|
+
homepage: http://calebclark.com
|
195
|
+
licenses:
|
196
|
+
- MIT
|
197
|
+
metadata: {}
|
198
|
+
post_install_message:
|
199
|
+
rdoc_options: []
|
200
|
+
require_paths:
|
201
|
+
- lib
|
202
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - '>='
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
|
+
requirements:
|
209
|
+
- - '>='
|
210
|
+
- !ruby/object:Gem::Version
|
211
|
+
version: '0'
|
212
|
+
requirements: []
|
213
|
+
rubyforge_project:
|
214
|
+
rubygems_version: 2.0.14
|
215
|
+
signing_key:
|
216
|
+
specification_version: 4
|
217
|
+
summary: AngularJS HTML templates in Sprockets and Rails
|
218
|
+
test_files:
|
219
|
+
- spec/angular/html2js/engine_spec.rb
|
220
|
+
- spec/angular/html2js/haml_spec.rb
|
221
|
+
- spec/angular/html2js/railtie_spec.rb
|
222
|
+
- spec/angular/html2js/template_spec.rb
|
223
|
+
- spec/assets/normal.html
|
224
|
+
- spec/assets/test.ngt
|
225
|
+
- spec/assets/test_haml.ngt.haml
|
226
|
+
- spec/assets/test_html.ngt.html
|
227
|
+
- spec/spec_helper.rb
|
228
|
+
- spec/support/template_cache.coffee
|
229
|
+
- spec/support/template_matchers.rb
|
230
|
+
has_rdoc:
|