premailer-rails3 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## v1.1.0
4
+
5
+ - Fixed several bugs
6
+
7
+ - Strip asset digest from CSS path
8
+
9
+ - Improve nokogiri support
10
+
11
+ - Request CSS file if asset is not found locally
12
+
13
+ This allows you to host all your assets on a CDN and deploy the
14
+ app without the `app/assets` folder.
15
+
16
+ Thanks to everyone who contributed!
@@ -3,7 +3,7 @@ require 'premailer-rails3/css_helper'
3
3
  require 'premailer-rails3/premailer'
4
4
  require 'premailer-rails3/hook'
5
5
 
6
- module PremailerRails3
6
+ module PremailerRails
7
7
  @config = {}
8
8
  class << self
9
9
  attr_accessor :config
@@ -1,3 +1,6 @@
1
+ require 'open-uri'
2
+ require 'zlib'
3
+
1
4
  module PremailerRails
2
5
  module CSSHelper
3
6
  extend self
@@ -6,7 +9,7 @@ module PremailerRails
6
9
 
7
10
  def css_for_doc(doc)
8
11
  css = doc.search('link[@type="text/css"]').map { |link|
9
- url = link.attributes['href']
12
+ url = link.attributes['href'].to_s
10
13
  load_css_at_path(url) unless url.blank?
11
14
  }.reject(&:blank?).join("\n")
12
15
  css = load_css_at_path(:default) if css.blank?
@@ -20,7 +23,7 @@ module PremailerRails
20
23
  # Remove everything after ? including ?
21
24
  path = path[0..(path.index('?') - 1)] if path.include? '?'
22
25
  # Remove the host
23
- path = path.gsub(/^https?\:\/\/[^\/]*/, '') if path.index('http') == 0
26
+ path = path.sub(/^https?\:\/\/[^\/]*/, '') if path.index('http') == 0
24
27
  end
25
28
 
26
29
  # Don't cache in development.
@@ -33,12 +36,13 @@ module PremailerRails
33
36
  file = if path == :default
34
37
  'email.css'
35
38
  else
36
- path.sub("#{Rails.configuration.assets.prefix}/", '')
39
+ path.sub("#{Rails.configuration.assets.prefix}/", '') \
40
+ .sub(/-.*\.css$/, '.css')
37
41
  end
38
42
  if asset = Rails.application.assets.find_asset(file)
39
43
  asset.to_s
40
44
  else
41
- raise "Couldn't find asset #{file} for premailer-rails3."
45
+ request_and_unzip(file)
42
46
  end
43
47
  else
44
48
  file = path == :default ? '/stylesheets/email.css' : path
@@ -48,13 +52,28 @@ module PremailerRails
48
52
 
49
53
  @@css_cache[path]
50
54
  rescue => ex
51
- # Print an error and store empty string as the CSS.
55
+ # Print an error and return empty css:
52
56
  puts ex.message
53
- @@css_cache[path] = ''
57
+ ''
54
58
  end
55
59
 
56
60
  def assets_enabled?
57
61
  Rails.configuration.assets.enabled rescue false
58
62
  end
