Skip to content

Commit

Permalink
Merge pull request MarceloCajueiro#4 from norbaoliveira/master
Browse files Browse the repository at this point in the history
Fix and refactoring mask_validator adding support to no active record classes
  • Loading branch information
MarceloCajueiro committed Jan 9, 2013
2 parents 580ce7f + 818df22 commit 1caf7dc
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 159 deletions.
12 changes: 11 additions & 1 deletion lib/mask_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(options)
end

def validate_each(record, attribute, value)
value = record.send("#{attribute.to_s}_before_type_cast")
value = normalize_value(record, attribute, value)

return if (@allow_nil && value.nil?) || (@allow_blank && value.blank?)

Expand Down Expand Up @@ -67,4 +67,14 @@ def mask_value_for(record=nil)
def message
options[:message]
end

def normalize_value(record, attribute, value)
attribute_before_type_cast = "#{attribute.to_s}_before_type_cast"

if record.respond_to?(attribute_before_type_cast)
value = record.send(attribute_before_type_cast)
end

value
end
end
329 changes: 171 additions & 158 deletions spec/mask_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,187 +1,200 @@
require 'spec_helper'

describe MaskValidator do
subject do
Person.new
end
context 'When class is not an ActiveRecord model' do
subject do
NoActiveRecordPerson.new
end

# validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
context "mask validation to phone" do
it "should be valid with a phone in according of the mask" do
subject.phone = '(48) 9874-4569'
subject.should have_valid_value_to(:phone)
end

it "should not be valid with a phone with a wrong pattern" do
subject.phone = '4852458787'
subject.should_not have_valid_value_to(:phone)

subject.phone = '48 9865 4879'
subject.should_not have_valid_value_to(:phone)

subject.phone = '(48) 9874-45169'
subject.should_not have_valid_value_to(:phone)

subject.phone = '(48)98956698'
subject.should_not have_valid_value_to(:phone)
end
end

# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym" do
it "should be valid with a acronym in according of the mask" do
subject.acronym = '1gd'
subject.should have_valid_value_to(:acronym)

subject.acronym = '666'
subject.should have_valid_value_to(:acronym)

subject.acronym = 'zzz'
subject.should have_valid_value_to(:acronym)
end

it "should not be valid with a acronym with a wrong pattern" do
subject.acronym = '1qw1'
subject.should_not have_valid_value_to(:acronym)
context 'When class is an ActiveRecord model' do
subject do
Person.new
end
end

# validates :alphanumeric, :mask => "aaa999"
context "mask validation to alphanumeric" do
it "should be valid with a alphanumeric in according of the mask" do
subject.alphanumeric = 'awe987'
subject.should have_valid_value_to(:alphanumeric)
end
# validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
context "mask validation to phone" do
it "should be valid with a phone in according of the mask" do
subject.phone = '(48) 9874-4569'
subject.should have_valid_value_to(:phone)
end

it "should not be valid with a alphanumeric with a wrong pattern" do
subject.alphanumeric = '999999'
subject.should_not have_valid_value_to(:alphanumeric)
it "should not be valid with a phone with a wrong pattern" do
subject.phone = '4852458787'
subject.should_not have_valid_value_to(:phone)

subject.alphanumeric = 'QQQQQQ'
subject.should_not have_valid_value_to(:alphanumeric)
subject.phone = '48 9865 4879'
subject.should_not have_valid_value_to(:phone)

subject.alphanumeric = '666aaaa'
subject.should_not have_valid_value_to(:alphanumeric)
subject.phone = '(48) 9874-45169'
subject.should_not have_valid_value_to(:phone)

subject.alphanumeric = '666AAA'
subject.should_not have_valid_value_to(:alphanumeric)
end
end

context "when the attributes type is not a string" do
it "should not be valid with a wrong date format" do
subject.birth_date = "15-12-2009"
subject.should_not have_valid_value_to(:birth_date)
subject.phone = '(48)98956698'
subject.should_not have_valid_value_to(:phone)
end
end

