mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ae49b8a0 | ||
|
|
2fef0a23d6 | ||
|
|
12f6d2a467 | ||
|
|
913bd4ccfa | ||
|
|
f1b8925a36 | ||
|
|
f983dcc9dc | ||
|
|
7be1539cee | ||
|
|
99ae8fe7d7 |
@@ -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]
|
= 3.0.14 [2012-08-23]
|
||||||
* Fix for using validates :timeliness => {} form to correctly add attributes to timeliness validated attributes.
|
* 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'
|
gem 'sqlite3'
|
||||||
|
|
||||||
group :mongoid do
|
group :mongoid do
|
||||||
gem 'mongoid', '~> 2.3.0'
|
gem 'mongoid', '>= 3.0'
|
||||||
gem 'bson_ext'
|
gem 'bson_ext'
|
||||||
gem 'system_timer', :platforms => [:ruby_18]
|
gem 'system_timer', :platforms => [:ruby_18]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ Then run
|
|||||||
|
|
||||||
$ rails generate validates_timeliness:install
|
$ 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.
|
options to customize the plugin.
|
||||||
|
|
||||||
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ ValidatesTimeliness.setup do |config|
|
|||||||
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
|
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
|
||||||
# config.parser.remove_formats()
|
# 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
|
# config.parser.ambiguous_year_threshold = 30
|
||||||
#
|
#
|
||||||
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
|
# 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
|
# 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.
|
# field value in Mongoid. Parser will return nil rather than error.
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
public
|
public
|
||||||
|
|
||||||
# Mongoid has no bulk attribute method definition hook. It defines
|
# Mongoid has no bulk attribute method definition hook. It defines
|
||||||
@@ -23,7 +23,7 @@ module ValidatesTimeliness
|
|||||||
Date => :date,
|
Date => :date,
|
||||||
Time => :time,
|
Time => :time,
|
||||||
DateTime => :datetime
|
DateTime => :datetime
|
||||||
}[fields[attr_name.to_s].type] || :datetime
|
}[fields[database_field_name(attr_name)].type] || :datetime
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@@ -33,31 +33,17 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
"#{var_name} = Timeliness::Parser.parse(value, :#{type})"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module Reload
|
def reload(*args)
|
||||||
def reload(*args)
|
_clear_timeliness_cache
|
||||||
_clear_timeliness_cache
|
super
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Mongoid::Document
|
module Mongoid::Document
|
||||||
include ValidatesTimeliness::AttributeMethods
|
include ValidatesTimeliness::AttributeMethods
|
||||||
include ValidatesTimeliness::ORM::Mongoid
|
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
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
VERSION = '3.0.14'
|
VERSION = '3.0.15'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ require 'mongoid'
|
|||||||
require 'validates_timeliness/orm/mongoid'
|
require 'validates_timeliness/orm/mongoid'
|
||||||
|
|
||||||
Mongoid.configure do |config|
|
Mongoid.configure do |config|
|
||||||
name = "validates_timeliness_test"
|
config.connect_to('validates_timeliness_test')
|
||||||
host = "localhost"
|
|
||||||
config.master = Mongo::Connection.new.db(name)
|
|
||||||
config.persist_in_safe_mode = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ValidatesTimeliness, 'Mongoid' do
|
describe ValidatesTimeliness, 'Mongoid' do
|
||||||
|
after(:each) do
|
||||||
|
Mongoid.purge!
|
||||||
|
end
|
||||||
|
|
||||||
class Article
|
class Article
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
@@ -47,16 +47,6 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|||||||
record.errors[:publish_date].should be_empty
|
record.errors[:publish_date].should be_empty
|
||||||
end
|
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
|
it "should validate a nil value" do
|
||||||
record.publish_date = nil
|
record.publish_date = nil
|
||||||
|
|
||||||
@@ -67,6 +57,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|||||||
|
|
||||||
it 'should determine type for attribute' do
|
it 'should determine type for attribute' do
|
||||||
Article.timeliness_attribute_type(:publish_date).should == :date
|
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
|
end
|
||||||
|
|
||||||
context "attribute write method" do
|
context "attribute write method" do
|
||||||
@@ -157,7 +149,7 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|||||||
record.publish_datetime.should be_kind_of(DateTime)
|
record.publish_datetime.should be_kind_of(DateTime)
|
||||||
end
|
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 = '2010-06-01 12:00'
|
||||||
|
|
||||||
record.publish_datetime.utc_offset.should eq Time.zone.utc_offset
|
record.publish_datetime.utc_offset.should eq Time.zone.utc_offset
|
||||||
@@ -176,8 +168,50 @@ describe ValidatesTimeliness, 'Mongoid' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "before_type_cast method" do
|
context "before_type_cast method" do
|
||||||
it 'should not be defined if ORM does not support it' do
|
let(:record){ Article.new }
|
||||||
Article.new.should_not respond_to(:publish_datetime_before_type_cast)
|
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user