Sha256: c359e2f0cd70ccef998c5149ab17229cb5b4c7c929f599a5bab0592ad160c5c9

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 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
      before_save :auto_html_prepare

      define_method("auto_html_prepare") do
        auto_html_methods = self.methods.select { |m| m=~/^auto_html_prepare_/ }
        auto_html_methods.each do |method|
          self.send(method)
        end
      end

      suffix =  AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix]

      [raw_attrs].flatten.each do |raw_attr|
        define_method("#{raw_attr}#{suffix}=") do |val|
          a = "#{raw_attr}#{suffix}"
          write_attribute(a, val) if attributes[a]
        end
        define_method("#{raw_attr}#{suffix}") do
          result = read_attribute("#{raw_attr}#{suffix}") || send("auto_html_prepare_#{raw_attr}")
          result.respond_to?(:html_safe) ?
            result.html_safe :
              result
        end
        define_method("auto_html_prepare_#{raw_attr}") do
          result = auto_html(self.send(raw_attr), &proc)
          self.send(raw_attr.to_s + suffix + "=", result)
          result
        end
      end
    end
  end

  module InstanceMethods
    include AutoHtml
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auto_html-1.5.1 lib/auto_html/auto_html_for.rb