it "should not be valid with a wrong birth year" do
subject.birth_year = 20110
subject.should_not have_valid_value_to(:birth_year)
end
# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym" do
it "should be valid with a acronym in according of the mask" do
subject.acronym = '1gd'
subject.should have_valid_value_to(:acronym)

it "should not be valid with a wrong birth time" do
subject.birth_time = "333:20"
subject.should_not have_valid_value_to(:birth_time)
end
subject.acronym = '666'
subject.should have_valid_value_to(:acronym)

it "should not be valid with a wrong body fat" do
subject.body_fat = 333.00
subject.should_not have_valid_value_to(:body_fat)
end
end
subject.acronym = 'zzz'
subject.should have_valid_value_to(:acronym)
end

# validates :custom, :mask => :custom_mask, :allow_blank => false
context "mask validation to custom field using custom_mask method" do
it "should be valid with an correct custom value" do
subject.stub!(:custom_mask => "99999-999")
subject.custom = '32632-567'
subject.should have_valid_value_to(:custom)
it "should not be valid with a acronym with a wrong pattern" do
subject.acronym = '1qw1'
subject.should_not have_valid_value_to(:acronym)
end
end

it "should not be valid with an empty custom value" do
subject.stub!(:custom_mask => "9.9.9")
subject.custom = ''
subject.should_not have_valid_value_to(:custom)
end
end
# validates :alphanumeric, :mask => "aaa999"
context "mask validation to alphanumeric" do
it "should be valid with a alphanumeric in according of the mask" do
subject.alphanumeric = 'awe987'
subject.should have_valid_value_to(:alphanumeric)
end

# validates :identification, :mask => Proc.new{|o| o.proc_mask}, :allow_blank => false
context "mask validation to proc field using a Proc" do
it "should be valid with an correct value" do
subject.stub!(:proc_mask => "999.9.99")
subject.identification = '326.3.67'
subject.should have_valid_value_to(:identification)
end
it "should not be valid with a alphanumeric with a wrong pattern" do
subject.alphanumeric = '999999'
subject.should_not have_valid_value_to(:alphanumeric)

subject.alphanumeric = 'QQQQQQ'
subject.should_not have_valid_value_to(:alphanumeric)

it "should not be valid with an empty value" do
subject.stub!(:prock_mask => "9.9.9")
subject.identification = ''
subject.should_not have_valid_value_to(:identification)
end
end

# validates :phone, :mask => "99999-999", :allow_blank => true
context "mask validation to phone with explicit ':allow_blank => true'" do
it "should not be valid with an nil phone" do
subject.phone = nil
subject.should have_valid_value_to(:phone)
end

it "should be valid with an empty phone" do
subject.phone = ''
subject.should have_valid_value_to(:phone)
end
end

# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym with explicit ':allow_nil => true'" do
it "should be valid with an nil acronym" do
subject.acronym = nil
subject.should have_valid_value_to(:acronym)
end

it "should be valid with an empty acronym" do
subject.acronym = ''
subject.should_not have_valid_value_to(:acronym)
end
end

# validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
context "mask validation to fax with explicit ':allow_nil => false'" do
it "should not be valid with an nil fax" do
subject.fax = nil
subject.should_not have_valid_value_to(:fax)
end

it "should be valid with an empty fax" do
subject.fax = ''
subject.should_not have_valid_value_to(:fax)
end
end

# validates :zip_code, :mask => "99999-999", :allow_blank => false
context "mask validation to zip_code with explicit ':allow_blank => false'" do
it "should not be valid with an nil zip_code" do
subject.zip_code = nil
subject.should_not have_valid_value_to(:zip_code)
end

it "should be valid with an empty zip_code" do
subject.zip_code = ''
subject.should_not have_valid_value_to(:zip_code)
end
end

# validates :birth_date, :mask => '99/99/9999'
context "mask validation to birth_date with implicit ':allow_blank => false'" do
it "should not be valid with an nil birth_date" do
subject.birth_date = nil
subject.should_not have_valid_value_to(:birth_date)
end
subject.alphanumeric = '666aaaa'
subject.should_not have_valid_value_to(:alphanumeric)

