initial Rails 3 rewrite commit

completely rewritten for ActiveModel compatibility
uses ActiveModel EachValidator class as validator base class
simplifies :between by splitting into a :on_or_before and an :on_of_after
only :is_at option tested
This commit is contained in:
Adam Meehan
2010-08-01 18:35:18 +10:00
commit fdc3086976
18 changed files with 642 additions and 0 deletions

27
spec/model_helpers.rb Normal file
View File

@@ -0,0 +1,27 @@
module ModelHelpers
# Some test helpers from Rails source
def invalid!(attr_name, values, error = nil)
with_each_person_value(attr_name, values) do |record, value|
record.should be_invalid
record.errors[attr_name].size.should >= 1
record.errors[attr_name].first.should == error if error
end
end
def valid!(attr_name, values)
with_each_person_value(attr_name, values) do |record, value|
record.should be_valid
end
end
def with_each_person_value(attr_name, values)
record = Person.new
values = [values] unless values.is_a?(Array)
values.each do |value|
record.send("#{attr_name}=", value)
yield record, value
end
end
end

44
spec/spec_helper.rb Normal file
View File

@@ -0,0 +1,44 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__))
require 'rspec'
require 'rspec/autorun'
require 'active_model'
require 'active_model/validations'
# require 'active_record'
# require 'action_controller'
# require 'action_view'
# require 'action_mailer'
# require 'rspec/rails'
require 'timecop'
require 'model_helpers'
require 'validates_timeliness'
Time.zone = 'Australia/Melbourne'
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/validates_timeliness/locale/en.yml')
I18n.load_path.unshift(LOCALE_PATH)
class Person
include ActiveModel::Validations
extend ActiveModel::Translation
attr_accessor :birth_date, :birth_time, :birth_datetime
def initialize(attributes = {})
attributes.each do |key, value|
send "#{key}=", value
end
end
end
Rspec.configure do |c|
c.mock_with :rspec
c.before do
Person.reset_callbacks(:validate)
Person._validators.clear
end
end

View File

