-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle asset_host #30
base: master
Are you sure you want to change the base?
Changes from all commits
4107951
2f6f230
45696cc
dbdb132
d206c2c
b46da40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Roadie | ||
module Rails | ||
module CssLocalizer | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
old_asset_host = self.asset_host | ||
if old_asset_host | ||
# don't include an asset_host for CSS files, so that they are picked up by Roadie | ||
self.asset_host = Proc.new { |source, request| | ||
uri = URI::parse(source) | ||
if uri.path.end_with?('.css') | ||
nil | ||
else | ||
# logic copied from Rails | ||
# http://git.io/vhYx7Q | ||
if old_asset_host.respond_to?(:call) | ||
# the original asset_host was a Proc | ||
arity = old_asset_host.respond_to?(:arity) ? old_asset_host.arity : old_asset_host.method(:call).arity | ||
args = [source] | ||
args << request if request && (arity > 1 || arity < 0) | ||
old_asset_host.call(*args) | ||
elsif old_asset_host =~ /%d/ | ||
# the original asset_host was a string value that needs the filename checksum added | ||
old_asset_host % (Zlib.crc32(source) % 4) | ||
else | ||
# the original asset_host was a simple string value | ||
old_asset_host | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be a way to re-use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this part needs improvement. I might be able to take a stab at it if you don't have any ideas. My initial thought is to move all of this into a new class (so the replacement |
||
end | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
|
||
module Roadie | ||
module Rails | ||
describe CssLocalizer do | ||
base_mailer = Class.new do | ||
cattr_accessor :asset_host | ||
end | ||
|
||
describe "#asset_host" do | ||
it "passes all arguments to the underlying simple accessor" do | ||
string_host_mailer = Class.new(base_mailer) do | ||
self.asset_host = 'http://original.com' | ||
|
||
include CssLocalizer | ||
end | ||
|
||
asset_host = string_host_mailer.asset_host | ||
expect(asset_host).to be_a(Proc) | ||
expect(asset_host.call('foo.png')).to eq('http://original.com') | ||
end | ||
|
||
it "passes all arguments to the underlying proc accessor" do | ||
proc_host_mailer = Class.new(base_mailer) do | ||
self.asset_host = Proc.new { |source, request| | ||
'http://original.com' | ||
} | ||
|
||
include CssLocalizer | ||
end | ||
|
||
asset_host = proc_host_mailer.asset_host | ||
expect(asset_host).to be_a(Proc) | ||
expect(asset_host.call('foo.png')).to eq('http://original.com') | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class AutoMailer < ActionMailer::Base | ||
self.asset_host = 'http://asset.host.com' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't think of a good way to do integration tests for a string and a proc for each type of mixin... open to ideas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's okay to have it being different types in the different mailers. As long as both paths have been crossed and we have some unit tests I think we're good. |
||
|
||
include Roadie::Rails::Automatic | ||
|
||
default from: 'john@example.com' | ||
|
@@ -7,6 +9,10 @@ def normal_email | |
generate_email | ||
end | ||
|
||
def asset_email | ||
mail(to: 'example@example.org', subject: "Notification for you") | ||
end | ||
|
||
def disabled_email | ||
generate_email | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
<%= stylesheet_link_tag "email" %> | ||
</head> | ||
<body> | ||
<h1>Normal email</h1> | ||
<%= image_tag "rails.png" %> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Asset email |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer not to use
ActiveSupport::Concern
, but I cannot find any rational reason not to (we're inside theRails
adapter, after all!) so I'll approve it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any bad side effects for using
ActiveSupport::Concern
?I am curious (since I use in my gems as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just that standard Ruby handles this pretty easily itself without the extra sugar (which feels like adding sugar to your soda IMO, maybe it's a bit sweeter but I don't see the point right here).
Strawman time
It's a bit like having an ActiveSupport library that overrides the
new
method on classes like this:I just don't see the point in using an external library for this pattern when just adding it straight to your class would do. Just accept a block in
initialize
and be done with it.But again, I cannot say a good reason not to do it here as we're in Rails, and the argument that it's so easy to implement without Rails means that if Rails ever removes it, it's very easy to remove again. It's a double-edged sword that way. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Thanks for your explanation.