Module: AutoHtmlFor::ClassMethods

Defined in:
lib/auto_html/auto_html_for.rb

Instance Method Summary collapse

Instance Method Details

#auto_html_for(raw_attrs, &proc) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/auto_html/auto_html_for.rb', line 14

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|
      write_attribute("#{raw_attr}#{suffix}", val)
    end
    define_method("#{raw_attr}#{suffix}") do
      read_attribute("#{raw_attr}#{suffix}") || send("auto_html_prepare_#{raw_attr}")
    end
    define_method("auto_html_prepare_#{raw_attr}") do
      self.send(raw_attr.to_s + suffix + "=", 
        auto_html(self.send(raw_attr), &proc))
    end
  end
end