mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bbb16cb093 | ||
|
|
b463b4356a | ||
|
|
7dd579b0e0 | ||
|
|
5becd7886b | ||
|
|
2225c747e1 |
@@ -25,3 +25,7 @@ end
|
||||
appraise "mongoid_2_4" do
|
||||
gem "mongoid", "~> 2.4.0"
|
||||
end
|
||||
|
||||
appraise "mongoid_3_0" do
|
||||
gem "mongoid", "~> 3.0.0"
|
||||
end
|
||||
|
||||
3
Gemfile
3
Gemfile
@@ -13,7 +13,8 @@ gem 'appraisal'
|
||||
gem 'sqlite3'
|
||||
|
||||
group :mongoid do
|
||||
gem 'mongoid', '~> 2.3.0'
|
||||
gem 'mongoid', '~> 3.0.0'
|
||||
gem 'mongo'
|
||||
gem 'bson_ext'
|
||||
gem 'system_timer', :platforms => [:ruby_18]
|
||||
end
|
||||
|
||||
2
Rakefile
2
Rakefile
@@ -5,7 +5,7 @@ require 'appraisal'
|
||||
|
||||
Bundler::GemHelper.install_tasks
|
||||
|
||||
require 'rake/rdoctask'
|
||||
require 'rdoc/task'
|
||||
require 'rspec/core/rake_task'
|
||||
|
||||
desc "Run specs"
|
||||
|
||||
16
gemfiles/mongoid_3_0.gemfile
Normal file
16
gemfiles/mongoid_3_0.gemfile
Normal file
@@ -0,0 +1,16 @@
|
||||
# This file was generated by Appraisal
|
||||
|
||||
source "http://rubygems.org"
|
||||
|
||||
gem "rails", "~> 3.2.6"
|
||||
gem "rspec", "~> 2.8"
|
||||
gem "rspec-rails", "~> 2.8"
|
||||
gem "timecop"
|
||||
gem "rspec_tag_matchers"
|
||||
gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
|
||||
gem "debugger", :platforms=>[:ruby_19]
|
||||
gem "appraisal"
|
||||
gem "sqlite3"
|
||||
gem "mongoid", "~> 3.0.0"
|
||||
|
||||
gemspec :path=>"../"
|
||||
16
gemfiles/mongoid_3_1.gemfile
Normal file
16
gemfiles/mongoid_3_1.gemfile
Normal file
@@ -0,0 +1,16 @@
|
||||
# This file was generated by Appraisal
|
||||
|
||||
source "http://rubygems.org"
|
||||
|
||||
gem "rails", "~> 3.2.6"
|
||||
gem "rspec", "~> 2.8"
|
||||
gem "rspec-rails", "~> 2.8"
|
||||
gem "timecop"
|
||||
gem "rspec_tag_matchers"
|
||||
gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
|
||||
gem "debugger", :platforms=>[:ruby_19]
|
||||
gem "appraisal"
|
||||
gem "sqlite3"
|
||||
gem "mongoid", "~> 3.1.0"
|
||||
|
||||
gemspec :path=>"../"
|
||||
@@ -24,26 +24,24 @@ module ValidatesTimeliness
|
||||
|
||||
class << self
|
||||
delegate :default_timezone, :default_timezone=, :dummy_date_for_time_type, :dummy_date_for_time_type=, :to => Timeliness
|
||||
|
||||
attr_accessor :extend_orms, :ignore_restriction_errors, :restriction_shorthand_symbols, :use_plugin_parser
|
||||
end
|
||||
|
||||
# Extend ORM/ODMs for full support (:active_record, :mongoid).
|
||||
mattr_accessor :extend_orms
|
||||
@@extend_orms = []
|
||||
self.extend_orms = []
|
||||
|
||||
# Ignore errors when restriction options are evaluated
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
@@ignore_restriction_errors = false
|
||||
self.ignore_restriction_errors = false
|
||||
|
||||
# Shorthand time and date symbols for restrictions
|
||||
mattr_accessor :restriction_shorthand_symbols
|
||||
@@restriction_shorthand_symbols = {
|
||||
self.restriction_shorthand_symbols = {
|
||||
:now => lambda { Time.current },
|
||||
:today => lambda { Date.current }
|
||||
}
|
||||
|
||||
# Use the plugin date/time parser which is stricter and extensible
|
||||
mattr_accessor :use_plugin_parser
|
||||
@@use_plugin_parser = false
|
||||
self.use_plugin_parser = false
|
||||
|
||||
# Default timezone
|
||||
self.default_timezone = :utc
|
||||
@@ -51,10 +49,6 @@ module ValidatesTimeliness
|
||||
# Set the dummy date part for a time type values.
|
||||
self.dummy_date_for_time_type = [ 2000, 1, 1 ]
|
||||
|
||||
def self.parser
|
||||
Timeliness
|
||||
end
|
||||
|
||||
# Setup method for plugin configuration
|
||||
def self.setup
|
||||
yield self
|
||||
@@ -64,6 +58,8 @@ module ValidatesTimeliness
|
||||
def self.load_orms
|
||||
extend_orms.each {|orm| require "validates_timeliness/orm/#{orm}" }
|
||||
end
|
||||
|
||||
def self.parser; Timeliness end
|
||||
end
|
||||
|
||||
require 'validates_timeliness/conversion'
|
||||
|
||||
6
spec/support/mongoid.yml
Normal file
6
spec/support/mongoid.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
test:
|
||||
sessions:
|
||||
default:
|
||||
database: mongoid
|
||||
hosts:
|
||||
- localhost:27017
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ValidatesTimeliness, 'HelperMethods' do
|
||||
let(:record) { Person.new }
|
||||
|
||||
it 'should define class validation methods' do
|
||||
Person.should respond_to(:validates_date)
|
||||
Person.should respond_to(:validates_time)
|
||||
@@ -8,23 +10,21 @@ describe ValidatesTimeliness, 'HelperMethods' do
|
||||
end
|
||||
|
||||
it 'should define instance validation methods' do
|
||||
Person.new.should respond_to(:validates_date)
|
||||
Person.new.should respond_to(:validates_time)
|
||||
Person.new.should respond_to(:validates_datetime)
|
||||
record.should respond_to(:validates_date)
|
||||
record.should respond_to(:validates_time)
|
||||
record.should respond_to(:validates_datetime)
|
||||
end
|
||||
|
||||
it 'should validate instance using class validation defined' do
|
||||
Person.validates_date :birth_date
|
||||
r = Person.new
|
||||
r.valid?
|
||||
record.valid?
|
||||
|
||||
r.errors[:birth_date].should_not be_empty
|
||||
record.errors[:birth_date].should_not be_empty
|
||||
end
|
||||
|
||||
it 'should validate instance using instance valiation method' do
|
||||
r = Person.new
|
||||
r.validates_date :birth_date
|
||||
record.validates_date :birth_date
|
||||
|
||||
r.errors[:birth_date].should_not be_empty
|
||||
record.errors[:birth_date].should_not be_empty
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,8 @@ require 'spec_helper'
|
||||
describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
|
||||
context "validation methods" do
|
||||
let(:record) { Employee.new }
|
||||
|
||||
it 'should be defined for the class' do
|
||||
ActiveRecord::Base.should respond_to(:validates_date)
|
||||
ActiveRecord::Base.should respond_to(:validates_time)
|
||||
@@ -10,33 +12,30 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
end
|
||||
|
||||
it 'should defines for the instance' do
|
||||
Employee.new.should respond_to(:validates_date)
|
||||
Employee.new.should respond_to(:validates_time)
|
||||
Employee.new.should respond_to(:validates_datetime)
|
||||
record.should respond_to(:validates_date)
|
||||
record.should respond_to(:validates_time)
|
||||
record.should respond_to(:validates_datetime)
|
||||
end
|
||||
|
||||
it "should validate a valid value string" do
|
||||
r = Employee.new
|
||||
r.birth_date = '2012-01-01'
|
||||
record.birth_date = '2012-01-01'
|
||||
|
||||
r.valid?
|
||||
r.errors[:birth_date].should be_empty
|
||||
record.valid?
|
||||
record.errors[:birth_date].should be_empty
|
||||
end
|
||||
|
||||
it "should validate a invalid value string" do
|
||||
r = Employee.new
|
||||
r.birth_date = 'not a date'
|
||||
record.birth_date = 'not a date'
|
||||
|
||||
r.valid?
|
||||
r.errors[:birth_date].should_not be_empty
|
||||
record.valid?
|
||||
record.errors[:birth_date].should_not be_empty
|
||||
end
|
||||
|
||||
it "should validate a nil value" do
|
||||
r = Employee.new
|
||||
r.birth_date = nil
|
||||
record.birth_date = nil
|
||||
|
||||
r.valid?
|
||||
r.errors[:birth_date].should be_empty
|
||||
record.valid?
|
||||
record.errors[:birth_date].should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
@@ -83,36 +82,36 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
end
|
||||
|
||||
context 'value cache' do
|
||||
let(:record) { EmployeeWithCache.new }
|
||||
|
||||
context 'for datetime column' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_datetime = datetime_string = '2010-01-01 12:30'
|
||||
record.birth_datetime = datetime_string = '2010-01-01 12:30'
|
||||
|
||||
r._timeliness_raw_value_for('birth_datetime').should eq datetime_string
|
||||
record._timeliness_raw_value_for('birth_datetime').should eq datetime_string
|
||||
end
|
||||
end
|
||||
|
||||
context 'for date column' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_date = date_string = '2010-01-01'
|
||||
record.birth_date = date_string = '2010-01-01'
|
||||
|
||||
r._timeliness_raw_value_for('birth_date').should eq date_string
|
||||
record._timeliness_raw_value_for('birth_date').should eq date_string
|
||||
end
|
||||
end
|
||||
|
||||
context 'for time column' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_time = time_string = '12:12'
|
||||
record.birth_time = time_string = '12:12'
|
||||
|
||||
r._timeliness_raw_value_for('birth_time').should eq time_string
|
||||
record._timeliness_raw_value_for('birth_time').should eq time_string
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
with_config(:use_plugin_parser, true)
|
||||
let(:record) { EmployeeWithParser.new }
|
||||
|
||||
class EmployeeWithParser < ActiveRecord::Base
|
||||
self.table_name = 'employees'
|
||||
@@ -125,22 +124,20 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = '2010-01-01'
|
||||
record.birth_date = '2010-01-01'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = 'not valid'
|
||||
|
||||
record.birth_date = 'not valid'
|
||||
end
|
||||
|
||||
it 'should store a Date value after parsing string' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = '2010-01-01'
|
||||
record.birth_date = '2010-01-01'
|
||||
|
||||
r.birth_date.should be_kind_of(Date)
|
||||
r.birth_date.should eq Date.new(2010, 1, 1)
|
||||
record.birth_date.should be_kind_of(Date)
|
||||
record.birth_date.should eq Date.new(2010, 1, 1)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -148,23 +145,20 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = '12:30'
|
||||
record.birth_time = '12:30'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = 'not valid'
|
||||
record.birth_time = 'not valid'
|
||||
end
|
||||
|
||||
it 'should store a Time value after parsing string' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = '12:30'
|
||||
record.birth_time = '12:30'
|
||||
|
||||
r.birth_time.should be_kind_of(Time)
|
||||
r.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
|
||||
record.birth_time.should be_kind_of(Time)
|
||||
record.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -174,29 +168,25 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-01-01 12:00'
|
||||
record.birth_datetime = '2010-01-01 12:00'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = 'not valid'
|
||||
record.birth_datetime = 'not valid'
|
||||
end
|
||||
|
||||
it 'should parse string into Time value' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-01-01 12:00'
|
||||
record.birth_datetime = '2010-01-01 12:00'
|
||||
|
||||
r.birth_datetime.should be_kind_of(Time)
|
||||
record.birth_datetime.should be_kind_of(Time)
|
||||
end
|
||||
|
||||
it 'should parse string as current timezone' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-06-01 12:00'
|
||||
record.birth_datetime = '2010-06-01 12:00'
|
||||
|
||||
r.birth_datetime.utc_offset.should eq Time.zone.utc_offset
|
||||
record.birth_datetime.utc_offset.should eq Time.zone.utc_offset
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -204,43 +194,43 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
|
||||
context "reload" do
|
||||
it 'should clear cache value' do
|
||||
r = Employee.create!
|
||||
r.birth_date = '2010-01-01'
|
||||
record = Employee.create!
|
||||
record.birth_date = '2010-01-01'
|
||||
|
||||
r.reload
|
||||
record.reload
|
||||
|
||||
r._timeliness_raw_value_for('birth_date').should be_nil
|
||||
record._timeliness_raw_value_for('birth_date').should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "before_type_cast method" do
|
||||
let(:record) { Employee.new }
|
||||
|
||||
it 'should be defined on class if ORM supports it' do
|
||||
Employee.new.should respond_to(:birth_datetime_before_type_cast)
|
||||
record.should respond_to(:birth_datetime_before_type_cast)
|
||||
end
|
||||
|
||||
it 'should return original value' do
|
||||
r = Employee.new
|
||||
r.birth_datetime = date_string = '2010-01-01'
|
||||
record.birth_datetime = date_string = '2010-01-01'
|
||||
|
||||
r.birth_datetime_before_type_cast.should eq date_string
|
||||
record.birth_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
|
||||
it 'should return attribute if no attribute assignment has been made' do
|
||||
datetime = Time.zone.local(2010,01,01)
|
||||
Employee.create(:birth_datetime => datetime)
|
||||
|
||||
r = Employee.last
|
||||
r.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
|
||||
record = Employee.last
|
||||
record.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
with_config(:use_plugin_parser, true)
|
||||
|
||||
it 'should return original value' do
|
||||
r = Employee.new
|
||||
r.birth_datetime = date_string = '2010-01-31'
|
||||
record.birth_datetime = date_string = '2010-01-31'
|
||||
|
||||
r.birth_datetime_before_type_cast.should eq date_string
|
||||
record.birth_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,13 +4,19 @@ require 'spec_helper'
|
||||
begin
|
||||
|
||||
require 'mongoid'
|
||||
require 'mongoid/version'
|
||||
require 'validates_timeliness/orm/mongoid'
|
||||
|
||||
Mongoid.configure do |config|
|
||||
name = "validates_timeliness_test"
|
||||
host = "localhost"
|
||||
config.master = Mongo::Connection.new.db(name)
|
||||
config.persist_in_safe_mode = false
|
||||
if Mongoid::VERSION >= '3.0'
|
||||
Mongoid.load!("spec/support/mongoid.yml", :test)
|
||||
else
|
||||
require 'mongo'
|
||||
Mongoid.configure do |config|
|
||||
name = "validates_timeliness_test"
|
||||
host = "localhost"
|
||||
config.master = Mongo::Connection.new.db(name)
|
||||
config.persist_in_safe_mode = false
|
||||
end
|
||||
end
|
||||
|
||||
describe ValidatesTimeliness, 'Mongoid' do
|
||||
@@ -47,9 +53,19 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.errors[:publish_date].should be_empty
|
||||
end
|
||||
|
||||
it "should validate a invalid value string" do
|
||||
it "should validate an invalid time value string" do
|
||||
begin
|
||||
record.publish_date = 'not a date'
|
||||
record.publish_time = 'not valid'
|
||||
rescue
|
||||
end
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_time].should_not be_empty
|
||||
end
|
||||
|
||||
it "should validate an invalid date value string" do
|
||||
begin
|
||||
record.publish_date = 'not valid'
|
||||
rescue
|
||||
end
|
||||
|
||||
@@ -57,12 +73,36 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.errors[:publish_date].should_not be_empty
|
||||
end
|
||||
|
||||
it "should validate a nil value" do
|
||||
it "should validate an invalid datetime value string" do
|
||||
begin
|
||||
record.publish_datetime = 'not valid'
|
||||
rescue
|
||||
end
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_datetime].should_not be_empty
|
||||
end
|
||||
|
||||
it "should accept a nil time value" do
|
||||
record.publish_date = nil
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_time].should be_empty
|
||||
end
|
||||
|
||||
it "should accept a nil date value" do
|
||||
record.publish_date = nil
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_date].should be_empty
|
||||
end
|
||||
|
||||
it "should accept a nil datetime value" do
|
||||
record.publish_datetime = nil
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_datetime].should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it 'should determine type for attribute' do
|
||||
@@ -105,6 +145,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
record.publish_date = 'not valid'
|
||||
|
||||
record.publish_date.should be_nil
|
||||
end
|
||||
|
||||
it 'should store a Date value after parsing string' do
|
||||
@@ -149,6 +191,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
record.publish_datetime = 'not valid'
|
||||
|
||||
record.publish_datetime.should be_nil
|
||||
end
|
||||
|
||||
it 'should parse string into DateTime value' do
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ValidatesTimeliness::Validator do
|
||||
NIL = [nil]
|
||||
|
||||
before do
|
||||
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
|
||||
end
|
||||
@@ -46,13 +44,13 @@ describe ValidatesTimeliness::Validator do
|
||||
describe ":allow_nil option" do
|
||||
it 'should not allow nil by default' do
|
||||
Person.validates_date :birth_date
|
||||
invalid!(:birth_date, NIL, 'is not a valid date')
|
||||
invalid!(:birth_date, [nil], 'is not a valid date')
|
||||
valid!(:birth_date, Date.today)
|
||||
end
|
||||
|
||||
it 'should allow nil when true' do
|
||||
Person.validates_date :birth_date, :allow_nil => true
|
||||
valid!(:birth_date, NIL)
|
||||
valid!(:birth_date, [nil])
|
||||
end
|
||||
|
||||
context "with raw value cache" do
|
||||
|
||||
Reference in New Issue
Block a user