63
+
64
+ def request_and_unzip(file)
65
+ url = [
66
+ Rails.configuration.action_controller.asset_host,
67
+ Rails.configuration.assets.prefix.sub(/^\//, ''),
68
+ Rails.configuration.assets.digests[file]
69
+ ].join('/')
70
+ response = Kernel.open(url)
71
+ begin
72
+ Zlib::GzipReader.new(response).read
73
+ rescue Zlib::GzipFile::Error
74
+ response.rewind
75
+ response.read
76
+ end
77
+ end
59
78
  end
60
79
  end
@@ -11,7 +11,7 @@ module PremailerRails
11
11
  ::Premailer.send(:include, Adapter.find(Adapter.use))
12
12
  doc = load_html(html)
13
13
 
14
- options = PremailerRails3.config.merge(
14
+ options = PremailerRails.config.merge(
15
15
  :with_html_string => true,
16
16
  :css_string => CSSHelper.css_for_doc(doc)
17
17
  )
@@ -1,3 +1,3 @@
1
1
  module PremailerRails
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -116,7 +116,7 @@ describe PremailerRails::CSSHelper do
116
116
  it 'should load email.css when the default CSS is requested' do
117
117
  Rails.application.assets.expects(:find_asset) \
118
118
  .with('email.css') \
119
- .returns(mock(:body => 'content of default css'))
119
+ .returns(mock(:to_s => 'content of default css'))
120
120
 
121
121
  load_css_at_path(:default).should == 'content of default css'
122
122
  end
@@ -124,11 +124,60 @@ describe PremailerRails::CSSHelper do
124
124
  it 'should return the content of the file compiled by Rails' do
125
125
  Rails.application.assets.expects(:find_asset) \
126
126
  .with('base.css') \
127
- .returns(mock(:body => 'content of base.css'))
127
+ .returns(mock(:to_s => 'content of base.css'))
128
128
 
129
129
  load_css_at_path('http://example.com/assets/base.css') \
130
130
  .should == 'content of base.css'
131
131
  end
132
+
133
+ it 'should return same file when path contains file fingerprint' do
134
+ Rails.application.assets \
135
+ .expects(:find_asset) \
136
+ .with('base.css') \
137
+ .returns(mock(:to_s => 'content of base.css'))
138
+
139
+ load_css_at_path(
140
+ 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
141
+ ).should == 'content of base.css'
142
+ end
143
+
144
+ context 'when asset can not be found' do
145
+ before {
146
+ Rails.application.assets.stubs(:find_asset).returns(nil)
147
+ Rails.configuration.stubs(:action_controller).returns(
148
+ stub(:asset_host => 'http://example.com')
149
+ )
150
+ Rails.configuration.stubs(:assets).returns(
151
+ stub(
152
+ :enabled => true,
153
+ :prefix => '/assets',
154
+ :digests => {
155
+ 'base.css' => 'base-089e35bd5d84297b8d31ad552e433275.css'
156
+ }
157
+ )
158
+ )
159
+ }
160
+ let(:string_io) { StringIO.new('content of base.css') }
161
+ let(:url) {
162
+ 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
163
+ }
164
+
165
+ it 'should request the file' do
166
+ Kernel.expects(:open).with(url).returns(string_io)
167
+
168
+ load_css_at_path(
169
+ 'http://example.com/assets/base.css'
170
+ ).should == 'content of base.css'
171
+ end
172
+
173
+ it 'should request the same file when path contains file fingerprint' do
174
+ Kernel.expects(:open).with(url).returns(string_io)
175
+
176
+ load_css_at_path(
177
+ 'http://example.com/assets/base-089e35bd5d84297b8d31ad552e433275.css'
178
+ ).should == 'content of base.css'
179
+ end
180
+ end
132
181
  end
133
182
 
134
183
  context 'when static stylesheets are used' do
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe PremailerRails3 do
3
+ describe PremailerRails do
4
4
  describe '#config' do
5
- subject { PremailerRails3.config }
5
+ subject { PremailerRails.config }
6
6
  it { should == {} }
7
7
 
8
8
  context 'when set' do
9
- before { PremailerRails3.config = { :foo => :bar } }
9
+ before { PremailerRails.config = { :foo => :bar } }
10
10
  it { should == { :foo => :bar } }
11
11
  end
12
12
  end
@@ -33,7 +33,7 @@ describe PremailerRails::Premailer do
33
33
  end
34
34
 
35
35
  it 'should pass on the configs' do
36
- PremailerRails3.config = { :foo => :bar }
36
+ PremailerRails.config = { :foo => :bar }
37
37
  premailer = PremailerRails::Premailer.new('some html')
38
38
  premailer.instance_variable_get(:'@options')[:foo].should == :bar
39
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: premailer-rails3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-10 00:00:00.000000000Z
12
+ date: 2012-02-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: premailer
16
- requirement: &2166561480 !ruby/object:Gem::Requirement
16
+ requirement: &2151950140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.7'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2166561480
24
+ version_requirements: *2151950140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &2166560900 !ruby/object:Gem::Requirement
27
+ requirement: &2151949620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2166560900
35
+ version_requirements: *2151949620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-core
38
- requirement: &2166560420 !ruby/object:Gem::Requirement
38
+ requirement: &2151949240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2166560420
46
+ version_requirements: *2151949240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec-expectations
49
- requirement: &2166559940 !ruby/object:Gem::Requirement
49
+ requirement: &2151948780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2166559940
57
+ version_requirements: *2151948780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &2166559460 !ruby/object:Gem::Requirement
60
+ requirement: &2151948360 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2166559460
68
+ version_requirements: *2151948360
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mail
71
- requirement: &2166558980 !ruby/object:Gem::Requirement
71
+ requirement: &2151947940 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2166558980
79
+ version_requirements: *2151947940
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: nokogiri
82
- requirement: &2166558500 !ruby/object:Gem::Requirement
82
+ requirement: &2151947520 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2166558500
90
+ version_requirements: *2151947520
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: hpricot
93
- requirement: &2166558020 !ruby/object:Gem::Requirement
93
+ requirement: &2151947100 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2166558020
101
+ version_requirements: *2151947100
102
102
  description: ! "This gem brings you the power of the premailer gem to Rails 3\n without
103
103
  any configuration needs. Create HTML emails, include a\n CSS
104
104
  file as you do in a normal HTML document and premailer will\n inline
@@ -111,6 +111,7 @@ extra_rdoc_files: []
111
111
  files:
112
112
  - .gitignore
113
113
  - .rspec
114
+ - CHANGELOG.md
114
115
  - Gemfile
115
116
  - README.md
116
117
  - Rakefile