Skip to content

Instantly share code, notes, and snippets.

@foliosus
Created June 24, 2013 23:22
Show Gist options
  • Save foliosus/5854630 to your computer and use it in GitHub Desktop.
Save foliosus/5854630 to your computer and use it in GitHub Desktop.
module Sanitizer
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def sanitizes(method_names)
method_names.each do |new_method_name|
old_method_name = :"#{method_name}_with_sanitization"
alias_method old_method_name, new_method_name
define_method new_method_name do(*args, &block)
result = send(old_method_name, *args, &block)
Sanitizer::SanitizingWrapper.new(result)
end
end
end
end
class SanitizingWrapper
def initialize(value)
@value = value
end
def method_missing(method_name)
@value.send(method_name)
end
def to_s
ActionController::Base.helpers.sanitize(
@value.to_s || 'No name',
:tags => %w(h3 h4 ul ol li p a strong em br font),
:attributes => %w(class id name rel data-width data-height data-tip data-tip-width data-tip-position size)
)
end
alias_method :to_html, :to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment