-
-
Save taf2/d899dc2de05e6df0727a146159e2563d to your computer and use it in GitHub Desktop.
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
module Ctm | |
module UrlWithMustache | |
extend ActiveSupport::Concern | |
included do | |
before_validation :mustache_safe_url_prepare | |
after_validation :mustache_safe_url_restore | |
def mustache_safe_url_prepare | |
@_mustache_safe_prepared_urls = {} | |
self.class.mustache_safe_urls.each {|attr| | |
method = attr.to_sym | |
@_mustache_safe_prepared_urls[method] = self.send(method) | |
self.send("#{attr}=", Mustache.render(self.send(method) || '', {})) | |
} | |
end | |
def mustache_safe_url_restore | |
self.class.mustache_safe_urls.each {|attr| | |
self.send("#{attr}=", @_mustache_safe_prepared_urls[attr.to_sym]) | |
} | |
end | |
end | |
class_methods do | |
def mustache_validated_url(*attrs) | |
@_mustache_safe_urls ||= Set.new | |
attrs.each {|attr| | |
@_mustache_safe_urls << attr | |
} | |
end | |
def mustache_safe_urls | |
@_mustache_safe_urls | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment