Module: BBTagCloser::ClassMethods

Defined in:
lib/bb_tag_closer.rb

Instance Method Summary collapse

Instance Method Details

#auto_close_bb_tags(options = {}) ⇒ Object

Closes tags on text attributes. Closes tags before save by default, but can be overridden by passing :on; the :on paramater accepts :save, :create, :update, :validation. Alternatively, pass :no_callback => true to prevent tags from being closed automatically (you will have to close them in the controller). If custom mass_assignment is used such as attr_accessible :foo, :as => :bar pass :mass_assignment_keys => :bar or :mass_assignment_keys => [:bar, :baz].



32
33
34
35
36
37
38
39
40
41
# File 'lib/bb_tag_closer.rb', line 32

def auto_close_bb_tags(options = {})
  unless options[:no_callback]
    target = options[:on] ? "before_#{options[:on]}" : "before_save"
    send(target, :close_tags)
  end
  mass_assignment_keys = Array options[:mass_assignment_keys]
  mass_assignment_keys.each do |key|
    attr_accessible :font_size, :font_color, :as => key
  end
end

#auto_close_bb_tags_for(*args) ⇒ Object

Specify which attributes to close tags on. Accepts the same options as auto_close_bb_tags.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bb_tag_closer.rb', line 45

def auto_close_bb_tags_for(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  unless options[:no_callback]
    target = options[:on] ? "before_#{options[:on]}" : "before_save"
    send(target, :close_tags)
  end
  self.singleton_class.class_eval do
    define_method :taggable_fields do
      args.delete_if {|i| not i.is_a? Symbol}
    end
  end
end

#taggable_fieldsObject

ActiveRecord text attributes are considered taggable by default. Using auto_close_bb_tags_for overrides default attributes with custom attributes.



20
21
22
# File 'lib/bb_tag_closer.rb', line 20

def taggable_fields
  columns.map {|c| {c.name.to_sym => c.type}}.delete_if {|i| not i.value? :text}.map(&:keys).flatten
end