Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some tests and a helper method #5

Merged
merged 2 commits into from
Jun 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/validators/phony_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "is an invalid number") if not Phony.plausible?(value)
end

end
end

module ActiveModel
module Validations
module HelperMethods

def validates_plausible_phone(*attr_names)
validates_with PhonyPlausibleValidator, _merge_attributes(attr_names)
end

end
end
end
60 changes: 60 additions & 0 deletions spec/lib/phony_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'

ActiveRecord::Schema.define do
create_table :validating_homes do |table|
table.column :phone_number, :string
table.column :fax_number, :string
end
end

class ValidatingHome < ActiveRecord::Base
attr_accessor :phone_method, :fax_number
validates :phone_number, :phony_plausible => true
validates_plausible_phone :fax_number
end



describe PhonyPlausibleValidator do

describe 'validates' do
before(:each) do
@home = ValidatingHome.new
end

it "should validate an empty number" do
@home.should be_valid
end

it "should validate a valid number" do
@home.phone_number = '123456789'
@home.should be_valid
end

it "should invalidate an invalid number" do
@home.phone_number = '123456789 123456789 123456789 123456789'
@home.should_not be_valid
end
end

describe 'validates_plausible_phone' do
before(:each) do
@home = ValidatingHome.new
end

it "should validate an empty number" do
@home.should be_valid
end

it "should validate a valid number" do
@home.fax_number = '123456789'
@home.should be_valid
end

it "should invalidate an invalid number" do
@home.fax_number = '123456789 123456789 123456789 123456789'
@home.should_not be_valid
end
end

end