-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows dates to be submitted in a few different formats, namely month/day/year, or spelled out, as in November 17, 2014. In the month/day/year format, both 2-digit and 4-digit years are accepted. The day/month/year format is also accepted, but to use it, you must first update the `date_format` option in `settings.yml`.
- Loading branch information
Showing
20 changed files
with
154 additions
and
13 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class DateValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
value = record.read_attribute_before_type_cast(attribute) | ||
return if value.blank? || value.is_a?(Date) | ||
|
||
default_message = "#{value} #{I18n.t('errors.messages.invalid_date')}" | ||
|
||
unless date_valid?(value) | ||
record.errors[attribute] << (options[:message] || default_message) | ||
return | ||
end | ||
|
||
convert_value_to_date(record, attribute, value) | ||
end | ||
|
||
private | ||
|
||
def date_valid?(date) | ||
return true unless date.include?('/') | ||
return Date.valid_date?(year(date), month(date), day(date)) if month_day? | ||
return Date.valid_date?(year(date), day(date), month(date)) if day_month? | ||
end | ||
|
||
def month_day? | ||
SETTINGS[:date_format] == '%m/%d/' | ||
end | ||
|
||
def day_month? | ||
SETTINGS[:date_format] == '%d/%m/' | ||
end | ||
|
||
def split_date(date) | ||
date.split('/') | ||
end | ||
|
||
def year(date) | ||
split_date(date).last.to_i | ||
end | ||
|
||
def month(date) | ||
split_date(date).first.to_i | ||
end | ||
|
||
def day(date) | ||
split_date(date).second.to_i | ||
end | ||
|
||
def convert_value_to_date(record, attribute, value) | ||
record[attribute] = parse_date(value) | ||
end | ||
|
||
def parse_date(date) | ||
Date.strptime(date, date_format_for(date)) rescue Date.parse(date) | ||
end | ||
|
||
def date_format_for(date) | ||
SETTINGS[:date_format] + year_format_for(date) | ||
end | ||
|
||
def year_format_for(date) | ||
return '%Y' if date =~ /\d{4}/ | ||
'%y' | ||
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
id,accreditations,alternate_name,date_incorporated,description,email,funding_sources,legal_status,licenses,name,tax_id,tax_status,website | ||
1,"BBB, State Board of Education",HFB,1/1/1970,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Harvest Food Bank,12-456789,501(c)3,http://www.example.org | ||
1,"BBB, State Board of Education",HFB,1/24/1970,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Harvest Food Bank,12-456789,501(c)3,http://www.example.org | ||
2,,,1/2/2014,Example org description,info@fakeorg.org,Donations,Nonprofit,,Example Agency,,,http://www.example.org |
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
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
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
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
id,accreditations,alternate_name,date_incorporated,description,email,funding_sources,legal_status,licenses,name,tax_id,tax_status,website | ||
1,"BBB, State Board of Education",HFB,1/24/70,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Parent Agency,12-456789,501(c)3,http://www.example.org |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
id,accreditations,alternate_name,date_incorporated,description,email,funding_sources,legal_status,licenses,name,tax_id,tax_status,website | ||
1,"BBB, State Board of Education",HFB,24/2/70,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Parent Agency,12-456789,501(c)3,http://www.example.org |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
id,accreditations,alternate_name,date_incorporated,description,email,funding_sources,legal_status,licenses,name,tax_id,tax_status,website | ||
1,"BBB, State Board of Education",HFB,"January 20, 1970","Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Parent Agency,12-456789,501(c)3,http://www.example.org |
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
id,accreditations,alternate_name,date_incorporated,description,email,funding_sources,legal_status,licenses,name,tax_id,tax_status,website | ||
1,"BBB, State Board of Education",HFB,1/1/1970,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Parent Agency,12-456789,501(c)3,http://www.example.org | ||
1,"BBB, State Board of Education",HFB,1/2/1970,"Harvest Food Bank provides fresh produce, dairy, and canned goods to food pantries throughout the city.",info@example.org,"Donations, Grants",Nonprofit,State Health Inspection License,Parent Agency,12-456789,501(c)3,http://www.example.org |