@@ -0,0 +1,102 @@
require 'spec_helper'
describe ValidatesTimeliness::Conversion do
include ValidatesTimeliness::Conversion
before do
Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
end
describe "#type_cast_value" do
describe "for date type" do
it "should return same value for date value" do
type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1)
end
it "should return date part of time value" do
type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
end
it "should return date part of datetime value" do
type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
end
end
describe "for time type" do
it "should return same value for time value matching dummy date part" do
type_cast_value(Time.mktime(2000, 1, 1, 0, 0, 0), :time).should == Time.mktime(2000, 1, 1, 0, 0, 0)
end
it "should return dummy time value with same time part for time value with different date" do
type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :time).should == Time.mktime(2000, 1, 1, 0, 0, 0)
end
it "should return dummy time only for date value" do
type_cast_value(Date.new(2010, 1, 1), :time).should == Time.mktime(2000, 1, 1, 0, 0, 0)
end
it "should return dummy date with shifted local time for UTC datetime value" do
type_cast_value(DateTime.new(2010, 1, 1, 12, 34, 56), :time).should == Time.mktime(2000, 1, 1, 23, 34, 56)
end
it "should return dummy date with time part for local datetime value" do
type_cast_value(DateTime.civil_from_format(:local, 2010, 1, 1, 12, 34, 56), :time).should == Time.mktime(2000, 1, 1, 12, 34, 56)
end
end
describe "for datetime type" do
it "should return time in local offset for date value" do
type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local_time(2010, 1, 1, 0, 0, 0)
end
it "should return same value for same time value in local offset" do
type_cast_value(Time.local_time(2010, 1, 1, 12, 34, 56), :datetime).should == Time.local_time(2010, 1, 1, 12, 34, 56)
end
it "should return shifted local time value for UTC time value" do
type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime).should == Time.local_time(2010, 1, 1, 23, 34, 56)
end
it "should return shifted local time value for UTC datetime value" do
type_cast_value(DateTime.new(2010, 1, 1, 12, 34, 56), :datetime).should == Time.local_time(2010, 1, 1, 23, 34, 56)
end
it "should return time value with same component values for local datetime value" do
type_cast_value(DateTime.civil_from_format(:local, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.local_time(2010, 1, 1, 12, 34, 56)
end
end
end
describe "#dummy_time" do
it 'should return dummy date with shifted local time part for UTC time value' do
dummy_time(Time.utc(2010, 11, 22, 12, 34, 56)).should == Time.local_time(2000, 1, 1, 23, 34, 56)
end
it 'should return dummy date with same time part part for local time value with non-dummy date' do
dummy_time(Time.local_time(2010, 11, 22, 12, 34, 56)).should == Time.local_time(2000, 1, 1, 12, 34, 56)
end
it 'should return same value for local time with dummy date' do
dummy_time(Time.local_time(2000, 1, 1, 12, 34, 56)).should == Time.local_time(2000, 1, 1, 12, 34, 56)
end
it 'should return exact dummy time value for date value' do
dummy_time(Date.new(2010, 11, 22)).should == Time.mktime(2000, 1, 1, 0, 0, 0)
end
describe "with custom dummy date" do
before(:all) do
@@original_dummy_date = ValidatesTimeliness.dummy_date_for_time_type
ValidatesTimeliness.dummy_date_for_time_type = [2010, 1, 1]
end
it 'should return dummy time with custom dummy date' do
dummy_time(Time.local_time(1999, 11, 22, 12, 34, 56)).should == Time.local_time(2010, 1, 1, 12, 34, 56)
end
after(:all) do
ValidatesTimeliness.dummy_date_for_time_type = @@original_dummy_date
end
end
end
end

View File

@@ -0,0 +1,85 @@
require 'spec_helper'
describe ValidatesTimeliness::Validator, ":is_at option" do
include ModelHelpers
before do
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
end
describe "for date type" do
it "should be equal to same date value" do
Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1)
valid!(:birth_date, Date.new(2010, 1, 1))
end
it "should be equal to Time with same date part" do
Person.validates_date :birth_date, :is_at => Time.local_time(2010, 1, 1, 0, 0, 0)
valid!(:birth_date, Date.new(2010, 1, 1))
end
it "should be equal to DateTime with same date part" do
Person.validates_date :birth_date, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0)
valid!(:birth_date, Date.new(2010, 1, 1))
end
end
describe "for time type" do
it "should be equal to Date if attribute value is midnight" do
Person.validates_time :birth_time, :is_at => Date.new(2010, 1, 1)
valid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0))
end
it "should not be be equal to Date if attribute value is other than midnight" do
Person.validates_time :birth_time, :is_at => Date.new(2010, 1, 1)
invalid!(:birth_time, Time.local_time(2000, 1, 1, 9, 30, 0))
end
it "should be equal to local Time with same time part" do
Person.validates_time :birth_time, :is_at => Time.local_time(2010, 1, 1, 0, 0, 0)
valid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0))
end
it "should not be equal to UTC Time with same time part" do
Person.validates_time :birth_time, :is_at => Time.utc(2010, 1, 1, 0, 0, 0)
invalid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0))
end
it "should be equal to local DateTime with same time part" do
Person.validates_time :birth_time, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0)
valid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0))
end
it "should not be equal to UTC DateTime with same time part" do
Person.validates_time :birth_time, :is_at => DateTime.new(2010, 1, 1, 0, 0, 0)
invalid!(:birth_time, Time.local_time(2000, 1, 1, 0, 0, 0))
end
end
describe "for datetime type" do
it "should be equal to Date with same" do
Person.validates_datetime :birth_datetime, :is_at => Date.new(2010, 1, 1)
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0))
end
it "should be equal to local Time with same component values" do
Person.validates_datetime :birth_datetime, :is_at => Time.local_time(2010, 1, 1, 0, 0, 0)
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0))
end
it "should not be equal to UTC Time with same component values" do
Person.validates_datetime :birth_datetime, :is_at => Time.utc(2010, 1, 1, 0, 0, 0)
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0))
end
it "should be equal to same local DateTime value" do
Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0)
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0))
end
it "should not be equal to UTC DateTime with same component values" do
Person.validates_datetime :birth_datetime, :is_at => DateTime.new(2010, 1, 1, 0, 0, 0)
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 0, 0, 0))
end
end
end

View File

@@ -0,0 +1,49 @@
require 'spec_helper'
describe ValidatesTimeliness::Validator do
include ModelHelpers
NIL = [nil]
before do
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
end
it 'should return validator kind as :timeliness' do
ValidatesTimeliness::Validator.kind.should == :timeliness
end
describe "Model.validates :timeliness option" do
it 'should use plugin validator class' do
Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
Person.validators.should have(1).kind_of(TimelinessValidator)
valid!(:birth_date, Date.new(2010,1,1))
invalid!(:birth_date, Date.new(2010,1,2))
end
end
describe ":allow_nil option" do
it 'should not allow nil by default' do
Person.validates_datetime :birth_date
invalid!(:birth_date, NIL)
valid!(:birth_date, Date.today)
end
it 'should allow nil when true' do
Person.validates_datetime :birth_date, :allow_nil => true
valid!(:birth_date, NIL)
end
end
describe ":allow_blank option" do
it 'should not allow blank by default' do
Person.validates_datetime :birth_date
invalid!(:birth_date, '')
valid!(:birth_date, Date.today)
end
it 'should allow blank when true' do
Person.validates_datetime :birth_date, :allow_blank => true
valid!(:birth_date, '')
end
end
end