Skip to content

Commit

Permalink
Generate correct default css path for namespaced classes.
Browse files Browse the repository at this point in the history
CSS background property is a shortcut for background-color.
After processing scss background automatically substitute with background-color.
That caused spec to file. Change background to background-color in tests and fixtures.
  • Loading branch information
ssemakov committed May 23, 2017
1 parent e84eb3d commit 125632f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 11 deletions.
11 changes: 9 additions & 2 deletions lib/inline_styles_mailer.rb
Expand Up @@ -23,9 +23,8 @@ def locate_template(name)
end

def css_content
@stylesheets ||= ["_#{self.name.underscore}*"]
@stylesheet_path ||= File.join("app", "assets", "stylesheets")
@css_content ||= @stylesheets.map {|stylesheet|
@css_content ||= stylesheets.map {|stylesheet|
Dir[Rails.root.join(@stylesheet_path, "#{stylesheet}")].map {|file|
case file
when /\.scss$/
Expand All @@ -41,6 +40,14 @@ def css_content
@css_content
end

def stylesheets
return @stylesheets if defined? @stylesheets

path = self.name.underscore
modified_path = path.gsub(/\//, '/_')
@stylesheets = [modified_path == path ? "_#{path}*" : "#{modified_path}*"]
end

def page
@page ||= InlineStyles::Page.new.with_css(css_content)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/assets/stylesheets/_foo_mailer.css.scss
@@ -1,5 +1,5 @@
body {
background: yellow;
background-color: yellow;
p {
color: red;
}
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/assets/stylesheets/_override.css
@@ -1,5 +1,5 @@
body {
background: yellow;
background-color: yellow;
}
body p {
color: blue;
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/assets/stylesheets/_override.css.sass
@@ -1,5 +1,5 @@
body
:background yellow
:background-color yellow
p
:color green

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/assets/stylesheets/_override.css.scss
@@ -1,5 +1,5 @@
body {
background: yellow;
background-color: yellow;
p {
color: orange;
}
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/assets/stylesheets/foo/_bar_mailer.css.scss
@@ -0,0 +1,6 @@
body {
background-color: orange;
p {
color: purple;
}
}
Empty file added spec/fixtures/mailers/Untitled
Empty file.
11 changes: 11 additions & 0 deletions spec/fixtures/mailers/foo/bar_mailer.rb
@@ -0,0 +1,11 @@
require 'rails/all'

module Foo
class BarMailer < ActionMailer::Base
include InlineStylesMailer

def bar
mail(:to => "test@localhost", :subject => "Test Bar")
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/views/foo/bar_mailer/bar.html.erb
@@ -0,0 +1 @@
<p>Testing foobar html.</p>
22 changes: 18 additions & 4 deletions spec/inline_styles_mailer/inline_styles_mailer_spec.rb
Expand Up @@ -18,7 +18,7 @@
mail = FooMailer.foo
mail.body.parts.length.should eq(2)
mail.body.parts[1].body.should =~ /<p style="color: red;">Testing foo text\/html\.<\/p>/
mail.body.parts[1].body.should =~ /<body style="background: yellow;">/
mail.body.parts[1].body.should =~ /<body style="background-color: yellow;">/
end
end

Expand All @@ -30,7 +30,7 @@
mail = FooMailer.foo
mail.body.parts.length.should eq(2)
mail.body.parts[1].body.should =~ /<p style="color: orange;">Testing foo text\/html\.<\/p>/
mail.body.parts[1].body.should =~ /<body style="background: yellow;">/
mail.body.parts[1].body.should =~ /<body style="background-color: yellow;">/
end
end

Expand All @@ -42,7 +42,7 @@
mail = FooMailer.foo
mail.body.parts.length.should eq(2)
mail.body.parts[1].body.should =~ /<p style="color: green;">Testing foo text\/html\.<\/p>/
mail.body.parts[1].body.should =~ /<body style="background: yellow;">/
mail.body.parts[1].body.should =~ /<body style="background-color: yellow;">/
end
end

Expand All @@ -54,7 +54,7 @@
mail = FooMailer.foo
mail.body.parts.length.should eq(2)
mail.body.parts[1].body.should =~ /<p style="color: blue;">Testing foo text\/html\.<\/p>/
mail.body.parts[1].body.should =~ /<body style="background: yellow;">/
mail.body.parts[1].body.should =~ /<body style="background-color: yellow;">/
end
end

Expand Down Expand Up @@ -82,4 +82,18 @@

end

describe "Namespaced mailers" do
context "Default CSS file" do
before do
Foo::BarMailer.stylesheet_path "assets/stylesheets"
end

it "should inline the CSS" do
mail = Foo::BarMailer.bar
mail.body.should =~ /<p style="color: purple;">Testing foobar html\.<\/p>/
mail.body.should =~ /<body style="background-color: orange;">/
end
end
end

end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -4,7 +4,7 @@
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.prepend_view_path File.join(File.dirname(__FILE__), "fixtures", "views")

Dir["#{File.dirname(__FILE__)}/fixtures/mailers/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/fixtures/mailers/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
config.expect_with :rspec do |c|
Expand Down

0 comments on commit 125632f

Please sign in to comment.