Module: Err::Acts::Textiled::ClassMethods

Defined in:
lib/acts_as_textiled/base.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_textiled(*attributes) ⇒ Object



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
# File 'lib/acts_as_textiled/base.rb', line 9

def acts_as_textiled(*attributes)
  @textiled_attributes = []

  @textiled_unicode = String.new.respond_to? :chars

  ruled = attributes.last.is_a?(Hash) ? attributes.pop : {}
  attributes += ruled.keys

  type_options = %w( plain source )

  attributes.each do |attribute|
    define_method(attribute) do |*type|
      type = type.first

      if type.nil? && self[attribute]
        textiled[attribute.to_s] ||= RedCloth.new(self[attribute], Array(ruled[attribute])).to_html 
      elsif type.nil? && self[attribute].nil?
        nil
      elsif type_options.include?(type.to_s)
        send("#{attribute}_#{type}")
      else
        raise "I don't understand the `#{type}' option.  Try #{type_options.join(' or ')}."
      end
    end

    define_method("#{attribute}_plain",  proc { strip_redcloth_html(__send__(attribute)) if __send__(attribute) } )
    define_method("#{attribute}_source", proc { __send__("#{attribute}_before_type_cast") } )

    @textiled_attributes << attribute
  end

  include Err::Acts::Textiled::InstanceMethods
end

#textiled_attributesObject



43
44
45
# File 'lib/acts_as_textiled/base.rb', line 43

def textiled_attributes
  Array(@textiled_attributes) 
end