subject.alphanumeric = '666AAA'
subject.should_not have_valid_value_to(:alphanumeric)
end
end

context "when the attributes type is not a string" do
it "should not be valid with a wrong date format" do
subject.birth_date = "15-12-2009"
subject.should_not have_valid_value_to(:birth_date)
end

it "should be valid with an empty birth_date" do
subject.birth_date = ''
subject.should_not have_valid_value_to(:birth_date)
it "should not be valid with a wrong birth year" do
subject.birth_year = 20110
subject.should_not have_valid_value_to(:birth_year)
end

it "should not be valid with a wrong birth time" do
subject.birth_time = "333:20"
subject.should_not have_valid_value_to(:birth_time)
end

it "should not be valid with a wrong body fat" do
subject.body_fat = 333.00
subject.should_not have_valid_value_to(:body_fat)
end
end

# validates :custom, :mask => :custom_mask, :allow_blank => false
context "mask validation to custom field using custom_mask method" do
it "should be valid with an correct custom value" do
subject.stub!(:custom_mask => "99999-999")
subject.custom = '32632-567'
subject.should have_valid_value_to(:custom)
end

it "should not be valid with an empty custom value" do
subject.stub!(:custom_mask => "9.9.9")
subject.custom = ''
subject.should_not have_valid_value_to(:custom)
end
end

# validates :identification, :mask => Proc.new{|o| o.proc_mask}, :allow_blank => false
context "mask validation to proc field using a Proc" do
it "should be valid with an correct value" do
subject.stub!(:proc_mask => "999.9.99")
subject.identification = '326.3.67'
subject.should have_valid_value_to(:identification)
end

it "should not be valid with an empty value" do
subject.stub!(:prock_mask => "9.9.9")
subject.identification = ''
subject.should_not have_valid_value_to(:identification)
end
end

# validates :phone, :mask => "99999-999", :allow_blank => true
context "mask validation to phone with explicit ':allow_blank => true'" do
it "should not be valid with an nil phone" do
subject.phone = nil
subject.should have_valid_value_to(:phone)
end

it "should be valid with an empty phone" do
subject.phone = ''
subject.should have_valid_value_to(:phone)
end
end

# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym with explicit ':allow_nil => true'" do
it "should be valid with an nil acronym" do
subject.acronym = nil
subject.should have_valid_value_to(:acronym)
end

it "should be valid with an empty acronym" do
subject.acronym = ''
subject.should_not have_valid_value_to(:acronym)
end
end

# validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
context "mask validation to fax with explicit ':allow_nil => false'" do
it "should not be valid with an nil fax" do
subject.fax = nil
subject.should_not have_valid_value_to(:fax)
end

it "should be valid with an empty fax" do
subject.fax = ''
subject.should_not have_valid_value_to(:fax)
end
end

# validates :zip_code, :mask => "99999-999", :allow_blank => false
context "mask validation to zip_code with explicit ':allow_blank => false'" do
it "should not be valid with an nil zip_code" do
subject.zip_code = nil
subject.should_not have_valid_value_to(:zip_code)
end

it "should be valid with an empty zip_code" do
subject.zip_code = ''
subject.should_not have_valid_value_to(:zip_code)
end
end

# validates :birth_date, :mask => '99/99/9999'
context "mask validation to birth_date with implicit ':allow_blank => false'" do
it "should not be valid with an nil birth_date" do
subject.birth_date = nil
subject.should_not have_valid_value_to(:birth_date)
end

it "should be valid with an empty birth_date" do
subject.birth_date = ''
subject.should_not have_valid_value_to(:birth_date)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require "support/db/schema"
require "support/models/person"
require "support/models/no_active_record_person"

RSpec::Matchers.define :have_valid_value_to do |attribute|
match do |actual|
Expand Down
7 changes: 7 additions & 0 deletions spec/support/models/no_active_record_person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class NoActiveRecordPerson
include ActiveModel::Validations

attr_accessor :phone

validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
end

0 comments on commit 1caf7dc

Please sign in to comment.