Module: IHelpers::ActiveRecord::Validations::ClassMethods
- Defined in:
- lib/i_helpers/active_record/validations.rb
Instance Method Summary collapse
-
#validates_as_email(*attr_names) ⇒ Object
Taken from: marklunds.com/articles/one/312.
- #validates_as_immutable(*attr_names) ⇒ Object
-
#validates_as_phone(*attr_names) ⇒ Object
Taken from: marklunds.com/articles/one/312.
Instance Method Details
#validates_as_email(*attr_names) ⇒ Object
Taken from: marklunds.com/articles/one/312
12 13 14 15 16 17 18 19 20 |
# File 'lib/i_helpers/active_record/validations.rb', line 12 def validates_as_email(*attr_names) configuration = { :message => :email, :with => Format::EMAIL, # Requires simply_useful gem } configuration.update(attr_names.) validates_format_of attr_names, configuration end |
#validates_as_immutable(*attr_names) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/i_helpers/active_record/validations.rb', line 39 def validates_as_immutable(*attr_names) = attr_names. .reverse_merge!({:message => :immutable, :allow_change_from_nil => false, :on => :save}) validates_each(attr_names, ) do |record, attr_name, value| if !record.new_record? && record.send("#{attr_name}_changed?") && !([:allow_change_from_nil] && record.send("#{attr_name}_was").nil?) record.errors.add(attr_name, [:message]) end end end |
#validates_as_phone(*attr_names) ⇒ Object
Taken from: marklunds.com/articles/one/312
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/i_helpers/active_record/validations.rb', line 23 def validates_as_phone(*attr_names) configuration = { :message => :phone, :on => :save } configuration.update(attr_names.) validates_each(attr_names, configuration) do |record, attr_name, value| n_digits = value.scan(/[0-9]/).size valid_chars = (value =~ /^[+\/\-() 0-9]+$/) if !(n_digits > 5 && valid_chars) record.errors.add(attr_name, configuration[:message]) end end end |