Sha256: e76e174de3590242c587bfc7c1a0ae9dee37697fdb9c028e06a4b1e75bb1bd93

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 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

      [raw_attrs].flatten.each do |raw_attr|
        define_method("auto_html_prepare_#{raw_attr}") do
          suffix = 
            AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix] ||
              AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix] 
          self.send(raw_attr.to_s + suffix + "=", 
            auto_html(self.send(raw_attr), &proc))
        end
      end
    end
  end

  module InstanceMethods
    include AutoHtml
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dejan-auto_html-1.1.0 lib/auto_html/auto_html_for.rb
dejan-auto_html-1.1.1 lib/auto_html/auto_html_for.rb
dejan-auto_html-1.1.2 lib/auto_html/auto_html_for.rb
dejan-auto_html-1.2.0 lib/auto_html/auto_html_for.rb
auto_html-1.2.1 lib/auto_html/auto_html_for.rb
auto_html-1.2.0 lib/auto_html/auto_html_for.rb