Skip to content

Commit

Permalink
Started work on a florida counties validator. Still more work to be d…
Browse files Browse the repository at this point in the history
…one.
  • Loading branch information
jeremiahishere committed Mar 14, 2012
1 parent 4a601f0 commit f377f4b
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 11 deletions.
88 changes: 88 additions & 0 deletions app/validators/florida_counties_validator.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,88 @@
#counties use abbreviations and will not validate otherwise
# ex: Saint Lucie must be St. Lucie
class FloridaCountiesValidator < ActiveModel::EachValidator
def validate_each record, attribute, value
value = value.downcase.split(' ').map {|w| w.capitalize }.join(' ')

# this part has not been tested
if value.include?("County")
message = value + ' county should not contain the word county.'
record.errors[attribute] << (options[:message] || message )
value.gsub("County", "").trim
end

message = value + ' is not a county in Florida'
record.errors[attribute] << (options[:message] || message ) unless COUNTIES.include? value

end

COUNTIES = [
"Alachua",
"Baker",
"Bay",
"Bradford",
"Brevard",
"Broward",
"Calhoun",
"Charlotte",
"Citrus",
"Clay",
"Collier",
"Columbia",
"De Soto",
"Suncoast",
"Dixie",
"Duval",
"Escambia",
"Flagler",
"Franklin",
"Gadsden",
"Gilchrist",
"Glades",
"Gulf",
"Hamilton",
"Hardee",
"Hendry",
"Hernando",
"Highlands",
"Hillsborough Suncoast",
"Holmes",
"Indian River",
"Jackson",
"Jefferson",
"Lafayette",
"Lake",
"Lee",
"Leon",
"Levy",
"Liberty",
"Madison",
"Manatee",
"Marion",
"Martin",
"Miami-Dade",
"Monroe",
"Nassau",
"Okaloosa",
"Okeechobee",
"Orange",
"Osceloa",
"Palm Beach",
"Pasco",
"Pinellas",
"Polk",
"Putnam",
"Santa Rosa",
"Sarasota",
"Seminole",
"St. Johns",
"St. Lucie",
"Sumter",
"Suwannee",
"Taylor",
"Union",
"Volusia",
"Wakulla",
"Walton",
"Washington"]
end
52 changes: 52 additions & 0 deletions spec/dummy/log/test.log
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -96,4 +96,56 @@
SQL (1.3ms)  SELECT name SQL (1.3ms)  SELECT name
FROM sqlite_master FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence' WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.5ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.6ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.5ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.4ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.3ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.4ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.4ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.6ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.4ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.0ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.5ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (1.4ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'

SQL (0.2ms)  SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
 
11 changes: 11 additions & 0 deletions spec/support/basic_record.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
# this class is used to test the validators
# it contains the methods needed to mimic and ActiveRecord::Base for the validators
class BasicRecord
def initialize(attribute)
@errors = {attribute => []}
end

def errors
@errors
end
end
39 changes: 39 additions & 0 deletions spec/validators/florida_counties_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe FloridaCountiesValidator do

describe ".validate_each" do
before(:all) do
@options = {:attributes => {}}
@validator = FloridaCountiesValidator.new(@options)
@record = BasicRecord.new(:county)
end

it "should return false for invalid county" do
@record.errors[:county].should_receive("<<")
@validator.validate_each(@record, :county, "123456789")
end

it "should return true for valid county" do
@record.errors[:county].should_not_receive("<<")
@validator.validate_each(@record, :county, "Orange")
end

it "should validate lowercase county name" do
@record.errors[:county].should_not_receive("<<")
@validator.validate_each(@record, :county, "orange")
end

it "should validate all uppercase county name" do
@record.errors[:county].should_not_receive("<<")
@validator.validate_each(@record, :county, "ORANGE")
end

it "should validate multi word count" do
@record.errors[:county].should_not_receive("<<")
@validator.validate_each(@record, :county, "St. Lucie")
end

end

end
12 changes: 1 addition & 11 deletions spec/validators/ssn_format_validator_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,11 @@
require 'spec_helper' require 'spec_helper'


class BasicRecord
def initialize
@errors = {:ssn => []}
end

def errors
@errors
end
end

describe SsnFormatValidator do describe SsnFormatValidator do
describe ".validate_each" do describe ".validate_each" do
before(:each) do before(:each) do
@options = {:attributes => {}} @options = {:attributes => {}}
@validator = SsnFormatValidator.new(@options) @validator = SsnFormatValidator.new(@options)
@record = BasicRecord.new @record = BasicRecord.new(:ssn)
end end


it "should validate the format for ###-##-####" do it "should validate the format for ###-##-####" do
Expand Down

0 comments on commit f377f4b

Please sign in to comment.