premailer-rails 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +64 -0
  3. data/CHANGELOG.md +10 -0
  4. data/Gemfile +13 -13
  5. data/README.md +11 -9
  6. data/VERSION +1 -1
  7. data/{example → examples/rails5}/.gitignore +0 -0
  8. data/examples/rails5/Gemfile +4 -0
  9. data/{example → examples/rails5}/README.md +0 -0
  10. data/{example → examples/rails5}/Rakefile +1 -1
  11. data/{example → examples/rails5}/app/assets/stylesheets/email.css +0 -0
  12. data/{example → examples/rails5}/app/mailers/example_mailer.rb +0 -0
  13. data/{example → examples/rails5}/app/views/example_mailer/test_message.html.erb +0 -0
  14. data/{example → examples/rails5}/bin/rails +1 -1
  15. data/{example → examples/rails5}/config/application.rb +4 -1
  16. data/examples/rails5/config/boot.rb +3 -0
  17. data/{example → examples/rails5}/config/environment.rb +1 -1
  18. data/{example → examples/rails5}/config/environments/development.rb +0 -0
  19. data/{example → examples/rails5}/config/environments/production.rb +0 -0
  20. data/{example → examples/rails5}/config/initializers/assets.rb +0 -0
  21. data/{example → examples/rails5}/config/routes.rb +0 -0
  22. data/{example → examples/rails5}/config.ru +2 -1
  23. data/{example → examples/rails5}/test/mailers/previews/example_mailer_preview.rb +0 -0
  24. data/examples/rails6/.gitignore +16 -0
  25. data/examples/rails6/Gemfile +5 -0
  26. data/examples/rails6/README.md +10 -0
  27. data/examples/rails6/Rakefile +6 -0
  28. data/examples/rails6/app/assets/config/manifest.js +1 -0
  29. data/examples/rails6/app/assets/stylesheets/email.css +32 -0
  30. data/examples/rails6/app/mailers/example_mailer.rb +7 -0
  31. data/examples/rails6/app/views/example_mailer/test_message.html.erb +18 -0
  32. data/examples/rails6/bin/rails +4 -0
  33. data/examples/rails6/config/application.rb +15 -0
  34. data/examples/rails6/config/boot.rb +3 -0
  35. data/examples/rails6/config/environment.rb +5 -0
  36. data/examples/rails6/config/environments/development.rb +9 -0
  37. data/examples/rails6/config/environments/production.rb +9 -0
  38. data/examples/rails6/config/routes.rb +3 -0
  39. data/examples/rails6/config.ru +6 -0
  40. data/examples/rails6/test/mailers/previews/example_mailer_preview.rb +5 -0
  41. data/examples/rails7-propshaft/.gitignore +16 -0
  42. data/examples/rails7-propshaft/Gemfile +6 -0
  43. data/examples/rails7-propshaft/README.md +10 -0
  44. data/examples/rails7-propshaft/Rakefile +6 -0
  45. data/examples/rails7-propshaft/app/assets/stylesheets/email.css +32 -0
  46. data/examples/rails7-propshaft/app/mailers/example_mailer.rb +7 -0
  47. data/examples/rails7-propshaft/app/views/example_mailer/test_message.html.erb +18 -0
  48. data/examples/rails7-propshaft/bin/rails +4 -0
  49. data/examples/rails7-propshaft/config/application.rb +14 -0
  50. data/examples/rails7-propshaft/config/boot.rb +3 -0
  51. data/examples/rails7-propshaft/config/environment.rb +5 -0
  52. data/examples/rails7-propshaft/config/environments/development.rb +9 -0
  53. data/examples/rails7-propshaft/config/environments/production.rb +9 -0
  54. data/examples/rails7-propshaft/config/routes.rb +3 -0
  55. data/examples/rails7-propshaft/config.ru +6 -0
  56. data/examples/rails7-propshaft/test/mailers/previews/example_mailer_preview.rb +5 -0
  57. data/examples/rails7-sprockets/.gitignore +16 -0
  58. data/examples/rails7-sprockets/Gemfile +6 -0
  59. data/examples/rails7-sprockets/README.md +10 -0
  60. data/examples/rails7-sprockets/Rakefile +6 -0
  61. data/examples/rails7-sprockets/app/assets/config/manifest.js +1 -0
  62. data/examples/rails7-sprockets/app/assets/stylesheets/email.css +32 -0
  63. data/examples/rails7-sprockets/app/mailers/example_mailer.rb +7 -0
  64. data/examples/rails7-sprockets/app/views/example_mailer/test_message.html.erb +18 -0
  65. data/examples/rails7-sprockets/bin/rails +4 -0
  66. data/examples/rails7-sprockets/config/application.rb +14 -0
  67. data/examples/rails7-sprockets/config/boot.rb +3 -0
  68. data/examples/rails7-sprockets/config/environment.rb +5 -0
  69. data/examples/rails7-sprockets/config/environments/development.rb +9 -0
  70. data/examples/rails7-sprockets/config/environments/production.rb +9 -0
  71. data/examples/rails7-sprockets/config/routes.rb +3 -0
  72. data/examples/rails7-sprockets/config.ru +6 -0
  73. data/examples/rails7-sprockets/test/mailers/previews/example_mailer_preview.rb +5 -0
  74. data/lib/premailer/rails/css_helper.rb +2 -0
  75. data/lib/premailer/rails/css_loaders/asset_pipeline_loader.rb +5 -4
  76. data/lib/premailer/rails/css_loaders/network_loader.rb +1 -1
  77. data/lib/premailer/rails/css_loaders/propshaft_loader.rb +38 -0
  78. data/lib/premailer/rails/css_loaders.rb +1 -0
  79. data/lib/premailer/rails/railtie.rb +1 -1
  80. data/lib/premailer/rails.rb +1 -1
  81. data/premailer-rails.gemspec +3 -1
  82. data/spec/integration/css_helper_spec.rb +173 -71
  83. data/spec/rails_app/config/application.rb +7 -1
  84. data/spec/spec_helper.rb +0 -13
  85. data/spec/unit/css_loaders/asset_pipeline_loader_spec.rb +44 -32
  86. data/spec/unit/css_loaders/propshaft_loader_spec.rb +57 -0
  87. metadata +91 -56
  88. data/.coveralls.yml +0 -1
  89. data/.travis.yml +0 -16
  90. data/example/Gemfile +0 -10
  91. data/example/config/boot.rb +0 -3
  92. data/example/config/secrets.yml +0 -2
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 7.0.4'
4
+ gem 'sprockets-rails'
5
+ gem 'webrick' if RUBY_VERSION >= '3'
6
+ gem 'premailer-rails', path: '../..'
@@ -0,0 +1,10 @@
1
+ # Example Rails App
2
+
3
+ To run this app, run:
4
+
5
+ ```shell
6
+ bundle
7
+ bundle exec rails s
8
+ ```
9
+
10
+ Then point your browser at [http://localhost:3000/](http://localhost:3000/).
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1 @@
1
+ //= link email.css
@@ -0,0 +1,32 @@
1
+ body {
2
+ background: #efefef;
3
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
4
+ }
5
+
6
+ p {
7
+ line-height: 1.4;
8
+ }
9
+
10
+ .wrap {
11
+ max-width: 40em;
12
+ margin: 0 auto;
13
+ padding: 1em;
14
+ background: white;
15
+ }
16
+
17
+ .greeting {
18
+ text-align: center;
19
+ font-weight: bold;
20
+ font-size: 110%;
21
+ }
22
+
23
+ .footer {
24
+ font-size: 90%;
25
+ color: #666;
26
+ }
27
+
28
+ a {
29
+ color: green;
30
+ text-decoration: none;
31
+ border-bottom: 2px solid green;
32
+ }
@@ -0,0 +1,7 @@
1
+ class ExampleMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+
4
+ def test_message
5
+ mail to: 'to@example.org', subject: 'Test Message'
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <%= stylesheet_link_tag :email %>
6
+ </head>
7
+ <body>
8
+ <div class='wrap'>
9
+ <p class='greeting'>Hi, John Doe</p>
10
+ <p>
11
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
12
+ </p>
13
+ <div class='footer'>
14
+ To unsubscribe, <a href='http://www.google.com/'>click here</a>.
15
+ </div>
16
+ </div>
17
+ </body>
18
+ </html>
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path("../config/application", __dir__)
3
+ require_relative "../config/boot"
4
+ require "rails/commands"
@@ -0,0 +1,14 @@
1
+ require_relative 'boot'
2
+
3
+ require 'action_controller/railtie'
4
+ require 'action_mailer/railtie'
5
+
6
+ # Require the gems listed in Gemfile, including any gems
7
+ # you've limited to :test, :development, or :production.
8
+ Bundler.require(*Rails.groups)
9
+
10
+ module Example
11
+ class Application < Rails::Application
12
+ config.load_defaults 7.0
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
2
+
3
+ require 'bundler/setup' # Set up gems listed in the Gemfile.
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require_relative 'application'
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,9 @@
1
+ Rails.application.configure do
2
+ config.cache_classes = false
3
+ config.eager_load = false
4
+ config.consider_all_requests_local = true
5
+ config.action_controller.perform_caching = false
6
+ config.assets.debug = true
7
+ config.assets.digest = true
8
+ config.assets.raise_runtime_errors = true
9
+ end
@@ -0,0 +1,9 @@
1
+ Rails.application.configure do
2
+ config.cache_classes = true
3
+ config.eager_load = true
4
+ config.consider_all_requests_local = false
5
+ config.action_controller.perform_caching = true
6
+ config.assets.compile = false
7
+ config.assets.digest = true
8
+ config.log_level = :info
9
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ root to: redirect('rails/mailers/example_mailer/test_message')
3
+ end
@@ -0,0 +1,6 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
6
+ Rails.application.load_server
@@ -0,0 +1,5 @@
1
+ class ExampleMailerPreview < ActionMailer::Preview
2
+ def test_message
3
+ ExampleMailer.test_message
4
+ end
5
+ end
@@ -57,6 +57,8 @@ class Premailer
57
57
  CSSLoaders::FileSystemLoader
58
58
  when :asset_pipeline
59
59
  CSSLoaders::AssetPipelineLoader
60
+ when :propshaft
61
+ CSSLoaders::PropshaftLoader
60
62
  when :network
61
63
  CSSLoaders::NetworkLoader
62
64
  else
@@ -13,11 +13,11 @@ class Premailer
13
13
  end
14
14
 
15
15
  def file_name(url)
16
- prefix = [
17
- ::Rails.configuration.relative_url_root,
18
- ::Rails.configuration.assets.prefix,
16
+ prefix = File.join(
17
+ ::Rails.configuration.relative_url_root.to_s,
18
+ ::Rails.configuration.assets.prefix.to_s,
19
19
  '/'
20
- ].join
20
+ )
21
21
  URI(url).path
22
22
  .sub(/\A#{prefix}/, '')
23
23
  .sub(/-(\h{32}|\h{64})\.css\z/, '.css')
@@ -25,6 +25,7 @@ class Premailer
25
25
 
26
26
  def asset_pipeline_present?
27
27
  defined?(::Rails) &&
28
+ ::Rails.respond_to?(:application) &&
28
29
  ::Rails.application &&
29
30
  ::Rails.application.respond_to?(:assets_manifest) &&
30
31
  ::Rails.application.assets_manifest
@@ -6,7 +6,7 @@ class Premailer
6
6
 
7
7
  def load(url)
8
8
  uri = uri_for_url(url)
9
- Net::HTTP.get(uri) if uri
9
+ Net::HTTP.get(uri, { 'Accept' => 'text/css' }) if uri
10
10
  end
11
11
 
12
12
  def uri_for_url(url)
@@ -0,0 +1,38 @@
1
+ class Premailer
2
+ module Rails
3
+ module CSSLoaders
4
+ module PropshaftLoader
5
+ extend self
6
+
7
+ def load(url)
8
+ return unless propshaft_present?
9
+
10
+ file = file_name(url)
11
+ asset = ::Rails.application.assets.load_path.find(file)
12
+
13
+ ::Rails.application.assets.compilers.compile(asset) if asset
14
+ end
15
+
16
+ def file_name(url)
17
+ prefix = File.join(
18
+ ::Rails.configuration.relative_url_root.to_s,
19
+ ::Rails.configuration.assets.prefix.to_s,
20
+ '/'
21
+ )
22
+
23
+ # Path extraction logic from Propshaft.
24
+ # See https://github.com/rails/propshaft/blob/390381548b125e8721c8aef1b9d894b7cc8bd868/lib/propshaft/server.rb#L35-L41
25
+ full_path = URI(url).path.sub(/\A#{prefix}/, '')
26
+ digest = full_path[/-([0-9a-zA-Z]{7,128}(?:\.digested)?)\.[^.]+\z/, 1]
27
+ path = digest ? full_path.sub("-#{digest}", "") : full_path
28
+
29
+ path
30
+ end
31
+
32
+ def propshaft_present?
33
+ defined?(::Propshaft) && defined?(::Rails)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -2,4 +2,5 @@ require 'uri'
2
2
 
3
3
  require 'premailer/rails/css_loaders/file_system_loader'
4
4
  require 'premailer/rails/css_loaders/asset_pipeline_loader'
5
+ require 'premailer/rails/css_loaders/propshaft_loader'
5
6
  require 'premailer/rails/css_loaders/network_loader'
@@ -1,7 +1,7 @@
1
1
  class Premailer
2
2
  module Rails
3
3
  class Railtie < ::Rails::Railtie
4
- config.after_initialize do
4
+ ActiveSupport.on_load(:action_mailer) do
5
5
  ::Premailer::Rails.register_interceptors
6
6
  end
7
7
  end
@@ -12,7 +12,7 @@ class Premailer
12
12
  @config = {
13
13
  input_encoding: 'UTF-8',
14
14
  generate_text_part: true,
15
- strategies: [:filesystem, :asset_pipeline, :network]
15
+ strategies: [:filesystem, :asset_pipeline, :propshaft, :network]
16
16
  }
17
17
  class << self
18
18
  attr_accessor :config
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
16
16
  include a CSS file as you do in a normal HTML document and
17
17
  premailer will inline the included CSS.}
18
18
 
19
+ s.metadata["changelog_uri"] = "https://github.com/fphilipe/premailer-rails/blob/v#{Premailer::Rails::VERSION}/CHANGELOG.md"
20
+
19
21
  s.files = `git ls-files`.split("\n")
20
22
  s.test_files = `git ls-files -- {example,spec}/*`.split("\n")
21
23
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -23,8 +25,8 @@ Gem::Specification.new do |s|
23
25
 
24
26
  s.add_dependency 'premailer', '~> 1.7', '>= 1.7.9'
25
27
  s.add_dependency 'actionmailer', '>= 3'
28
+ s.add_dependency 'net-smtp' if RUBY_VERSION >= '3'
26
29
 
27
30
  s.add_development_dependency 'rspec', '~> 3.3'
28
31
  s.add_development_dependency 'nokogiri'
29
- s.add_development_dependency 'coveralls' if RUBY_ENGINE == 'ruby'
30
32
  end
@@ -96,111 +96,213 @@ describe Premailer::Rails::CSSHelper do
96
96
  end
97
97
  end
98
98
 
99
- context 'when Rails asset pipeline is used' do
100
- before do
101
- allow(Rails.configuration)
102
- .to receive(:assets).and_return(double(prefix: '/assets'))
103
- allow(Rails.configuration)
104
- .to receive(:relative_url_root).and_return(nil)
105
- end
99
+ if defined?(::Sprockets)
100
+ context 'when Rails asset pipeline is used' do
101
+ before do
102
+ allow(Rails.configuration)
103
+ .to receive(:assets).and_return(double(prefix: '/assets'))
104
+ allow(Rails.configuration)
105
+ .to receive(:relative_url_root).and_return(nil)
106
+ end
106
107
 
107
- context 'and a precompiled file exists' do
108
- it 'returns that file' do
109
- path = '/assets/email-digest.css'
110
- content = 'read from file'
111
- expect_file("public#{path}", content)
112
- expect(css_for_url(path)).to eq(content)
108
+ context 'and a precompiled file exists' do
109
+ it 'returns that file' do
110
+ path = '/assets/email-digest.css'
111
+ content = 'read from file'
112
+ expect_file("public#{path}", content)
113
+ expect(css_for_url(path)).to eq(content)
114
+ end
113
115
  end
114
- end
115
116
 
116
- context "when find_sources raises TypeError" do
117
- let(:response) { 'content of base.css' }
118
- let(:uri) { URI('http://example.com/assets/base.css') }
117
+ context "when find_sources raises TypeError" do
118
+ let(:response) { 'content of base.css' }
119
+ let(:uri) { URI('http://example.com/assets/base.css') }
120
+
121
+ it "falls back to Net::HTTP" do
122
+ expect(Rails.application.assets_manifest).to \
123
+ receive(:find_sources)
124
+ .with('base.css')
125
+ .and_raise(TypeError)
126
+
127
+ allow(Net::HTTP).to \
128
+ receive(:get)
129
+ .with(uri, { 'Accept' => 'text/css' })
130
+ .and_return(response)
131
+ expect(css_for_url('http://example.com/assets/base.css')).to \
132
+ eq(response)
133
+ end
134
+ end
135
+
136
+ context "when find_sources raises Errno::ENOENT" do
137
+ let(:response) { 'content of base.css' }
138
+ let(:uri) { URI('http://example.com/assets/base.css') }
139
+
140
+ it "falls back to Net::HTTP" do
141
+ expect(Rails.application.assets_manifest).to \
142
+ receive(:find_sources)
143
+ .with('base.css')
144
+ .and_raise(Errno::ENOENT)
145
+
146
+ allow(Net::HTTP).to \
147
+ receive(:get)
148
+ .with(uri, { 'Accept' => 'text/css' })
149
+ .and_return(response)
150
+ expect(css_for_url('http://example.com/assets/base.css')).to \
151
+ eq(response)
152
+ end
153
+ end
119
154
 
120
- it "falls back to Net::HTTP" do
155
+ it 'returns the content of the file compiled by Rails' do
121
156
  expect(Rails.application.assets_manifest).to \
122
157
  receive(:find_sources)
123
158
  .with('base.css')
124
- .and_raise(TypeError)
159
+ .and_return(['content of base.css'])
125
160
 
126
- allow(Net::HTTP).to \
127
- receive(:get).with(uri).and_return(response)
128
161
  expect(css_for_url('http://example.com/assets/base.css')).to \
129
- eq(response)
162
+ eq('content of base.css')
130
163
  end
131
- end
132
-
133
- context "when find_sources raises Errno::ENOENT" do
134
- let(:response) { 'content of base.css' }
135
- let(:uri) { URI('http://example.com/assets/base.css') }
136
164
 
137
- it "falls back to Net::HTTP" do
165
+ it 'returns same file when path contains file fingerprint' do
138
166
  expect(Rails.application.assets_manifest).to \
139
167
  receive(:find_sources)
140
168
  .with('base.css')
141
- .and_raise(Errno::ENOENT)
169
+ .and_return(['content of base.css'])
142
170
 
143
- allow(Net::HTTP).to \
144
- receive(:get).with(uri).and_return(response)
145
- expect(css_for_url('http://example.com/assets/base.css')).to \
146
- eq(response)
171
+ expect(css_for_url(
172
+ 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
173
+ )).to eq('content of base.css')
147
174
  end
148
- end
149
175
 
150
- it 'returns the content of the file compiled by Rails' do
151
- expect(Rails.application.assets_manifest).to \
152
- receive(:find_sources)
153
- .with('base.css')
154
- .and_return(['content of base.css'])
176
+ context 'when asset can not be found' do
177
+ let(:response) { 'content of base.css' }
178
+ let(:path) { '/assets/base-089e35bd5d84297b8d31ad552e433275.css' }
179
+ let(:url) { "http://assets.example.com#{path}" }
180
+ let(:asset_host) { 'http://assets.example.com' }
155
181
 
156
- expect(css_for_url('http://example.com/assets/base.css')).to \
157
- eq('content of base.css')
158
- end
182
+ before do
183
+ allow(Rails.application.assets_manifest).to \
184
+ receive(:find_sources).and_return([])
159
185
 
160
- it 'returns same file when path contains file fingerprint' do
161
- expect(Rails.application.assets_manifest).to \
162
- receive(:find_sources)
163
- .with('base.css')
164
- .and_return(['content of base.css'])
186
+ config = double(asset_host: asset_host)
187
+ allow(Rails.configuration).to \
188
+ receive(:action_controller).and_return(config)
165
189
 
166
- expect(css_for_url(
167
- 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
168
- )).to eq('content of base.css')
169
- end
190
+ allow(Net::HTTP).to \
191
+ receive(:get)
192
+ .with(URI(url), { 'Accept' => 'text/css' })
193
+ .and_return(response)
194
+ end
195
+
196
+ it 'requests the file' do
197
+ expect(css_for_url(url)).to eq('content of base.css')
198
+ end
170
199
 
171
- context 'when asset can not be found' do
172
- let(:response) { 'content of base.css' }
173
- let(:path) { '/assets/base-089e35bd5d84297b8d31ad552e433275.css' }
174
- let(:url) { "http://assets.example.com#{path}" }
175
- let(:asset_host) { 'http://assets.example.com' }
200
+ context 'when file url does not include the host' do
201
+ it 'requests the file using the asset host as host' do
202
+ expect(css_for_url(path)).to eq('content of base.css')
203
+ end
176
204
 
205
+ context 'and the asset host uses protocol relative scheme' do
206
+ let(:asset_host) { '//assets.example.com' }
207
+
208
+ it 'requests the file using http as the scheme' do
209
+ expect(css_for_url(path)).to eq('content of base.css')
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ elsif defined?(::Propshaft)
216
+ context 'when Propshaft is used' do
177
217
  before do
178
- allow(Rails.application.assets_manifest).to \
179
- receive(:find_sources).and_return([])
218
+ allow(Rails.configuration)
219
+ .to receive(:assets).and_return(double(prefix: '/assets'))
220
+ allow(Rails.configuration)
221
+ .to receive(:relative_url_root).and_return(nil)
222
+ end
223
+
224
+ context 'and a precompiled file exists' do
225
+ it 'returns that file' do
226
+ path = '/assets/email-digest.css'
227
+ content = 'read from file'
228
+ expect_file("public#{path}", content)
229
+ expect(css_for_url(path)).to eq(content)
230
+ end
231
+ end
232
+
233
+ it 'returns the content of the file compiled by Propshaft' do
234
+ asset = double()
235
+
236
+ expect(Rails.application.assets.load_path).to \
237
+ receive(:find)
238
+ .with('base.css')
239
+ .and_return(asset)
180
240
 
181
- config = double(asset_host: asset_host)
182
- allow(Rails.configuration).to \
183
- receive(:action_controller).and_return(config)
241
+ expect(Rails.application.assets.compilers).to \
242
+ receive(:compile)
243
+ .with(asset)
244
+ .and_return('content of base.css')
184
245
 
185
- allow(Net::HTTP).to \
186
- receive(:get).with(URI(url)).and_return(response)
246
+ expect(css_for_url('http://example.com/assets/base.css')).to \
247
+ eq('content of base.css')
187
248
  end
188
249
 
189
- it 'requests the file' do
190
- expect(css_for_url(url)).to eq('content of base.css')
250
+ it 'returns same file when path contains file fingerprint' do
251
+ asset = double()
252
+
253
+ expect(Rails.application.assets.load_path).to \
254
+ receive(:find)
255
+ .with('base.css')
256
+ .and_return(asset)
257
+
258
+ expect(Rails.application.assets.compilers).to \
259
+ receive(:compile)
260
+ .with(asset)
261
+ .and_return('content of base.css')
262
+
263
+ expect(css_for_url(
264
+ 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
265
+ )).to eq('content of base.css')
191
266
  end
192
267
 
193
- context 'when file url does not include the host' do
194
- it 'requests the file using the asset host as host' do
195
- expect(css_for_url(path)).to eq('content of base.css')
268
+ context 'when asset can not be found' do
269
+ let(:response) { 'content of base.css' }
270
+ let(:path) { '/assets/base-089e35bd5d84297b8d31ad552e433275.css' }
271
+ let(:url) { "http://assets.example.com#{path}" }
272
+ let(:asset_host) { 'http://assets.example.com' }
273
+
274
+ before do
275
+ expect(Rails.application.assets.load_path).to \
276
+ receive(:find)
277
+ .with('base.css')
278
+ .and_return(nil)
279
+
280
+ config = double(asset_host: asset_host)
281
+ allow(Rails.configuration).to \
282
+ receive(:action_controller).and_return(config)
283
+
284
+ allow(Net::HTTP).to \
285
+ receive(:get)
286
+ .with(URI(url), { 'Accept' => 'text/css' })
287
+ .and_return(response)
196
288
  end
197
289
 
198
- context 'and the asset host uses protocol relative scheme' do
199
- let(:asset_host) { '//assets.example.com' }
290
+ it 'requests the file' do
291
+ expect(css_for_url(url)).to eq('content of base.css')
292
+ end
200
293
 
201
- it 'requests the file using http as the scheme' do
294
+ context 'when file url does not include the host' do
295
+ it 'requests the file using the asset host as host' do
202
296
  expect(css_for_url(path)).to eq('content of base.css')
203
297
  end
298
+
299
+ context 'and the asset host uses protocol relative scheme' do
300
+ let(:asset_host) { '//assets.example.com' }
301
+
302
+ it 'requests the file using http as the scheme' do
303
+ expect(css_for_url(path)).to eq('content of base.css')
304
+ end
305
+ end
204
306
  end
205
307
  end
206
308
  end
@@ -1,8 +1,14 @@
1
1
  require_relative 'boot'
2
2
 
3
+ require "action_controller/railtie"
3
4
  require "action_mailer/railtie"
4
5
  require "action_view/railtie"
5
- require "sprockets/railtie"
6
+
7
+ begin
8
+ require "sprockets/railtie"
9
+ rescue LoadError
10
+ end
11
+
6
12
  require "rails/test_unit/railtie"
7
13
 
8
14
  Bundler.require(*Rails.groups)
data/spec/spec_helper.rb CHANGED
@@ -1,16 +1,3 @@
1
- if RUBY_ENGINE == 'ruby'
2
- if ENV['CI']
3
- require 'coveralls'
4
- Coveralls::Output.silent = true
5
- Coveralls.wear! do
6
- add_filter 'spec/'
7
- end
8
- else
9
- require 'simplecov'
10
- SimpleCov.start
11
- end
12
- end
13
-
14
1
  # Configure Rails Environment
15
2
  ENV["RAILS_ENV"] = "test"
16
3
  require File.expand_path("../../spec/rails_app/config/environment.rb", __FILE__)