Module: NilifyBlanks::ClassMethods

Defined in:
lib/nilify_blanks.rb

Constant Summary collapse

DEFAULT_TYPES =
[:string, :text]
@@define_nilify_blank_methods_lock =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#define_attribute_methodsObject

This overrides the underlying rails method that defines attribute methods. This must be thread safe, just like the underlying method.



14
15
16
17
18
# File 'lib/nilify_blanks.rb', line 14

def define_attribute_methods
  if super
    define_nilify_blank_methods
  end
end

#inherited(child_class) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/nilify_blanks.rb', line 20

def inherited(child_class)
  if instance_variable_get(:@_nilify_blanks_options) and !child_class.instance_variable_get(:@_nilify_blanks_options)
    child_class.nilify_blanks @_nilify_blanks_options
  end

  super
end

#nilify_blanks(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nilify_blanks.rb', line 28

def nilify_blanks(options = {})
  return if @_nilify_blanks_options

  unless included_modules.include?(NilifyBlanks::InstanceMethods)
    include NilifyBlanks::InstanceMethods
  end

  @_nilify_blanks_options = options

  # Normally we wait for rails to define attribute methods, but we could be calling this after this has already been done.
  # If so, let's just immediately generate nilify blanks methods.
  #
  if @attribute_methods_generated
    define_nilify_blank_methods
  end

  descendants.each do |subclass|
    subclass.nilify_blanks @_nilify_blanks_options
  end
end