Skip to content

Instantly share code, notes, and snippets.

@taf2
Created February 24, 2021 01:22
Show Gist options
  • Save taf2/d899dc2de05e6df0727a146159e2563d to your computer and use it in GitHub Desktop.
Save taf2/d899dc2de05e6df0727a146159e2563d to your computer and use it in GitHub Desktop.
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