forked from jeremiahishere/format_validators
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started work on a florida counties validator. Still more work to be d…
…one.
- Loading branch information
1 parent
4a601f0
commit f377f4b
Showing
5 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters