lib/preformatter.rb in preformatter-0.3.0 vs lib/preformatter.rb in preformatter-0.5.0
- old
+ new
@@ -2,10 +2,26 @@
def self.included(base)
base.extend ClassMethods
end
+ def self.has_spanish_chars?(string)
+ string.index(/[áéíóúñÑÁÉÍÓÚ]/) rescue nil
+ end
+
+ def self.replace_spanish_chars_in(string)
+ replace = {'á' => 'a','é' => 'e','í' => 'i','ó' => 'o','ú' => 'u',
+ 'ñ' => 'n', 'Ñ' => 'N',
+ 'Á' => 'A' , 'É' => 'E', 'Í' => 'I', 'Ó' => 'O','Ú' => 'U'}
+ unless string.nil?
+ string_with_no_spanish_chars = string.gsub(/[#{replace.keys.join('|')}]/).each do |c|
+ replace[c]
+ end
+ return string_with_no_spanish_chars
+ end
+ end
+
module ClassMethods
def no_spaces_in(*args)
args.each do |field|
before_validation do |record|
@@ -17,17 +33,12 @@
def no_accents_in(*args)
args.each do |field|
before_validation do |record|
attribute = record.send("#{field.to_s}")
- replace = {'á' => 'a','é' => 'e','í' => 'i','ó' => 'o','ú' => 'u',
- 'ñ' => 'n', 'Ñ' => 'N',
- 'Á' => 'A' , 'É' => 'E', 'Í' => 'I', 'Ó' => 'O','Ú' => 'U'}
unless attribute.nil?
- attribute.gsub!(/[#{replace.keys.join('|')}]/).each do |c|
- replace[c]
- end
+ record.send "#{field.to_s}=", Preformatter.replace_spanish_chars_in(attribute)
end
end
end
end
@@ -36,16 +47,27 @@
options[:in].each do |field|
before_validation do |record|
attribute = record.send("#{field.to_s}")
unless attribute.nil?
attribute = attribute.delete!(chars)
- RAILS_DEFAULT_LOGGER.debug attribute
end
end
end
end
+ 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
+
end
+
end
class ActiveRecord::Base
include Preformatter
\ No newline at end of file