Module: ModelFormatting::Init

Defined in:
lib/model_formatting/init.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_on(base) ⇒ Object



2
3
4
# File 'lib/model_formatting/init.rb', line 2

def self.setup_on(base)
  base.extend self
end

Instance Method Details

#formats(*args, &block) ⇒ Object

class Foo < ActiveRecord::Base

formats :body => :formatted_body do
  # add more attributes
  attributes[:title] = :full_title

  # add model methods to add to the processing context
  context << :project_id

  # modify the sanitizer
  white_list.allowed_tags << 'form'
  white_list.allowed_attributes << 'class'

  # add a callback for before html/markdown is processed
  before do |format, text, options|

  end

  # add a callback for after html/markdown is processed
  after do |format, text, options|

  end
end

end



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/model_formatting/init.rb', line 30

def formats(*args, &block)
  unless respond_to?(:model_formatting_attributes)
    # use all these attributes instead of a single ModelFormatting::Config because
    # it's easier to support subclassing.
    class_attribute :model_formatting_attributes, 
      :model_formatting_white_list, :model_formatting_context, 
      :model_formatting_before_callback, :model_formatting_after_callback
    send :include, ModelFormatting::InstanceMethods
    self.model_formatting_context    = []
    self.model_formatting_attributes = {} 
    self.model_formatting_white_list = HTML::WhiteListSanitizer.new
    before_save :format_content_with_model_formatting
  end

  model_formatting_attributes.update args.extract_options!
  args.each do |field|
    model_formatting_attributes[field] = "formatted_#{field}"
  end

  if block
    config = ModelFormatting::Config.new(model_formatting_white_list, model_formatting_attributes, model_formatting_context)
    config.instance_eval &block
    self.model_formatting_before_callback = config.before_callback if config.before_callback
    self.model_formatting_after_callback  = config.after_callback  if config.after_callback
  end
end