Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started work on a florida counties validator. Still more work to be d…

…one.
  • Loading branch information...
commit f377f4b18bd350b27b338d05fbe6b1fb19866e6f 1 parent 4a601f0
@jeremiahishere jeremiahishere authored
View
88 app/validators/florida_counties_validator.rb
@@ -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
View
52 spec/dummy/log/test.log
@@ -97,3 +97,55 @@
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.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'
+
View
11 spec/support/basic_record.rb
@@ -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
View
39 spec/validators/florida_counties_spec.rb
@@ -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
View
12 spec/validators/ssn_format_validator_spec.rb
@@ -1,21 +1,11 @@
require 'spec_helper'
-class BasicRecord
- def initialize
- @errors = {:ssn => []}
- end
-
- def errors
- @errors
- end
-end
-
describe SsnFormatValidator do
describe ".validate_each" do
before(:each) do
@options = {:attributes => {}}
@validator = SsnFormatValidator.new(@options)
- @record = BasicRecord.new
+ @record = BasicRecord.new(:ssn)
end
it "should validate the format for ###-##-####" do
Please sign in to comment.
Something went wrong with that request. Please try again.