startback 0.4.0 → 0.4.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: 2c343f2a58bb989ce6d4f14f0fe478a0e8b69ac8
4
- data.tar.gz: 4ef6a2962120e23b5f4afdd401918a5c8688ac58
3
+ metadata.gz: a06b0b31cc8840a4dd20381feb44c16a2f7a83dd
4
+ data.tar.gz: 9cc508e4ac8f662f4bb1a8f60fba6f7fe8b36c82
5
5
  SHA512:
6
- metadata.gz: 8f3ac41ca8f1bd8ea51ef4d58d868aa0694839ccbbc372a1fa0445c5f20be02c3ff036fed65cbdc24aa0283b37256b54aa16aa5711019891a648bb8c0fab34a3
7
- data.tar.gz: db4057a610fd4e8676816bd05892419900f860dbc4b622ca2d34d93702d1a50418d0a43f56105ac1fbd2a2fc46d73d7974aed6d1ffedcab41238c0f5ef40c526
6
+ metadata.gz: 4936d6d7fc528704900c7178c95c9d9b1ab20befd46080ac1545f49fb56054deb8b3befd68afb426dc751ba3d4c065adf432c94b2b6c9e12a0690da1c5ac98a1
7
+ data.tar.gz: d652e019cf8c22043bf8688252b89578f71fa73fe0265307105e78dd69473beb9ba36ae79f4e6cc626847aeab8f5616bd881630d957aa32dc0155dbe62114330
@@ -2,7 +2,7 @@ module Startback
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 4
5
- TINY = 0
5
+ TINY = 1
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -34,7 +34,8 @@ module Startback
34
34
  class MagicAssets
35
35
 
36
36
  DEFAULT_OPTIONS = {
37
- sprockets: {}
37
+ sprockets: {},
38
+ plugins: {}
38
39
  }
39
40
 
40
41
  def initialize(app, options = {})
@@ -85,6 +86,9 @@ module Startback
85
86
  @options[:sprockets].each_pair do |k,v|
86
87
  s.public_send(:"#{k}=", v)
87
88
  end
89
+ @options[:plugins].each do |p|
90
+ p.install(s)
91
+ end
88
92
  }
89
93
  end
90
94
 
