Module: Preformatter::ClassMethods

Defined in:
lib/preformatter.rb

Instance Method Summary collapse

Instance Method Details

#include_ascii_for_fields(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/preformatter.rb', line 57

def include_ascii_for_fields(*args)
  args.each do |field|
    before_validation do |record|
      attribute = record.send("#{field.to_s}")
      if Preformatter.has_spanish_chars?(attribute)
        record.send "ascii_#{field.to_s}=", Preformatter.replace_spanish_chars_in(attribute)
      end
    end
  end
end

#no_accents_in(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/preformatter.rb', line 34

def no_accents_in(*args)
  args.each do |field|
    before_validation do |record|
      attribute = record.send("#{field.to_s}")
      unless attribute.nil?
        record.send "#{field.to_s}=", Preformatter.replace_spanish_chars_in(attribute)
      end
    end
  end
end

#no_spaces_in(*args) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/preformatter.rb', line 25

def no_spaces_in(*args) 
  args.each do |field|
    before_validation do |record|
      attribute = record.send("#{field.to_s}")
      attribute.delete!(' ') unless attribute.nil?
    end
  end
end

#no_special_characters(chars, options = {}) ⇒ Object



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

def no_special_characters(chars, options = {})
  options[:in] ||= []
  options[:in].each do |field|
    before_validation do |record|
      attribute = record.send("#{field.to_s}")
      unless attribute.nil?
        attribute = attribute.delete!(chars)
      end
    end
  end
end

#remove_extra_white_spaces(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/preformatter.rb', line 68

def remove_extra_white_spaces(*args)
  args.each do |field|
    before_save do |record|
      attribute = record.send("#{field.to_s}")
      attribute.strip! if !attribute.nil? and !attribute.strip!.nil?
      attribute.gsub!(/[ ]+/, ' ') if !attribute.nil? and !attribute.gsub!(/[ ]+/, ' ').nil?
    end
  end
end