roadie 2.0.0 → 2.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples/asset_provider_examples'
3
+
4
+ module Roadie
5
+ describe AssetPipelineProvider do
6
+ let(:provider) { AssetPipelineProvider.new }
7
+
8
+ it_behaves_like AssetProvider
9
+
10
+ it "has a configurable prefix" do
11
+ AssetPipelineProvider.new("/prefix").prefix.should == "/prefix"
12
+ end
13
+
14
+ it 'has a prefix of "/assets" by default' do
15
+ provider.prefix.should == "/assets"
16
+ end
17
+
18
+ describe "#find(file)" do
19
+ let(:pipeline) { double("Rails asset pipeline") }
20
+ before(:each) { Roadie.app.stub(:assets => pipeline) }
21
+
22
+ def expect_pipeline_access(name, returning = '')
23
+ pipeline.should_receive(:[]).with(name).and_return(returning)
24
+ end
25
+
26
+ it "loads files matching the target names in Rails assets" do
27
+ expect_pipeline_access('foo', 'contents of foo')
28
+ expect_pipeline_access('foo.css', 'contents of foo.css')
29
+
30
+ provider.find('foo').should == 'contents of foo'
31
+ provider.find('foo.css').should == 'contents of foo.css'
32
+ end
33
+
34
+ it "strips the contents" do
35
+ expect_pipeline_access('foo', " contents \n ")
36
+ provider.find('foo').should == "contents"
37
+ end
38
+
39
+ it "removes the prefix from the filename" do
40
+ expect_pipeline_access('foo')
41
+ expect_pipeline_access('path/to/foo')
42
+ expect_pipeline_access('bar')
43
+
44
+ provider = AssetPipelineProvider.new("/prefix")
45
+ provider.find('/prefix/foo')
46
+ provider.find('/prefix/path/to/foo')
47
+ provider.find('prefix/bar')
48
+ end
49
+
50
+ it "cleans up double slashes from the path" do
51
+ expect_pipeline_access('path/to/foo')
52
+
53
+ provider = AssetPipelineProvider.new("/prefix/")
54
+ provider.find('/prefix/path/to//foo')
55
+ end
56
+
57
+ it "raises a Roadie::CSSFileNotFound error when the file could not be found" do
58
+ expect_pipeline_access('not_here', nil)
59
+ expect {
60
+ provider.find('not_here')
61
+ }.to raise_error(Roadie::CSSFileNotFound, /not_here/)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples/asset_provider_examples'
3
+ require 'tmpdir'
4
+
5
+ module Roadie
6
+ describe FilesystemProvider do
7
+ let(:provider) { FilesystemProvider.new }
8
+
9
+ it_behaves_like AssetProvider
10
+
11
+ it "has a configurable prefix" do
12
+ FilesystemProvider.new("/prefix").prefix.should == "/prefix"
13
+ end
14
+
15
+ it "has a configurable path" do
16
+ path = Pathname.new("/path")
17
+ FilesystemProvider.new('', path).path.should == path
18
+ end
19
+
20
+ it "bases the path on the project root if passed a string with a relative path" do
21
+ FilesystemProvider.new('', "foo/bar").path.should == Roadie.app.root.join("foo", "bar")
22
+ FilesystemProvider.new('', "/foo/bar").path.should == Pathname.new("/foo/bar")
23
+ end
24
+
25
+ it 'has a path of "<root>/public/stylesheets" by default' do
26
+ FilesystemProvider.new.path.should == Roadie.app.root.join('public', 'stylesheets')
27
+ end
28
+
29
+ it 'has a prefix of "/stylesheets" by default' do
30
+ FilesystemProvider.new.prefix.should == "/stylesheets"
31
+ end
32
+
33
+ describe "#find(file)" do
34
+ around(:each) do |example|
35
+ Dir.mktmpdir do |path|
36
+ Dir.chdir(path) { example.run }
37
+ end
38
+ end
39
+
40
+ let(:provider) { FilesystemProvider.new('/prefix', Pathname.new('.')) }
41
+
42
+ def create_file(name, contents = '')
43
+ Pathname.new(name).tap do |path|
44
+ path.dirname.mkpath unless path.dirname.directory?
45
+ path.open('w') { |file| file << contents }
46
+ end
47
+ end
48
+
49
+ it "loads files from the filesystem" do
50
+ create_file('foo.css', 'contents of foo.css')
51
+ provider.find('foo.css').should == 'contents of foo.css'
52
+ end
53
+
54
+ it "removes the prefix" do
55
+ create_file('foo.css', 'contents of foo.css')
56
+ provider.find('/prefix/foo.css').should == 'contents of foo.css'
57
+ provider.find('prefix/foo.css').should == 'contents of foo.css'
58
+ end
59
+
60
+ it 'tries the filename with a ".css" extension if it does not exist' do
61
+ create_file('bar', 'in bare bar')
62
+ create_file('bar.css', 'in bar.css')
63
+ create_file('foo.css', 'in foo.css')
64
+
65
+ provider.find('bar').should == 'in bare bar'
66
+ provider.find('foo').should == 'in foo.css'
67
+ end
68
+
69
+ it "strips the contents" do
70
+ create_file('foo.css', " contents \n ")
71
+ provider.find('foo.css').should == "contents"
72
+ end
73
+
74
+ it "supports nested directories" do
75
+ create_file('path/to/foo.css')
76
+ create_file('path/from/bar.css')
77
+
78
+ provider.find('path/to/foo.css')
79
+ provider.find('path/from/bar.css')
80
+ end
81
+
82
+ it "works with double slashes in the path" do
83
+ create_file('path/to/foo.css')
84
+ provider.find('path/to//foo.css')
85
+ end
86
+
87
+ it "raises a Roadie::CSSFileNotFound error when the file could not be found" do
88
+ expect {
89
+ provider.find('not_here.css')
90
+ }.to raise_error(Roadie::CSSFileNotFound, /not_here/)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -2,10 +2,23 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Roadie::Inliner do
5
- def use_css(css); @css = css; end
5
+ let(:provider) { double("asset provider", :all => '') }
6
+
7
+ def use_css(css)
8
+ provider.stub(:all).with(['global.css']).and_return(css)
9
+ end
10
+
6
11
  def rendering(html, options = {})