@@ -0,0 +1,80 @@
1
+ module Startback
2
+ module Web
3
+ class MagicAssets
4
+ #
5
+ # Plugin for MagicAssets that compiles .html angular templates in the
6
+ # assets structure to javascript files filling angular's template cache.
7
+ #
8
+ # Heavily inspired, yet over-simplified version, of angular-rails-templates
9
+ # See https://github.com/pitr/angular-rails-templates, licensed under MIT
10
+ #
11
+ # Example:
12
+ #
13
+ # use Startback::Web::MagicAssets, {
14
+ # plugins: [Startback::Web::MagicAssets::NgHtmlTransfomer.new]
15
+ # }
16
+ #
17
+ class NgHtmlTransformer
18
+
19
+ DEFAULT_OPTIONS = {
20
+ :path => '/assets',
21
+ :ng_module => 'templates',
22
+ :mime_type => 'text/ng-html',
23
+ :extensions => [".html"]
24
+ }
25
+
26
+ def initialize(options = {})
27
+ @options = DEFAULT_OPTIONS.merge(options)
28
+ end
29
+ attr_reader :options
30
+
31
+ def install(sprockets)
32
+ sprockets.register_mime_type options[:mime_type], extensions: options[:extensions]
33
+ sprockets.register_transformer options[:mime_type], 'application/javascript', self
34
+ end
35
+
36
+ TPL = <<-EOF
37
+ angular.module("<%= ng_module %>").run(["$templateCache", function($templateCache) {
38
+ $templateCache.put("<%= angular_template_name %>", <%= html %>)
39
+ }]);
40
+ EOF
41
+
42
+ # inspired by Rails' action_view/helpers/javascript_helper.rb
43
+ JS_ESCAPE_MAP = {
44
+ '\\' => '\\\\',
45
+ "\r\n" => '\n',
46
+ "\n" => '\n',
47
+ "\r" => '\n',
48
+ '"' => '\\"',
49
+ "'" => "\\'"
50
+ }
51
+
52
+ # We want to deliver the shortist valid javascript escaped string
53
+ # Count the number of " vs '
54
+ # If more ', escape "
55
+ # If more ", escape '
56
+ # If equal, prefer to escape "
57
+
58
+ def escape_javascript(raw)
59
+ if raw
60
+ quote = raw.count(%{'}) >= raw.count(%{"}) ? %{"} : %{'}
61
+ escaped = raw.gsub(/(\\|\r\n|[\n\r#{quote}])/u) {|match| JS_ESCAPE_MAP[match] }
62
+ "#{quote}#{escaped}#{quote}"
63
+ else
64
+ '""'
65
+ end
66
+ end
67
+
68
+ def call(input)
69
+ file_path = input[:filename]
70
+ angular_template_name = "#{options[:path]}/#{input[:name]}.html"
71
+ source_file = file_path
72
+ ng_module = options[:ng_module]
73
+ html = escape_javascript(input[:data].chomp)
74
+ ERB.new(TPL).result(binding)
75
+ end
76
+
77
+ end # class NgHtmlTransformer
78
+ end # class MagicAssets
79
+ end # module Web
80
+ end # module Startback
@@ -0,0 +1 @@
1
+ <p>Hello {{who}}</p>
@@ -1 +1 @@
1
- //= require ./app/hello
1
+ //= require_tree ./app
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'startback/web/magic_assets'
3
+ require 'startback/web/magic_assets/ng_html_transformer'
3
4
 
4
5
  module Startback
5
6
  module Web
@@ -21,7 +22,7 @@ module Startback
21
22
 
22
23
  it 'delegates a [] call to sprockets' do
23
24
  result = app['index.js']
24
- expect(result.to_s).to match(/function test/)
25
+ expect(result.to_s).to match(/function test/)
25
26
  end
26
27
 
27
28
  it 'returns a 404 on unknown' do
@@ -56,6 +57,26 @@ module Startback
56
57
  end
57
58
  end
58
59
 
60
+ context 'when registering the NgHtmlTransformer' do
61
+ let(:app){
62
+ plugin = MagicAssets::NgHtmlTransformer.new({
63
+ ng_module: "test.templates"
64
+ })
65
+ MagicAssets.new({
66
+ folder: Path.dir/"fixtures/assets",
67
+ plugins: [plugin]
68
+ })
69
+ }
70
+
71
+ it 'works as expected' do
72
+ get "/index.js"
73
+ expect(last_response.status).to eql(200)
74
+ expect(last_response.body).to match(/function test/)
75
+ expect(last_response.body).to match(/Hello \{\{who\}\}/)
76
+ expect(last_response.body).to match(/angular.module\("test.templates"\)/)
77
+ end
78
+ end
79
+
59
80
  end
60
81
  end
61
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
@@ -164,6 +164,7 @@ files:
164
164
  - lib/startback/web/cors_headers.rb
165
165
  - lib/startback/web/health_check.rb
166
166
  - lib/startback/web/magic_assets.rb
167
+ - lib/startback/web/magic_assets/ng_html_transformer.rb
167
168
  - lib/startback/web/magic_assets/rake_tasks.rb
168
169
  - lib/startback/web/shield.rb
169
170
  - spec/spec_helper.rb
@@ -171,6 +172,7 @@ files:
171
172
  - spec/unit/test_operation.rb
172
173
  - spec/unit/test_support.rb
173
174
  - spec/unit/web/fixtures/assets/app/hello.es6
175
+ - spec/unit/web/fixtures/assets/app/hello.html
174
176
  - spec/unit/web/fixtures/assets/index.es6
175
177
  - spec/unit/web/test_auto_caching.rb
176
178
  - spec/unit/web/test_catch_all.rb