angular-html2js 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +20 -0
  6. data/README.md +138 -0
  7. data/Rakefile +1 -0
  8. data/angular-html2js.gemspec +33 -0
  9. data/app/.gitignore +16 -0
  10. data/app/Gemfile +48 -0
  11. data/app/Rakefile +6 -0
  12. data/app/app/assets/javascripts/templates/test.js.ngt +1 -0
  13. data/app/bin/bundle +3 -0
  14. data/app/bin/rails +4 -0
  15. data/app/bin/rake +4 -0
  16. data/app/config.ru +4 -0
  17. data/app/config/application.rb +23 -0
  18. data/app/config/boot.rb +4 -0
  19. data/app/config/database.yml +25 -0
  20. data/app/config/environment.rb +5 -0
  21. data/app/config/environments/development.rb +29 -0
  22. data/app/config/environments/production.rb +80 -0
  23. data/app/config/environments/test.rb +36 -0
  24. data/app/config/initializers/backtrace_silencers.rb +7 -0
  25. data/app/config/initializers/filter_parameter_logging.rb +4 -0
  26. data/app/config/initializers/inflections.rb +16 -0
  27. data/app/config/initializers/mime_types.rb +5 -0
  28. data/app/config/initializers/secret_token.rb +12 -0
  29. data/app/config/initializers/session_store.rb +3 -0
  30. data/app/config/initializers/wrap_parameters.rb +14 -0
  31. data/app/config/locales/en.yml +23 -0
  32. data/app/config/routes.rb +56 -0
  33. data/app/db/seeds.rb +7 -0
  34. data/app/log/.keep +0 -0
  35. data/lib/angular/html2js.rb +3 -0
  36. data/lib/angular/html2js/configuration.rb +39 -0
  37. data/lib/angular/html2js/engine.rb +14 -0
  38. data/lib/angular/html2js/railtie.rb +18 -0
  39. data/lib/angular/html2js/template.rb +66 -0
  40. data/lib/angular/html2js/version.rb +5 -0
  41. data/spec/angular/html2js/engine_spec.rb +31 -0
  42. data/spec/angular/html2js/railtie_spec.rb +39 -0
  43. data/spec/angular/html2js/template_spec.rb +89 -0
  44. data/spec/assets/test.js.ngt +1 -0
  45. data/spec/spec_helper.rb +15 -0
  46. data/spec/support/template_cache.coffee +27 -0
  47. data/spec/support/template_matchers.rb +70 -0
  48. metadata +252 -0
