Created
December 5, 2013 18:39
-
-
Save cyu/7810850 to your computer and use it in GitHub Desktop.
Protocol relative URLs in Paperclip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/paperclip.rb | |
require 'paperclip/protocol_relative_url_support' | |
Paperclip::Attachment.send :include, Paperclip::ProtocolRelativeURLSupport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/paperclip/protocol_relative_url_support.rb | |
module Paperclip | |
module ProtocolRelativeURLSupport | |
def self.included(base) | |
base.alias_method_chain :url, :protocol_relative | |
end | |
def url_with_protocol_relative(style_name = default_style, options = {}) | |
u = url_without_protocol_relative(style_name, options) | |
if options and options[:protocol_relative] | |
u.gsub(/^https?:/, '') | |
else | |
u | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get a protocol relative URL like this | |
product.image.attachment.url(:original, protocol_relative: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment