rails-template-cache 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc268901f07132d99450aeb81de8122eaf154505
4
+ data.tar.gz: 791cb335571992755d5f3aeb0c344b09d8c35750
5
+ SHA512:
6
+ metadata.gz: 65419d317d7e51c7844ec856e81f185eb3643cf74d7b3afe7e2a23728516f3fb9b91ff1882ea21507876f5923c1dafe7e4247f621b32e6f910fe50b045cd609e
7
+ data.tar.gz: 7a8b5a9ed68d100e2953d2000fcfd375c8bca0f4ec8a1507b73b37dd397ff6cfbcd1a87b754bdbe8fd9126d6b4be6882aa6b5992cda874a00d8c38c9a81cf5f9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Fernando Mendes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,11 @@
1
+ (function() {
2
+ angular
3
+ .module('rails-template-cache', [])
4
+ .run(['$templateCache', function($templateCache) {
5
+ <% environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper } %>
6
+ <% RailsTemplateCache.templates.each do |key, template| %>
7
+ <% depend_on template.source_file %>
8
+ $templateCache.put("<%= key %>", "<%= escape_javascript(template.render) %>");
9
+ <% end %>
10
+ }])
11
+ })();
@@ -0,0 +1,27 @@
1
+ require "rails-template-cache/version"
2
+ require "rails-template-cache/template"
3
+ require "rails-template-cache/engine"
4
+
5
+ module RailsTemplateCache
6
+ mattr_accessor :templates
7
+ self.templates ||= {}
8
+
9
+ def self.setup(&block)
10
+ set_config
11
+ yield @@config.rails_template_cache if block
12
+ @@config.rails_template_cache
13
+ end
14
+
15
+ def self.config
16
+ Rails.application.config
17
+ end
18
+
19
+ private
20
+
21
+ def self.set_config
22
+ unless @config
23
+ @@config = RailsTemplateCache::Engine::Configuration.new
24
+ @@config.rails_template_cache = ActiveSupport::OrderedOptions.new
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ module RailsTemplateCache
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsTemplateCache
4
+
5
+ config.before_configuration do |app|
6
+ # Setting default options for the setup
7
+ RailsTemplateCache.setup do |rtc_config|
8
+ rtc_config.compress_html = false
9
+ rtc_config.templates_path = File.join( ['app', 'assets', 'javascripts'] )
10
+ rtc_config.extensions = %w(erb haml html slim)
11
+ end
12
+ end
13
+
14
+ initializer "rails-template-cache" do |app|
15
+ base = Rails.root.join(config.rails_template_cache.templates_path)
16
+ exts = "{#{ config.rails_template_cache.extensions.join(',') }}"
17
+
18
+ Dir.glob("#{base}/**/*.#{exts}").each do |f|
19
+ key, ext = RailsTemplateCache::Template
20
+ .asset_data(f, config.rails_template_cache.templates_path)
21
+
22
+ RailsTemplateCache.templates[key] = RailsTemplateCache::Template
23
+ .new(f, ext, compress: config.rails_template_cache.compress_html)
24
+ end
25
+
26
+ # Clear cache if config changes
27
+ app.config.assets.version = [
28
+ app.config.assets.version,
29
+ Digest::MD5.hexdigest("#{VERSION}-#{app.config.rails_template_cache}")
30
+ ].join('-')
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module RailsTemplateCache
2
+ class Template
3
+ attr_reader :source_file
4
+
5
+ def initialize(source_file, markup, options = {})
6
+ silence_warnings do
7
+ @template = Tilt[markup]
8
+ @source_file = source_file
9
+ @opts = options
10
+ end
11
+ end
12
+
13
+ def render
14
+ # Only render when needed, to avoid caching issues
15
+ rendered_template = @template.new(@source_file).render
16
+ @opts[:compress] ? compress_html(rendered_template) : rendered_template
17
+ end
18
+
19
+ def self.asset_data(path, templates_path = "")
20
+ result = []
21
+ case path.to_s
22
+ when /#{templates_path}\/(.*)(\.html)$/
23
+ result = "#{$1}#{$2}", $2
24
+ when /#{templates_path}\/(.*\.html)(\.\w+)$/
25
+ result = $1, $2
26
+ when /#{templates_path}\/(.*)(\.\w+)$/
27
+ result = "#{$1}.html", $2
28
+ end
29
+ result
30
+ end
31
+
32
+ private
33
+
34
+ def compress_html(html)
35
+ @compressor ||= HtmlCompressor::Compressor.new
36
+ @compressor.compress(html)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module RailsTemplateCache
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'rails_helper'
2
+
3
+ describe "RailsTemplateCache inclusion", :type => :feature do
4
+ before do
5
+ @files = RailsTemplateCache.templates.keys
6
+ visit '/assets/rails-template-cache/rails-template-cache.js'
7
+ end
8
+
9
+ it "adds templates to angular templateCache" do
10
+ @files.each do |f|
11
+ expect(page.source).to include("$templateCache.put(\"#{f}")
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "asset rendering", :type => :feature, :js => true do
17
+ before do
18
+ @files = RailsTemplateCache.templates.values.map do |f|
19
+ # Adding angular classes
20
+ f.render.gsub(/(<[^\/]\w+)>/, '\1 class="ng-scope">')
21
+ end
22
+ visit '/'
23
+ end
24
+
25
+ it "adds templates to page" do
26
+ @files.each do |f|
27
+ expect(page.source).to include(f)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV['RAILS_ENV'] ||= 'test'
3
+ require File.expand_path('../../test/dummy/config/environment', __FILE__)
4
+ # Prevent database truncation if the environment is production
5
+ abort("The Rails environment is running in production mode!") if Rails.env.production?
6
+ require 'spec_helper'
7
+ require 'rspec/rails'
8
+ # Add additional requires below this line. Rails is not loaded until this point!
9
+
10
+ # Requires supporting ruby files with custom matchers and macros, etc, in
11
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
12
+ # run as spec files by default. This means that files in spec/support that end
13
+ # in _spec.rb will both be required and run as specs, causing the specs to be
14
+ # run twice. It is recommended that you do not name files matching this glob to
15
+ # end with _spec.rb. You can configure this pattern with the --pattern
16
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
17
+ #
18
+ # The following line is provided for convenience purposes. It has the downside
19
+ # of increasing the boot-up time by auto-requiring all files in the support
20
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
21
+ # require only the support files necessary.
22
+ #
23
+ # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
24
+
25
+ RSpec.configure do |config|
26
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
27
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
28
+
29
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
30
+ # examples within a transaction, remove the following line or assign false
31
+ # instead of true.
32
+ config.use_transactional_fixtures = true
33
+
34
+ # RSpec Rails can automatically mix in different behaviours to your tests
35
+ # based on their file location, for example enabling you to call `get` and
36
+ # `post` in specs under `spec/controllers`.
37
+ #
38
+ # You can disable this behaviour by removing the line below, and instead
39
+ # explicitly tag your specs with their type, e.g.:
40
+ #
41
+ # RSpec.describe UsersController, :type => :controller do
42
+ # # ...
43
+ # end
44
+ #
45
+ # The different available types are documented in the features, such as in
46
+ # https://relishapp.com/rspec/rspec-rails/docs
47
+ config.infer_spec_type_from_file_location!
48
+ end
@@ -0,0 +1,17 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'capybara/rspec'
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ config.use_transactional_fixtures = true
15
+ config.infer_base_class_for_anonymous_controllers = false
16
+ config.order = "random"
17
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-template-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Mendes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.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: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: rspec-rails
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
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: capybara
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: selenium-webdriver
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - devfrmendes@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - app/assets/javascripts/rails-template-cache/application.js
120
+ - app/assets/javascripts/rails-template-cache/rails-template-cache.js.erb
121
+ - lib/rails-template-cache.rb
122
+ - lib/rails-template-cache/engine.rb
123
+ - lib/rails-template-cache/template.rb
124
+ - lib/rails-template-cache/version.rb
125
+ - spec/integration/assets_integration_spec.rb
126
+ - spec/rails_helper.rb
127
+ - spec/spec_helper.rb
128
+ homepage: https://github.com/frmendes/rails-template-cache
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Adding templates to Angular's templateCache with Rails.
152
+ test_files:
153
+ - spec/integration/assets_integration_spec.rb
154
+ - spec/rails_helper.rb
155
+ - spec/spec_helper.rb