@@ -0,0 +1,36 @@
1
+ App::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static asset server for tests with Cache-Control for performance.
16
+ config.serve_static_assets = true
17
+ config.static_cache_control = "public, max-age=3600"
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Tell Action Mailer not to deliver emails to the real world.
30
+ # The :test delivery method accumulates sent emails in the
31
+ # ActionMailer::Base.deliveries array.
32
+ config.action_mailer.delivery_method = :test
33
+
34
+ # Print deprecation notices to the stderr.
35
+ config.active_support.deprecation = :stderr
36
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]
@@ -0,0 +1,16 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format. Inflections
4
+ # are locale specific, and you may define rules for as many different
5
+ # locales as you wish. All of these examples are active by default:
6
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
7
+ # inflect.plural /^(ox)$/i, '\1en'
8
+ # inflect.singular /^(ox)en/i, '\1'
9
+ # inflect.irregular 'person', 'people'
10
+ # inflect.uncountable %w( fish sheep )
11
+ # end
12
+
13
+ # These inflection rules are supported but not enabled by default:
14
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
15
+ # inflect.acronym 'RESTful'
16
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,12 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure your secret_key_base is kept private
11
+ # if you're sharing your code publicly.
12
+ App::Application.config.secret_key_base = '5e180a8386a7bb528cb6176b8b0eb56d8ba2eae48d30a9e7e4b70046ef8e5d4670d4bf3920246e6955d14be7f2e7767664f4c22faa1f397af1170dcc8a9fa9fe'
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ App::Application.config.session_store :cookie_store, key: '_app_session'
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,56 @@
1
+ App::Application.routes.draw do
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+ # See how all your routes lay out with "rake routes".
4
+
5
+ # You can have the root of your site routed with "root"
6
+ # root 'welcome#index'
7
+
8
+ # Example of regular route:
9
+ # get 'products/:id' => 'catalog#view'
10
+
11
+ # Example of named route that can be invoked with purchase_url(id: product.id)
12
+ # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
13
+
14
+ # Example resource route (maps HTTP verbs to controller actions automatically):
15
+ # resources :products
16
+
17
+ # Example resource route with options:
18
+ # resources :products do
19
+ # member do
20
+ # get 'short'
21
+ # post 'toggle'
22
+ # end
23
+ #
24
+ # collection do
25
+ # get 'sold'
26
+ # end
27
+ # end
28
+
29
+ # Example resource route with sub-resources:
30
+ # resources :products do
31
+ # resources :comments, :sales
32
+ # resource :seller
33
+ # end
34
+
35
+ # Example resource route with more complex sub-resources:
36
+ # resources :products do
37
+ # resources :comments
38
+ # resources :sales do
39
+ # get 'recent', on: :collection
40
+ # end
41
+ # end
42
+
43
+ # Example resource route with concerns:
44
+ # concern :toggleable do
45
+ # post 'toggle'
46
+ # end
47
+ # resources :posts, concerns: :toggleable
48
+ # resources :photos, concerns: :toggleable
49
+
50
+ # Example resource route within a namespace:
51
+ # namespace :admin do
52
+ # # Directs /admin/products/* to Admin::ProductsController
53
+ # # (app/controllers/admin/products_controller.rb)
54
+ # resources :products
55
+ # end
56
+ end
data/app/db/seeds.rb ADDED
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
data/app/log/.keep ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ require "angular/html2js/version"
2
+ require "angular/html2js/engine"
3
+ require 'angular/html2js/railtie' if defined?(Rails)
@@ -0,0 +1,39 @@
1
+ module Angular
2
+ module Html2js
3
+ def self.configure
4
+ yield config
5
+ end
6
+
7
+
8
+ def self.config
9
+ @config ||= Configuration.new
10
+ end
11
+
12
+
13
+ def self.reset_config!
14
+ config.reset!
15
+ end
16
+
17
+
18
+ class Configuration
19
+ attr_accessor :module_name
20
+
21
+ def cache_id(&block)
22
+ if block
23
+ @cache_id = block
24
+ else
25
+ @cache_id
26
+ end
27
+ end
28
+
29
+ def reset!
30
+ @cache_id = @module_name = nil
31
+ end
32
+
33
+ def method_missing(config_name, *)
34
+ puts "Sorry, there is no such configuration option named #{config_name}"
35
+ super
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ require 'angular/html2js/template'
2
+
3
+ module Angular
4
+ module Html2js
5
+ class Engine < Template
6
+
7
+ private
8
+
9
+ def default_cache_id_proc
10
+ Proc.new { |file_path, scope| scope.logical_path }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'angular/html2js'
2
+ require 'rails'
3
+
4
+ module Angular
5
+ module Html2js
6
+ class Railtie < Rails::Railtie
7
+ railtie_name :angular_html2js
8
+
9
+ config.before_configuration do
10
+ config.angular_html2js = Angular::Html2js.config
11
+ end
12
+
13
+ initializer "angular_html2js.configure_rails_initialization" do
14
+ Sprockets.register_engine '.ngt', Angular::Html2js::Engine
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,66 @@
1
+ require 'tilt'
2
+ require 'angular/html2js/configuration'
3
+
4
+ module Angular
5
+ module Html2js
6
+ class Template < Tilt::Template
7
+ attr_accessor :file
8
+
9
+ self.default_mime_type = 'application/javascript'
10
+
11
+ TEMPLATE = <<-TEMPLATE
12
+ angular.module(\'%s\', []).run(function($templateCache) {
13
+ $templateCache.put(\'%s\',
14
+ \'%s\');
15
+ });
16
+ TEMPLATE
17
+
18
+
19
+ SINGLE_MODULE_TPL = <<-SINGLE_MODULE_TPL
20
+ (function(module) {
21
+ try {
22
+ module = angular.module(\'%s\');
23
+ } catch (e) {
24
+ module = angular.module(\'%s\', []);
25
+ }
26
+ module.run(function($templateCache) {
27
+ $templateCache.put(\'%s\',
28
+ \'%s\');
29
+ });
30
+ })();
31
+ SINGLE_MODULE_TPL
32
+
33
+ def config
34
+ Html2js.config
35
+ end
36
+
37
+ def prepare; end
38
+
39
+ def evaluate(scope, locals, &block)
40
+ @module_name = config.module_name
41
+ @cache_id = config.cache_id || default_cache_id_proc
42
+ @scope = scope
43
+ if @module_name
44
+ SINGLE_MODULE_TPL % [@module_name, @module_name, cache_id, escapeContent(data)]
45
+ else
46
+ TEMPLATE % [cache_id, cache_id, escapeContent(data)]
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def escapeContent(content)
53
+ content.gsub(/\\/, '\\\\\\').gsub("'", %q{\\\\'}).gsub(/\r?\n/, "\\\\n\' +\n \'")
54
+ end
55
+
56
+ def cache_id
57
+ @cache_id.call(file, @scope)
58
+ end
59
+
60
+ def default_cache_id_proc
61
+ Proc.new { file }
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module Angular
2
+ module Html2js
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'sprockets'
3
+ require 'angular/html2js/engine'
4
+
5
+ module Angular
6
+ module Html2js
7
+ describe Engine do
8
+ let(:env) do
9
+ Sprockets::Environment.new do |env|
10
+ env.register_engine '.ngt', Engine
11
+ env.append_path 'spec/assets/'
12
+ end
13
+ end
14
+
15
+ let(:asset) { env.find_asset('test.js.ngt') }
16
+
17
+ it 'makes angular templates available' do
18
+ asset.to_s.should include("angular.module")
19
+ asset.to_s.should include("<html>")
20
+ end
21
+
22
+ it 'recognizes them as javascript' do
23
+ asset.content_type.should == 'application/javascript'
24
+ end
25
+
26
+ it 'uses the logical path if no module_name is provided' do
27
+ asset.to_s.should include(".put('test'")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
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 Html2js
12
+ describe Railtie, type: :feature do
13
+ before(:all) do
14
+ end
15
+
16
+ before(:each) { FileUtils.rm_rf Rails.root.join('tmp', 'cache', 'assets', 'test') }
17
+
18
+ it "should setup rails to serve ng templates" do
19
+ visit '/assets/templates/test.js'
20
+ page.should have_content "$templateCache"
21
+ page.should have_content "Hello World"
22
+ end
23
+
24
+ describe "Configuration" do
25
+ after { Html2js.reset_config! }
26
+
27
+ it "allows you to configure the module" do
28
+ Rails.configuration.angular_html2js.module_name = 'myRailsModule'
29
+ Html2js.config.module_name.should == 'myRailsModule'
30
+ end
31
+
32
+ it "allows you to set the cache key" do
33
+ Rails.configuration.angular_html2js.cache_id { |file| "rails-#{file}" }
34
+ Html2js.config.cache_id.call('test').should == 'rails-test'
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+ require 'angular/html2js/template'
3
+
4
+ module Angular
5
+ module Html2js
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 { Html2js.reset_config! }
43
+
44
+ describe 'cache_id_from_scope ' do
45
+ before do
46
+ Html2js.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 { Html2js.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