Module: RedCloth::ActiveModelDecorator

Defined in:
lib/redcloth_on.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
# File 'lib/redcloth_on.rb', line 4

def self.included(base)

  base.class_eval do

    #
    # USAGE: 
    #
    # class Person
    #   redcloth_on [:name, :last_name]
    # end
    #
    # and those attributes will be decorated by RedCloth
    #


    def self.redcloth_on(attributes_array=[])
      attributes_array.each{|a| define_proper_methods_on(a) }
    end

    private

    #
    # nodoc
    #

    def self.define_proper_methods_on(name)

      solve_value = (name.to_s + "_attribute").to_sym

      define_method(name) do
        RedCloth.new(self.send(solve_value)).to_html
      end

      define_method(solve_value) do
        qt = attributes[name.to_s]
        qt.nil? ? "" : qt
      end

      define_method("old_" + name.to_s) do
        attributes[name.to_s]
      end

    end
  end
end