7
12
  url_options = options.fetch(:url_options, {:host => 'example.com'})
8
- Nokogiri::HTML.parse Roadie::Inliner.new(@css, html, url_options).execute
13
+ Nokogiri::HTML.parse Roadie::Inliner.new(provider, ['global.css'], html, url_options).execute
14
+ end
15
+
16
+ describe "initialization" do
17
+ it "warns about asset_path_prefix being non-functional" do
18
+ expect {
19
+ Roadie::Inliner.new(provider, [], '', :asset_path_prefix => 'foo')
20
+ }.to raise_error(ArgumentError, /asset_path_prefix/)
21
+ end
9
22
  end
10
23
 
11
24
  describe "inlining styles" do
@@ -149,7 +162,13 @@ describe Roadie::Inliner do
149
162
  end
150
163
 
151
164
  describe "linked stylesheets" do
165
+ def fake_file(name, contents)
166
+ provider.should_receive(:find).with(name).and_return(contents)
167
+ end
168
+
152
169
  it "inlines styles from the linked stylesheet" do
170
+ fake_file('/assets/green_paragraphs.css', 'p { color: green; }')
171
+
153
172
  rendering(<<-HTML).should have_styling('color' => 'green').at_selector('p')
154
173
  <html>
155
174
  <head>
@@ -163,6 +182,8 @@ describe Roadie::Inliner do
163
182
  end
164
183
 
165
184
  it "inlines styles from the linked stylesheet in subdirectory" do
185
+ fake_file('/assets/subdirectory/red_paragraphs.css', 'p { color: red; }')
186
+
166
187
  rendering(<<-HTML).should have_styling('color' => 'red').at_selector('p')
167
188
  <html>
168
189
  <head>
