mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Compare commits
21 Commits
v5.0.0.alp
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16221ac092 | ||
|
|
c0c42edd3f | ||
|
|
f8b91e9cea | ||
|
|
7fa4d85ee3 | ||
|
|
70307293c6 | ||
|
|
f42e905cd1 | ||
|
|
35eb50aa40 | ||
|
|
3134072f01 | ||
|
|
797ba48036 | ||
|
|
f8e6480f58 | ||
|
|
3835b2b161 | ||
|
|
00d038bc6f | ||
|
|
a3d0182b5e | ||
|
|
46296f914f | ||
|
|
4447361743 | ||
|
|
4523138c3c | ||
|
|
72807b87a7 | ||
|
|
9daf12c4a1 | ||
|
|
2a80683683 | ||
|
|
3a2882be75 | ||
|
|
bf1c808846 |
@ -8,4 +8,4 @@ end
|
||||
|
||||
appraise "rails_5_2" do
|
||||
gem "rails", "~> 5.2.0"
|
||||
end
|
||||
end
|
||||
|
||||
@ -3,12 +3,19 @@
|
||||
* Relaxed Timeliness dependency version which allows for >= 0.4.0 with
|
||||
threadsafety fix for use_us_formats and use_euro_formats for hot switching
|
||||
in a request.
|
||||
* Add initializer to ensure Timeliness v0.4+ ambiguous date config is set
|
||||
correctly when using `use_euro_formats` or `remove_use_formats'.
|
||||
|
||||
Breaking Changes
|
||||
* Update Multiparameter extension to use ActiveRecord type classes with multiparameter handling
|
||||
which stores a hash of multiparamter values as the value before type cast, no longer a mushed datetime string
|
||||
* Removed all custom plugin attribute methods and method overrides in favour using ActiveModel type system
|
||||
|
||||
= 4.1.0 [2019-06-11]
|
||||
* Relaxed Timeliness dependency version to >= 0.3.10 and < 1, which allows
|
||||
version 0.4 with threadsafety fix for use_us_formats and use_euro_formats
|
||||
hot switching in a request.
|
||||
|
||||
= 4.0.2 [2016-01-07]
|
||||
* Fix undefine_generated_methods ivar guard setting to false
|
||||
|
||||
|
||||
2
Gemfile
2
Gemfile
@ -2,7 +2,7 @@ source 'http://rubygems.org'
|
||||
|
||||
gemspec
|
||||
|
||||
gem 'rails', '~> 5.0.0'
|
||||
gem 'rails', '~> 5.2.4'
|
||||
gem 'rspec'
|
||||
gem 'rspec-rails', '~> 3.7'
|
||||
gem 'timecop'
|
||||
|
||||
36
README.rdoc
36
README.rdoc
@ -5,7 +5,7 @@
|
||||
|
||||
== Description
|
||||
|
||||
Complete validation of dates, times and datetimes for Rails 5.0.x and ActiveModel.
|
||||
Complete validation of dates, times and datetimes for Rails 5.x and ActiveModel.
|
||||
|
||||
If you a looking for the old version for Rails 4.x go here [https://github.com/adzap/validates_timeliness/tree/4-0-stable].
|
||||
|
||||
@ -30,7 +30,7 @@ If you a looking for the old version for Rails 4.x go here [https://github.com/a
|
||||
== Installation
|
||||
|
||||
# in Gemfile
|
||||
gem 'validates_timeliness', '~> 5.0.0.alpha3'
|
||||
gem 'validates_timeliness', '~> 5.0.0.beta1'
|
||||
|
||||
# Run bundler
|
||||
$ bundle install
|
||||
@ -49,21 +49,21 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
|
||||
|
||||
validates_datetime :occurred_at
|
||||
|
||||
validates_date :date_of_birth, :before => lambda { 18.years.ago },
|
||||
:before_message => "must be at least 18 years old"
|
||||
validates_date :date_of_birth, before: lambda { 18.years.ago },
|
||||
before_message: "must be at least 18 years old"
|
||||
|
||||
validates_datetime :finish_time, :after => :start_time # Method symbol
|
||||
validates_datetime :finish_time, after: :start_time # Method symbol
|
||||
|
||||
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
|
||||
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'] # 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',
|
||||
:before => :lunchtime,
|
||||
:before_message => 'must be before lunch time'
|
||||
validates_time :breakfast_time, on_or_after: '6:00am',
|
||||
on_or_after_message: 'must be after opening time',
|
||||
before: :lunchtime,
|
||||
before_message: 'must be before lunch time'
|
||||
|
||||
|
||||
== Usage
|
||||
@ -72,14 +72,14 @@ To validate a model with a date, time or datetime attribute you just use the
|
||||
validation method
|
||||
|
||||
class Person < ActiveRecord::Base
|
||||
validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
||||
validates_date :date_of_birth, on_or_before: lambda { Date.current }
|
||||
# or
|
||||
validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
|
||||
validates :date_of_birth, timeliness: {on_or_before: lambda { Date.current }, type: :date}
|
||||
end
|
||||
|
||||
or even on a specific record, per ActiveModel API.
|
||||
|
||||
@person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
||||
@person.validates_date :date_of_birth, on_or_before: lambda { Date.current }
|
||||
|
||||
|
||||
The list of validation methods available are as follows:
|
||||
@ -206,14 +206,14 @@ plugin allows you to use shorthand symbols for often used relative times or date
|
||||
|
||||
Just provide the symbol as the option value like so:
|
||||
|
||||
validates_date :birth_date, :on_or_before => :today
|
||||
validates_date :birth_date, on_or_before: :today
|
||||
|
||||
The :today symbol is evaluated as <tt>lambda { Date.today }</tt>. The :now and :today
|
||||
symbols are pre-configured. Configure your own like so:
|
||||
|
||||
# in the setup block
|
||||
config.restriction_shorthand_symbols.update(
|
||||
:yesterday => lambda { 1.day.ago }
|
||||
yesterday: lambda { 1.day.ago }
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -8,11 +8,7 @@ gem "rspec-rails", "~> 3.7"
|
||||
gem "timecop"
|
||||
gem "byebug"
|
||||
gem "appraisal"
|
||||
gem "sqlite3"
|
||||
gem "sqlite3", "~> 1.3.6"
|
||||
gem "nokogiri", "~> 1.8"
|
||||
|
||||
group :active_record do
|
||||
gem "sqlite3-ruby", require: "sqlite3"
|
||||
end
|
||||
|
||||
gemspec path: "../"
|
||||
|
||||
@ -8,11 +8,7 @@ gem "rspec-rails", "~> 3.7"
|
||||
gem "timecop"
|
||||
gem "byebug"
|
||||
gem "appraisal"
|
||||
gem "sqlite3"
|
||||
gem "sqlite3", "~> 1.3.6"
|
||||
gem "nokogiri", "~> 1.8"
|
||||
|
||||
group :active_record do
|
||||
gem "sqlite3-ruby", require: "sqlite3"
|
||||
end
|
||||
|
||||
gemspec path: "../"
|
||||
|
||||
@ -8,11 +8,7 @@ gem "rspec-rails", "~> 3.7"
|
||||
gem "timecop"
|
||||
gem "byebug"
|
||||
gem "appraisal"
|
||||
gem "sqlite3"
|
||||
gem "sqlite3", "~> 1.3.6"
|
||||
gem "nokogiri", "~> 1.8"
|
||||
|
||||
group :active_record do
|
||||
gem "sqlite3-ruby", require: "sqlite3"
|
||||
end
|
||||
|
||||
gemspec path: "../"
|
||||
|
||||
@ -36,8 +36,8 @@ module ValidatesTimeliness
|
||||
|
||||
# Shorthand time and date symbols for restrictions
|
||||
self.restriction_shorthand_symbols = {
|
||||
:now => lambda { Time.current },
|
||||
:today => lambda { Date.current }
|
||||
now: proc { Time.current },
|
||||
today: proc { Date.current }
|
||||
}
|
||||
|
||||
# Use the plugin date/time parser which is stricter and extensible
|
||||
|
||||
@ -11,7 +11,7 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveRecord::Base
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
include ValidatesTimeliness::AttributeMethods
|
||||
include ValidatesTimeliness::ORM::ActiveRecord
|
||||
end
|
||||
|
||||
@ -11,5 +11,13 @@ module ValidatesTimeliness
|
||||
initializer "validates_timeliness.initialize_restriction_errors" do
|
||||
ValidatesTimeliness.ignore_restriction_errors = !Rails.env.test?
|
||||
end
|
||||
|
||||
initializer "validates_timeliness.initialize_timeliness_ambiguous_date_format", :after => :load_config_initializers do
|
||||
if Timeliness.respond_to?(:ambiguous_date_format) # i.e. v0.4+
|
||||
# Set default for each new thread if you have changed the default using
|
||||
# the format switching methods.
|
||||
Timeliness.configuration.ambiguous_date_format = Timeliness::Definitions.current_date_format
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = '5.0.0.alpha4'
|
||||
VERSION = '5.0.0.beta2'
|
||||
end
|
||||
|
||||
@ -10,6 +10,8 @@ require 'timecop'
|
||||
require 'validates_timeliness'
|
||||
require 'validates_timeliness/orm/active_model'
|
||||
|
||||
require 'rails/railtie'
|
||||
|
||||
require 'support/test_model'
|
||||
require 'support/model_helpers'
|
||||
require 'support/config_helper'
|
||||
|
||||
@ -17,8 +17,7 @@ module ModelHelpers
|
||||
|
||||
def with_each_person_value(attr_name, values)
|
||||
record = Person.new
|
||||
values = [values] unless values.is_a?(Array)
|
||||
values.each do |value|
|
||||
Array.wrap(values).each do |value|
|
||||
record.send("#{attr_name}=", value)
|
||||
yield record, value
|
||||
end
|
||||
|
||||
22
spec/validates_timeliness/railtie_spec.rb
Normal file
22
spec/validates_timeliness/railtie_spec.rb
Normal file
@ -0,0 +1,22 @@
|
||||
require 'validates_timeliness/railtie'
|
||||
|
||||
RSpec.describe ValidatesTimeliness::Railtie do
|
||||
context "intializers" do
|
||||
context "validates_timeliness.initialize_timeliness_ambiguous_date_format" do
|
||||
it 'should set the timeliness default ambiguous date format from the current format' do
|
||||
expect(Timeliness.configuration.ambiguous_date_format).to eq :us
|
||||
ValidatesTimeliness.parser.use_euro_formats
|
||||
|
||||
initializer("validates_timeliness.initialize_timeliness_ambiguous_date_format").run
|
||||
|
||||
expect(Timeliness.configuration.ambiguous_date_format).to eq :euro
|
||||
end
|
||||
end if Timeliness.respond_to?(:ambiguous_date_format)
|
||||
|
||||
def initializer(name)
|
||||
ValidatesTimeliness::Railtie.initializers.find { |i|
|
||||
i.name == name
|
||||
} || raise("Initializer #{name} not found")
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user