Sha256: fccb0fe4a90b00679a70c27cb3826b6e6ca32f3860506d7a4d8a354c209f5d13
Contents?: true
Size: 1.54 KB
Versions: 2
Compression:
Stored size: 1.54 KB
Contents
module AutoHtmlFor # default options that can be overridden on the global level @@auto_html_for_options = { :htmlized_attribute_suffix => '_html' } mattr_reader :auto_html_for_options def self.included(base) base.extend(ClassMethods) end module ClassMethods def auto_html_for(raw_attrs, &proc) include AutoHtmlFor::InstanceMethods suffix = AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix] auto_html_for_columns = [raw_attrs].flatten.map { |a| "#{a}#{suffix}" } # Needed for Mongoid column_names = self.respond_to?(:column_names) ? self.column_names : fields.keys missing_cache_columns = auto_html_for_columns - column_names missing_cache_columns.each do |missing_cache_column| raw_attr = missing_cache_column.gsub(suffix, '') define_method(missing_cache_column) do val = self[raw_attr] auto_html(val, &proc) end end cache_columns = auto_html_for_columns - missing_cache_columns cache_columns.each do |cache_column| raw_attr = cache_column.gsub(suffix, '') define_method("#{raw_attr}=") do |val| self[raw_attr] = val result = auto_html(val, &proc) self.send("#{cache_column}=", result) val end define_method(cache_column) do result = self[cache_column] result.respond_to?(:html_safe) ? result.html_safe : result end end end end module InstanceMethods include AutoHtml end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
auto_html-1.6.0 | lib/auto_html/auto_html_for.rb |
auto_html-1.5.3 | lib/auto_html/auto_html_for.rb |