@@ -176,11 +197,14 @@ describe Roadie::Inliner do
176
197
  end
177
198
 
178
199
  it "inlines styles from more than one linked stylesheet" do
200
+ fake_file('/assets/large_purple_paragraphs.css', 'p { font-size: 18px; color: purple; }')
201
+ fake_file('/assets/green_paragraphs.css', 'p { color: green; }')
202
+
179
203
  html = <<-HTML
180
204
  <html>
181
205
  <head>
182
- <link rel="stylesheet" href="/assets/green_paragraphs.css">
183
206
  <link rel="stylesheet" href="/assets/large_purple_paragraphs.css">
207
+ <link rel="stylesheet" href="/assets/green_paragraphs.css">
184
208
  </head>
185
209
  <body>
186
210
  <p></p>
@@ -189,12 +213,13 @@ describe Roadie::Inliner do
189
213
  HTML
190
214
 
191
215
  rendering(html).should have_styling([
192
- ['color', 'purple'],
193
216
  ['font-size', '18px'],
217
+ ['color', 'green'],
194
218
  ]).at_selector('p')
195
219
  end
196
220
 
197
221
  it "removes the stylesheet links from the DOM" do
222
+ provider.stub(:find => '')
198
223
  rendering(<<-HTML).should_not have_selector('link')
199
224
  <html>
200
225
  <head>
@@ -302,7 +327,7 @@ describe Roadie::Inliner do
302
327
 
303
328
  expect { rendering(html) }.to raise_error do |error|
304
329
  error.should be_a(Roadie::CSSFileNotFound)
305
- error.filename.should == Roadie.assets['not_found.css']
330
+ error.filename.should == Roadie.app.assets['not_found.css']
306
331
  error.guess.should == '/assets/not_found.css'
307
332
  end
308
333
  end
@@ -8,31 +8,46 @@ describe Roadie do
8
8
  end
9
9
  end
10
10
 
11
- describe ".assets" do
12
- it "delegates to Rails.application.assets" do
13
- Rails.stub(:application => double(:assets => 'assets'))
14
- Roadie.assets.should == 'assets'
11
+ describe ".app" do
12
+ it "delegates to Rails.application" do
13
+ Rails.stub(:application => 'application')
14
+ Roadie.app.should == 'application'
15
15
  end
16
16
  end
17
17
 
18
- describe ".load_css(targets)" do
19
- it "loads files matching the target names in Rails assets" do
20
- Roadie.load_css(['foo']).should == 'contents of foo'
21
- Roadie.load_css(['foo.css']).should == 'contents of foo'
18
+ describe ".providers" do
19
+ it "returns an array of all provider classes" do
20
+ Roadie.should have(2).providers
21
+ Roadie.providers.should include(Roadie::AssetPipelineProvider, Roadie::FilesystemProvider)
22
22
  end
23
+ end
24
+
25
+ describe ".current_provider" do
26
+ let(:provider) { Object.new }
23
27
 
24
- it "loads files in order and join them with a newline" do
25
- Roadie.load_css(%w[foo bar]).should == "contents of foo\ncontents of bar"
26
- Roadie.load_css(%w[bar foo]).should == "contents of bar\ncontents of foo"
28
+ context "with a set provider in the config" do
29
+ it "uses the set provider" do
30
+ Roadie.app.config.roadie.provider = provider
31
+ Roadie.current_provider.should == provider
32
+ end
27
33
  end
28
34
 
29
- it "loads files also from asset subdirectories" do
30
- Roadie.load_css(%w[foo subdirectory/findme.css]).should == "contents of foo\ncan you really find me?"
31
- Roadie.load_css(%w[bar foo]).should == "contents of bar\ncontents of foo"
35
+ context "with rails' asset pipeline enabled" do
36
+ before(:each) { Roadie.app.config.assets.enabled = true }
37
+
38
+ it "uses the AssetPipelineProvider" do
39
+ Roadie::AssetPipelineProvider.should_receive(:new).and_return(provider)
40
+ Roadie.current_provider.should == provider
41
+ end
32
42
  end
33
43
 
34
- it "raises a Roadie::CSSFileNotFound error when a css file could not be found" do
35
- expect { Roadie.load_css(['not_here']) }.to raise_error(Roadie::CSSFileNotFound, /not_here/)
44
+ context "with rails' asset pipeline disabled" do
45
+ before(:each) { Roadie.app.config.assets.enabled = false }
46
+
47
+ it "uses the FilesystemProvider" do
48
+ Roadie::FilesystemProvider.should_receive(:new).and_return(provider)
49
+ Roadie.current_provider.should == provider
50
+ end
36
51
  end
37
52
  end
38
53
  end
@@ -0,0 +1,11 @@
1
+ shared_examples_for Roadie::AssetProvider do
2
+ describe "#all(files)" do
3
+ it "loads files in order and join them with a newline" do
4
+ provider.should_receive(:find).with('one').twice.and_return('contents of one')
5
+ provider.should_receive(:find).with('two').twice.and_return('contents of two')
6
+
7
+ provider.all(%w[one two]).should == "contents of one\ncontents of two"
8
+ provider.all(%w[two one]).should == "contents of two\ncontents of one"
9
+ end
10
+ end
11
+ end
@@ -13,27 +13,31 @@ rescue Bundler::BundlerError => e
13
13
  end
14
14
 
15
15
  require 'rspec'
16
-
17
- require 'action_mailer'
16
+ require 'rails'
18
17
  require 'sprockets'
19
18
  require 'roadie'
20
19
 
21
20
  FIXTURES_PATH = Pathname.new(File.dirname(__FILE__)).join('fixtures')
21
+ Roadie::Railtie.run_initializers
22
22
 
23
23
  class TestApplication
24
24
  def config
25
- OpenStruct.new(:action_mailer => OpenStruct.new(:default_url_options => {:host => "example.com"}))
25
+ @config ||= OpenStruct.new({
26
+ :action_mailer => OpenStruct.new(:default_url_options => {:host => "example.com"}),
27
+ :assets => OpenStruct.new(:enabled => false),
28
+ :roadie => OpenStruct.new(:provider => nil),
29
+ })
26
30
  end
27
31
 
28
32
  def assets
29
33
  env = Sprockets::Environment.new
30
- env.append_path FIXTURES_PATH.join('app','assets','stylesheets')
34
+ env.append_path root.join('app','assets','stylesheets')
31
35
  env
32
36
  end
33
- end
34
37
 
35
- unless defined?(Rails)
36
- class Rails; end
38
+ def root
39
+ FIXTURES_PATH
40
+ end
37
41
  end
38
42
 
39
43
  RSpec.configure do |c|
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roadie
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 2.0.0
4
+ prerelease: 6
5
+ version: 2.1.0.pre1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Magnus Bergmark
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-13 00:00:00 +02:00
13
+ date: 2011-11-24 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -58,16 +58,27 @@ dependencies:
58
58
  type: :runtime
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: rspec-rails
61
+ name: rails
62
62
  prerelease: false
63
63
  requirement: &id005 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 2.0.0
68
+ version: "0"
69
69
  type: :development
70
70
  version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec-rails
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.0.0
80
+ type: :development
81
+ version_requirements: *id006
71
82
  description: Roadie tries to make sending HTML emails a little less painful in Rails 3 by inlining stylesheets and rewrite relative URLs for you.
72
83
  email:
73
84
  - magnus.bergmark@gmail.com
@@ -92,27 +103,29 @@ files:
92
103
  - autotest/discover.rb
93
104
  - lib/roadie.rb
94
105
  - lib/roadie/action_mailer_extensions.rb
106
+ - lib/roadie/asset_pipeline_provider.rb
107
+ - lib/roadie/asset_provider.rb
95
108
  - lib/roadie/css_file_not_found.rb
109
+ - lib/roadie/filesystem_provider.rb
96
110
  - lib/roadie/inliner.rb
