mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 23:33:00 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bbb16cb093 |
@@ -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
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
= 3.0.15 [2015-12-29]
|
||||
* Fixes mongoid 3 support and removes mongoid 2 support(johnnyshields)
|
||||
* Some documentation/comments tidying
|
||||
* Some general tidying up
|
||||
|
||||
= 3.0.14 [2012-08-23]
|
||||
* Fix for using validates :timeliness => {} form to correctly add attributes to timeliness validated attributes.
|
||||
|
||||
|
||||
3
Gemfile
3
Gemfile
@@ -13,7 +13,8 @@ gem 'appraisal'
|
||||
gem 'sqlite3'
|
||||
|
||||
group :mongoid do
|
||||
gem 'mongoid', '>= 3.0'
|
||||
gem 'mongoid', '~> 3.0.0'
|
||||
gem 'mongo'
|
||||
gem 'bson_ext'
|
||||
gem 'system_timer', :platforms => [:ruby_18]
|
||||
end
|
||||
|
||||
11
README.rdoc
11
README.rdoc
@@ -39,7 +39,7 @@ Then run
|
||||
|
||||
$ rails generate validates_timeliness:install
|
||||
|
||||
This creates configuration initializer and locale files. In the initializer, there are a number of config
|
||||
This creates configuration initializer and locale files. In the initializer, you there are a number of config
|
||||
options to customize the plugin.
|
||||
|
||||
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
||||
@@ -55,10 +55,7 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
|
||||
validates_datetime :finish_time, :after => :start_time # Method symbol
|
||||
|
||||
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
|
||||
|
||||
validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
|
||||
validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
|
||||
validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
|
||||
validates_time :booked_at, :between => ['9.00am', '5:00pm']
|
||||
|
||||
validates_time :breakfast_time, :on_or_after => '6:00am',
|
||||
:on_or_after_message => 'must be after opening time',
|
||||
@@ -176,8 +173,8 @@ You can also use validation options for custom error messages. The following opt
|
||||
:after_message
|
||||
:on_or_after_message
|
||||
|
||||
Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before
|
||||
(:before in case when :between argument is a Range with excluded high value, see Examples) messages.
|
||||
Note: There is no :between_message option. The between error message should be defined using the
|
||||
:on_or_before and :on_or_after messages.
|
||||
|
||||
It is highly recommended you use the I18n system for error messages.
|
||||
|
||||
|
||||
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=>"../"
|
||||
@@ -32,7 +32,7 @@ ValidatesTimeliness.setup do |config|
|
||||
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
|
||||
# config.parser.remove_formats()
|
||||
#
|
||||
# Change the ambiguous year threshold when parsing a 2 digit year
|
||||
# Change the amiguous year threshold when parsing a 2 digit year
|
||||
# config.parser.ambiguous_year_threshold = 30
|
||||
#
|
||||
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
|
||||
|
||||
@@ -23,7 +23,7 @@ module ValidatesTimeliness
|
||||
Date => :date,
|
||||
Time => :time,
|
||||
DateTime => :datetime
|
||||
}[fields[database_field_name(attr_name)].type] || :datetime
|
||||
}[fields[attr_name.to_s].type] || :datetime
|
||||
end
|
||||
|
||||
protected
|
||||
@@ -33,8 +33,10 @@ module ValidatesTimeliness
|
||||
|
||||
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module Reload
|
||||
def reload(*args)
|
||||
_clear_timeliness_cache
|
||||
super
|
||||
@@ -42,8 +44,20 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Mongoid::Document
|
||||
include ValidatesTimeliness::AttributeMethods
|
||||
include ValidatesTimeliness::ORM::Mongoid
|
||||
|
||||
# Pre-2.3 reload
|
||||
if (instance_methods & ['reload', :reload]).present?
|
||||
def reload_with_timeliness
|
||||
_clear_timeliness_cache
|
||||
reload_without_timeliness
|
||||
end
|
||||
alias_method_chain :reload, :timeliness
|
||||
else
|
||||
include ValidatesTimeliness::ORM::Mongoid::Reload
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
require 'active_model'
|
||||
require 'active_model/validator'
|
||||
|
||||
module ValidatesTimeliness
|
||||
@@ -33,12 +32,7 @@ module ValidatesTimeliness
|
||||
|
||||
if range = options.delete(:between)
|
||||
raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
|
||||
options[:on_or_after] = range.first
|
||||
if range.is_a?(Range) && range.exclude_end?
|
||||
options[:before] = range.last
|
||||
else
|
||||
options[:on_or_before] = range.last
|
||||
end
|
||||
options[:on_or_after], options[:on_or_before] = range.first, range.last
|
||||
end
|
||||
|
||||
@restrictions_to_check = RESTRICTIONS.keys & options.keys
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = '3.0.15'
|
||||
VERSION = '3.0.14'
|
||||
end
|
||||
|
||||
@@ -57,7 +57,6 @@ class PersonWithShim < Person
|
||||
include TestModelShim
|
||||
end
|
||||
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||
ActiveRecord::Migration.verbose = false
|
||||
|
||||
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
|
||||
@@ -10,7 +10,7 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
|
||||
|
||||
it 'should assign a Time value for valid datetimes' do
|
||||
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
|
||||
employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0)
|
||||
employee.birth_datetime_before_type_cast.should eq Time.local(2000, 2, 28, 12, 0, 0)
|
||||
end
|
||||
|
||||
it 'should assign a string value for incomplete time' do
|
||||
|
||||
@@ -221,7 +221,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
Employee.create(:birth_datetime => datetime)
|
||||
|
||||
record = Employee.last
|
||||
record.birth_datetime_before_type_cast.should match(/#{datetime.utc.to_s[0...-4]}/)
|
||||
record.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
|
||||
@@ -4,16 +4,22 @@ require 'spec_helper'
|
||||
begin
|
||||
|
||||
require 'mongoid'
|
||||
require 'mongoid/version'
|
||||
require 'validates_timeliness/orm/mongoid'
|
||||
|
||||
if Mongoid::VERSION >= '3.0'
|
||||
Mongoid.load!("spec/support/mongoid.yml", :test)
|
||||
else
|
||||
require 'mongo'
|
||||
Mongoid.configure do |config|
|
||||
config.connect_to('validates_timeliness_test')
|
||||
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
|
||||
after(:each) do
|
||||
Mongoid.purge!
|
||||
end
|
||||
|
||||
class Article
|
||||
include Mongoid::Document
|
||||
@@ -47,18 +53,60 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.errors[:publish_date].should be_empty
|
||||
end
|
||||
|
||||
it "should validate a nil value" do
|
||||
it "should validate an invalid time value string" do
|
||||
begin
|
||||
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
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_date].should_not be_empty
|
||||
end
|
||||
|
||||
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
|
||||
Article.timeliness_attribute_type(:publish_date).should == :date
|
||||
Article.timeliness_attribute_type(:publish_time).should == :time
|
||||
Article.timeliness_attribute_type(:publish_datetime).should == :datetime
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
@@ -97,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
|
||||
@@ -141,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
|
||||
@@ -149,7 +201,7 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.publish_datetime.should be_kind_of(DateTime)
|
||||
end
|
||||
|
||||
it 'should parse string as current timezone' do
|
||||
pending 'should parse string as current timezone' do
|
||||
record.publish_datetime = '2010-06-01 12:00'
|
||||
|
||||
record.publish_datetime.utc_offset.should eq Time.zone.utc_offset
|
||||
@@ -168,50 +220,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
end
|
||||
|
||||
context "before_type_cast method" do
|
||||
let(:record){ Article.new }
|
||||
|
||||
it 'should be defined on class if ORM supports it' do
|
||||
record.should respond_to(:publish_datetime_before_type_cast)
|
||||
end
|
||||
|
||||
it 'should return original value' do
|
||||
record.publish_datetime = date_string = '2010-01-01'
|
||||
|
||||
record.publish_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
|
||||
it 'should return attribute if no attribute assignment has been made' do
|
||||
time = Time.zone.local(2010,01,01)
|
||||
Article.create(:publish_datetime => time)
|
||||
record = Article.last
|
||||
record.publish_datetime_before_type_cast.should eq time.to_datetime
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
with_config(:use_plugin_parser, true)
|
||||
|
||||
it 'should return original value' do
|
||||
record.publish_datetime = date_string = '2010-01-31'
|
||||
record.publish_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with aliased fields" do
|
||||
class ArticleWithAliasedFields
|
||||
include Mongoid::Document
|
||||
field :pd, as: :publish_date, :type => Date
|
||||
field :pt, as: :publish_time, :type => Time
|
||||
field :pdt, as: :publish_datetime, :type => DateTime
|
||||
validates_date :publish_date, :allow_nil => true
|
||||
validates_time :publish_time, :allow_nil => true
|
||||
validates_datetime :publish_datetime, :allow_nil => true
|
||||
end
|
||||
|
||||
it 'should determine type for attribute' do
|
||||
ArticleWithAliasedFields.timeliness_attribute_type(:publish_date).should == :date
|
||||
ArticleWithAliasedFields.timeliness_attribute_type(:publish_time).should == :time
|
||||
ArticleWithAliasedFields.timeliness_attribute_type(:publish_datetime).should == :datetime
|
||||
it 'should not be defined if ORM does not support it' do
|
||||
Article.new.should_not respond_to(:publish_datetime_before_type_cast)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -115,19 +115,6 @@ describe ValidatesTimeliness::Validator do
|
||||
valid!(:birth_date, on_or_before)
|
||||
end
|
||||
end
|
||||
|
||||
describe "range with excluded end value" do
|
||||
it 'should be split option into :on_or_after and :before values' do
|
||||
on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
|
||||
Person.validates_date :birth_date, :between => on_or_after...before
|
||||
Person.validators.first.options[:on_or_after].should == on_or_after
|
||||
Person.validators.first.options[:before].should == before
|
||||
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
||||
invalid!(:birth_date, before, "must be before 2010-01-03")
|
||||
valid!(:birth_date, on_or_after)
|
||||
valid!(:birth_date, before - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ":ignore_usec option" do
|
||||
|
||||
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
|
||||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
||||
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
|
||||
|
||||
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
|
||||
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.6"])
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user