mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ae49b8a0 | ||
|
|
2fef0a23d6 | ||
|
|
12f6d2a467 | ||
|
|
913bd4ccfa | ||
|
|
f1b8925a36 | ||
|
|
f983dcc9dc | ||
|
|
7be1539cee | ||
|
|
99ae8fe7d7 | ||
|
|
00ce472d3e | ||
|
|
0d2c7ce554 | ||
|
|
3d798697e1 | ||
|
|
dc0fdc0340 | ||
|
|
dd3b6b5514 | ||
|
|
609fafe7bb | ||
|
|
df9677f5bf |
@@ -1,3 +1,8 @@
|
||||
= 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.
|
||||
|
||||
|
||||
2
Gemfile
2
Gemfile
@@ -13,7 +13,7 @@ gem 'appraisal'
|
||||
gem 'sqlite3'
|
||||
|
||||
group :mongoid do
|
||||
gem 'mongoid', '~> 2.3.0'
|
||||
gem 'mongoid', '>= 3.0'
|
||||
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, you there are a number of config
|
||||
This creates configuration initializer and locale files. In the initializer, 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,7 +55,10 @@ 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']
|
||||
|
||||
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 :breakfast_time, :on_or_after => '6:00am',
|
||||
:on_or_after_message => 'must be after opening time',
|
||||
@@ -173,8 +176,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_before and :on_or_after messages.
|
||||
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.
|
||||
|
||||
It is highly recommended you use the I18n system for error messages.
|
||||
|
||||
|
||||
@@ -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 amiguous year threshold when parsing a 2 digit year
|
||||
# Change the ambiguous 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.
|
||||
|
||||
@@ -6,7 +6,7 @@ module ValidatesTimeliness
|
||||
# It is best to use the plugin parser to avoid errors on a bad
|
||||
# field value in Mongoid. Parser will return nil rather than error.
|
||||
|
||||
module ClassMethods
|
||||
module ClassMethods
|
||||
public
|
||||
|
||||
# Mongoid has no bulk attribute method definition hook. It defines
|
||||
@@ -23,7 +23,7 @@ module ValidatesTimeliness
|
||||
Date => :date,
|
||||
Time => :time,
|
||||
DateTime => :datetime
|
||||
}[fields[attr_name.to_s].type] || :datetime
|
||||
}[fields[database_field_name(attr_name)].type] || :datetime
|
||||
end
|
||||
|
||||
protected
|
||||
@@ -33,31 +33,17 @@ module ValidatesTimeliness
|
||||
|
||||
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module Reload
|
||||
def reload(*args)
|
||||
_clear_timeliness_cache
|
||||
super
|
||||
end
|
||||
def reload(*args)
|
||||
_clear_timeliness_cache
|
||||
super
|
||||
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,3 +1,4 @@
|
||||
require 'active_model'
|
||||
require 'active_model/validator'
|
||||
|
||||
module ValidatesTimeliness
|
||||
@@ -32,7 +33,12 @@ 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], options[:on_or_before] = range.first, range.last
|
||||
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
|
||||
end
|
||||
|
||||
@restrictions_to_check = RESTRICTIONS.keys & options.keys
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = '3.0.14'
|
||||
VERSION = '3.0.15'
|
||||
end
|
||||
|
||||
@@ -57,6 +57,7 @@ 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
|
||||
|
||||
@@ -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.local(2000, 2, 28, 12, 0, 0)
|
||||
employee.birth_datetime_before_type_cast.should eq Time.zone.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(/2010-01-01 00:00:00/)
|
||||
record.birth_datetime_before_type_cast.should match(/#{datetime.utc.to_s[0...-4]}/)
|
||||
end
|
||||
|
||||
context "with plugin parser" do
|
||||
|
||||
@@ -7,13 +7,13 @@ require 'mongoid'
|
||||
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
|
||||
config.connect_to('validates_timeliness_test')
|
||||
end
|
||||
|
||||
describe ValidatesTimeliness, 'Mongoid' do
|
||||
after(:each) do
|
||||
Mongoid.purge!
|
||||
end
|
||||
|
||||
class Article
|
||||
include Mongoid::Document
|
||||
@@ -47,16 +47,6 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.errors[:publish_date].should be_empty
|
||||
end
|
||||
|
||||
it "should validate a invalid value string" do
|
||||
begin
|
||||
record.publish_date = 'not a date'
|
||||
rescue
|
||||
end
|
||||
|
||||
record.valid?
|
||||
record.errors[:publish_date].should_not be_empty
|
||||
end
|
||||
|
||||
it "should validate a nil value" do
|
||||
record.publish_date = nil
|
||||
|
||||
@@ -67,6 +57,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
|
||||
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
|
||||
@@ -157,7 +149,7 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
record.publish_datetime.should be_kind_of(DateTime)
|
||||
end
|
||||
|
||||
pending 'should parse string as current timezone' do
|
||||
it '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
|
||||
@@ -176,8 +168,50 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
end
|
||||
|
||||
context "before_type_cast method" do
|
||||
it 'should not be defined if ORM does not support it' do
|
||||
Article.new.should_not respond_to(:publish_datetime_before_type_cast)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -115,6 +115,19 @@ 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
|
||||
@@ -224,7 +237,7 @@ describe ValidatesTimeliness::Validator do
|
||||
Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
|
||||
invalid!(:birth_date, 'asdf', 'custom invalid message')
|
||||
end
|
||||
|
||||
|
||||
it 'should be used for invalid restriction' do
|
||||
Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
|
||||
invalid!(:birth_date, Time.now, 'custom before message')
|
||||
|
||||
@@ -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.6"])
|
||||
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user