111
+ - lib/roadie/railtie.rb
97
112
  - lib/roadie/style_declaration.rb
98
113
  - lib/roadie/version.rb
99
114
  - roadie.gemspec
100
- - spec/fixtures/app/assets/stylesheets/bar.css
101
- - spec/fixtures/app/assets/stylesheets/foo.css
102
- - spec/fixtures/app/assets/stylesheets/green_paragraphs.css
103
115
  - spec/fixtures/app/assets/stylesheets/integration.css
104
- - spec/fixtures/app/assets/stylesheets/large_purple_paragraphs.css
105
- - spec/fixtures/app/assets/stylesheets/subdirectory/findme.css
106
- - spec/fixtures/app/assets/stylesheets/subdirectory/red_paragraphs.css
116
+ - spec/fixtures/public/stylesheets/integration.css
107
117
  - spec/fixtures/views/integration_mailer/marketing.html.erb
108
118
  - spec/fixtures/views/integration_mailer/notification.html.erb
109
119
  - spec/fixtures/views/integration_mailer/notification.text.erb
110
120
  - spec/integration_spec.rb
111
121
  - spec/lib/roadie/action_mailer_extensions_spec.rb
122
+ - spec/lib/roadie/asset_pipeline_provider_spec.rb
112
123
  - spec/lib/roadie/css_file_not_found_spec.rb
124
+ - spec/lib/roadie/filesystem_provider_spec.rb
113
125
  - spec/lib/roadie/inliner_spec.rb
114
126
  - spec/lib/roadie/style_declaration_spec.rb
115
127
  - spec/lib/roadie_spec.rb
128
+ - spec/shared_examples/asset_provider_examples.rb
116
129
  - spec/spec_helper.rb
117
130
  - spec/support/have_attribute_matcher.rb
118
131
  - spec/support/have_selector_matcher.rb
@@ -136,9 +149,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
149
  required_rubygems_version: !ruby/object:Gem::Requirement
137
150
  none: false
138
151
  requirements:
139
- - - ">="
152
+ - - ">"
140
153
  - !ruby/object:Gem::Version
141
- version: "0"
154
+ version: 1.3.1
142
155
  requirements: []
143
156
 
144
157
  rubyforge_project:
@@ -147,22 +160,20 @@ signing_key:
147
160
  specification_version: 3
148
161
  summary: Making HTML emails comfortable for the Rails rockstars
149
162
  test_files:
150
- - spec/fixtures/app/assets/stylesheets/bar.css
151
- - spec/fixtures/app/assets/stylesheets/foo.css
152
- - spec/fixtures/app/assets/stylesheets/green_paragraphs.css
153
163
  - spec/fixtures/app/assets/stylesheets/integration.css
154
- - spec/fixtures/app/assets/stylesheets/large_purple_paragraphs.css
155
- - spec/fixtures/app/assets/stylesheets/subdirectory/findme.css
156
- - spec/fixtures/app/assets/stylesheets/subdirectory/red_paragraphs.css
164
+ - spec/fixtures/public/stylesheets/integration.css
157
165
  - spec/fixtures/views/integration_mailer/marketing.html.erb
158
166
  - spec/fixtures/views/integration_mailer/notification.html.erb
159
167
  - spec/fixtures/views/integration_mailer/notification.text.erb
160
168
  - spec/integration_spec.rb
161
169
  - spec/lib/roadie/action_mailer_extensions_spec.rb
170
+ - spec/lib/roadie/asset_pipeline_provider_spec.rb
162
171
  - spec/lib/roadie/css_file_not_found_spec.rb
172
+ - spec/lib/roadie/filesystem_provider_spec.rb
163
173
  - spec/lib/roadie/inliner_spec.rb
164
174
  - spec/lib/roadie/style_declaration_spec.rb
165
175
  - spec/lib/roadie_spec.rb
176
+ - spec/shared_examples/asset_provider_examples.rb
166
177
  - spec/spec_helper.rb
167
178
  - spec/support/have_attribute_matcher.rb
168
179
  - spec/support/have_selector_matcher.rb