Module: SanitizeAttributes::Macros

Included in:
SanitizeAttributes
Defined in:
lib/sanitize_attributes/macros.rb

Instance Method Summary collapse

Instance Method Details

#sanitize_attributes(*args, &block) ⇒ Object

sanitize_attributes is used to define sanitizable attributes within a model definition.



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
# File 'lib/sanitize_attributes/macros.rb', line 4

def sanitize_attributes(*args, &block)
  if (args.last && args.last.is_a?(Hash))
    options = args.pop
  end
  options ||= {}
  unless @sanitize_hook_already_defined
    include InstanceMethods
    extend ClassMethods

    if options[:before_validation]
      before_validation :sanitize!
    else
      before_save :sanitize!
    end

    cattr_accessor :sanitizable_attribute_hash
    cattr_accessor :sanitization_block_array
    self.sanitizable_attribute_hash ||= {}
    self.sanitization_block_array ||= []

    @sanitize_hook_already_defined = true
  end

  if block
    self.sanitization_block_array << block
    block = self.sanitization_block_array.index(block)
  else
    block = nil
  end

  args.each do |attr|
    self.sanitizable_attribute_hash[attr] = block
  end

  true
end