mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
refactored AR parsing methods into Parser module to reduce AR method pollution and make more consistent
This commit is contained in:
@@ -39,17 +39,17 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
end
|
||||
|
||||
it "should call parser on write for datetime attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
||||
end
|
||||
|
||||
it "should call parser on write for date attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_date = "2000-01-01"
|
||||
end
|
||||
|
||||
it "should call parser on write for time attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_time = "12:00"
|
||||
end
|
||||
|
||||
|
||||
61
spec/parser_spec.rb
Normal file
61
spec/parser_spec.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
describe ValidatesTimeliness::Parser do
|
||||
attr_accessor :person
|
||||
|
||||
describe "parse" do
|
||||
it "should return time object for valid time string" do
|
||||
parse("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it "should return nil for time string with invalid date part" do
|
||||
parse("2000-02-30 12:13:14", :datetime).should be_nil
|
||||
end
|
||||
|
||||
it "should return nil for time string with invalid time part" do
|
||||
parse("2000-02-01 25:13:14", :datetime).should be_nil
|
||||
end
|
||||
|
||||
it "should return Time object when passed a Time object" do
|
||||
parse(Time.now, :datetime).should be_kind_of(Time)
|
||||
end
|
||||
|
||||
if RAILS_VER >= '2.1'
|
||||
it "should convert time string into current timezone" do
|
||||
Time.zone = 'Melbourne'
|
||||
time = parse("2000-01-01 12:13:14", :datetime)
|
||||
Time.zone.utc_offset.should == 10.hours
|
||||
end
|
||||
end
|
||||
|
||||
it "should return nil for invalid date string" do
|
||||
parse("2000-02-30", :date).should be_nil
|
||||
end
|
||||
|
||||
def parse(*args)
|
||||
ValidatesTimeliness::Parser.parse(*args)
|
||||
end
|
||||
end
|
||||
|
||||
describe "make_time" do
|
||||
|
||||
if RAILS_VER >= '2.1'
|
||||
|
||||
it "should create time using current timezone" do
|
||||
Time.zone = 'Melbourne'
|
||||
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||
time.zone.should == "EST"
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
it "should create time using default timezone" do
|
||||
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||
time.zone.should == "UTC"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -30,6 +30,7 @@ require 'active_record'
|
||||
require 'active_record/version'
|
||||
require 'action_controller'
|
||||
require 'action_view'
|
||||
require 'action_mailer'
|
||||
|
||||
require 'spec/rails'
|
||||
require 'time_travel/time_travel'
|
||||
@@ -38,13 +39,13 @@ ActiveRecord::Base.default_timezone = :utc
|
||||
RAILS_VER = Rails::VERSION::STRING
|
||||
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
||||
|
||||
require 'validates_timeliness'
|
||||
|
||||
if RAILS_VER >= '2.1'
|
||||
Time.zone_default = ActiveSupport::TimeZone['UTC']
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
end
|
||||
|
||||
require 'validates_timeliness'
|
||||
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
it "should return array of Time objects when restriction is array of strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
evaluate_option_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value([time1, time2], :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is Range of Time objects" do
|
||||
@@ -76,7 +76,7 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
it "should return array of Time objects when restriction is Range of time strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
evaluate_option_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value(time1..time2, :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||
end
|
||||
def evaluate_option_value(restriction, type)
|
||||
configure_validator(:type => type)
|
||||
@@ -587,6 +587,10 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
end
|
||||
|
||||
def parse(*args)
|
||||
ValidatesTimeliness::Parser.parse(*args)
|
||||
end
|
||||
|
||||
def configure_validator(options={})
|
||||
@validator = ValidatesTimeliness::Validator.new(options)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user