mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb3ac8d445 | ||
|
|
4e5f5fa695 | ||
|
|
321834b5ff | ||
|
|
ff131415f7 | ||
|
|
ad43eb31d6 | ||
|
|
0f782a4ed5 | ||
|
|
d57c64dcc2 | ||
|
|
244f398cc4 | ||
|
|
0feeb746cd | ||
|
|
6f23e40c25 | ||
|
|
773e816a4d | ||
|
|
80c9df448d | ||
|
|
54ae49b8a0 | ||
|
|
2fef0a23d6 |
1
.rspec
1
.rspec
@@ -1,3 +1,4 @@
|
|||||||
--format documentation
|
--format documentation
|
||||||
--color
|
--color
|
||||||
|
--require spec_helper
|
||||||
--require byebug
|
--require byebug
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ gemfile:
|
|||||||
- gemfiles/rails_4_2.gemfile
|
- gemfiles/rails_4_2.gemfile
|
||||||
|
|
||||||
rvm:
|
rvm:
|
||||||
- "2.0.0"
|
- "2.2.3"
|
||||||
- "2.2.0"
|
|
||||||
- "2.3.0"
|
- "2.3.0"
|
||||||
|
|
||||||
script: 'bundle exec rake'
|
script: 'bundle exec rake'
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
appraise "rails_4_0" do
|
appraise "rails_4_0" do
|
||||||
gem "rails", "~> 4.0.0"
|
gem "rails", "~> 4.0.13"
|
||||||
end
|
end
|
||||||
|
|
||||||
appraise "rails_4_1" do
|
appraise "rails_4_1" do
|
||||||
gem "rails", "~> 4.1.0"
|
gem "rails", "~> 4.1.14"
|
||||||
end
|
end
|
||||||
|
|
||||||
appraise "rails_4_2" do
|
appraise "rails_4_2" do
|
||||||
gem "rails", "~> 4.2.0"
|
gem "rails", "~> 4.2.5"
|
||||||
end
|
end
|
||||||
@@ -1,3 +1,15 @@
|
|||||||
|
= 4.0.0 [2015-12-29]
|
||||||
|
* Extracted mongoid support into https://github.com/adzap/validates_timeliness-mongoid which is broken (not supported anymore).
|
||||||
|
* Fixed Rails 4.0, 4.1 and 4.2 compatability issues
|
||||||
|
* Upgrade specs to RSpec 3
|
||||||
|
* Added travis config
|
||||||
|
* Huge thanks to @johncarney for keeping it alive with his fork (https://github.com/johncarney/validates_timeliness)
|
||||||
|
|
||||||
|
= 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.
|
||||||
|
|
||||||
|
|||||||
4
Gemfile
4
Gemfile
@@ -3,8 +3,8 @@ source 'http://rubygems.org'
|
|||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem 'rails', '~> 4.0.13'
|
gem 'rails', '~> 4.0.13'
|
||||||
gem 'rspec', '~> 3.0.0'
|
gem 'rspec', '~> 3.4.0'
|
||||||
gem 'rspec-rails', '~> 3.0.0'
|
gem 'rspec-rails', '~> 3.4.0'
|
||||||
gem 'timecop'
|
gem 'timecop'
|
||||||
gem 'byebug'
|
gem 'byebug'
|
||||||
gem 'appraisal'
|
gem 'appraisal'
|
||||||
|
|||||||
@@ -33,15 +33,9 @@ module ValidatesTimeliness
|
|||||||
}.tap { |mod| include mod }
|
}.tap { |mod| include mod }
|
||||||
end
|
end
|
||||||
|
|
||||||
def undefine_attribute_methods
|
|
||||||
super.tap { undefine_timeliness_attribute_methods }
|
|
||||||
end
|
|
||||||
|
|
||||||
def undefine_timeliness_attribute_methods
|
def undefine_timeliness_attribute_methods
|
||||||
generated_timeliness_methods.synchronize do
|
generated_timeliness_methods.module_eval do
|
||||||
generated_timeliness_methods.module_eval do
|
instance_methods.each { |m| undef_method(m) }
|
||||||
instance_methods.each { |m| undef_method(m) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -74,8 +68,10 @@ module ValidatesTimeliness
|
|||||||
@timeliness_cache[attr_name] = value
|
@timeliness_cache[attr_name] = value
|
||||||
|
|
||||||
if ValidatesTimeliness.use_plugin_parser
|
if ValidatesTimeliness.use_plugin_parser
|
||||||
|
type = self.class.timeliness_attribute_type(attr_name)
|
||||||
timezone = :current if self.class.timeliness_attribute_timezone_aware?(attr_name)
|
timezone = :current if self.class.timeliness_attribute_timezone_aware?(attr_name)
|
||||||
value = Timeliness::Parser.parse(value, self.class.timeliness_attribute_type(attr_name), :zone => timezone)
|
value = Timeliness::Parser.parse(value, type, :zone => timezone)
|
||||||
|
value = value.to_date if value && type == :date
|
||||||
end
|
end
|
||||||
|
|
||||||
@attributes[attr_name] = value
|
@attributes[attr_name] = value
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ module ValidatesTimeliness
|
|||||||
value.to_date
|
value.to_date
|
||||||
when :datetime
|
when :datetime
|
||||||
value.is_a?(Time) ? value : value.to_time
|
value.is_a?(Time) ? value : value.to_time
|
||||||
|
else
|
||||||
|
value
|
||||||
end
|
end
|
||||||
if options[:ignore_usec] && value.is_a?(Time)
|
if options[:ignore_usec] && value.is_a?(Time)
|
||||||
Timeliness::Parser.make_time(Array(value).reverse[4..9], (:current if @timezone_aware))
|
Timeliness::Parser.make_time(Array(value).reverse[4..9], (:current if @timezone_aware))
|
||||||
|
|||||||
20
lib/validates_timeliness/orm/active_model.rb
Normal file
20
lib/validates_timeliness/orm/active_model.rb
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
module ORM
|
||||||
|
module ActiveModel
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
public
|
||||||
|
|
||||||
|
def define_attribute_methods(*attr_names)
|
||||||
|
super.tap { define_timeliness_methods}
|
||||||
|
end
|
||||||
|
|
||||||
|
def undefine_attribute_methods
|
||||||
|
super.tap { undefine_timeliness_attribute_methods }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -14,26 +14,26 @@ module ValidatesTimeliness
|
|||||||
timeliness_column_for_attribute(attr_name).type
|
timeliness_column_for_attribute(attr_name).type
|
||||||
end
|
end
|
||||||
|
|
||||||
if ActiveModel.version >= Gem::Version.new('4.2')
|
if ::ActiveModel.version >= Gem::Version.new('4.2')
|
||||||
def timeliness_column_for_attribute(attr_name)
|
def timeliness_column_for_attribute(attr_name)
|
||||||
columns_hash.fetch(attr_name.to_s) do |attr_name|
|
columns_hash.fetch(attr_name.to_s) do |key|
|
||||||
validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
||||||
::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, lookup_cast_type(validation_type), validation_type)
|
::ActiveRecord::ConnectionAdapters::Column.new(key, nil, lookup_cast_type(validation_type), validation_type)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_cast_type(sql_type)
|
def lookup_cast_type(sql_type)
|
||||||
case sql_type
|
case sql_type
|
||||||
when 'datetime' then ::ActiveRecord::Type::DateTime.new
|
when 'datetime' then ::ActiveRecord::Type::DateTime.new
|
||||||
when 'date' then ::ActiveRecord::Type::Date.new
|
when 'date' then ::ActiveRecord::Type::Date.new
|
||||||
when 'time' then ::ActiveRecord::Type::Time.new
|
when 'time' then ::ActiveRecord::Type::Time.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def timeliness_column_for_attribute(attr_name)
|
def timeliness_column_for_attribute(attr_name)
|
||||||
columns_hash.fetch(attr_name.to_s) do |attr_name|
|
columns_hash.fetch(attr_name.to_s) do |key|
|
||||||
validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
||||||
::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type)
|
::ActiveRecord::ConnectionAdapters::Column.new(key, nil, validation_type)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
VERSION = '4.0.0'
|
VERSION = '4.0.1'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ require 'action_view'
|
|||||||
require 'timecop'
|
require 'timecop'
|
||||||
|
|
||||||
require 'validates_timeliness'
|
require 'validates_timeliness'
|
||||||
|
require 'validates_timeliness/orm/active_model'
|
||||||
|
|
||||||
require 'support/test_model'
|
require 'support/test_model'
|
||||||
require 'support/model_helpers'
|
require 'support/model_helpers'
|
||||||
@@ -30,14 +31,9 @@ I18n.available_locales = ['en', 'es']
|
|||||||
module TestModelShim
|
module TestModelShim
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
include ValidatesTimeliness::AttributeMethods
|
include ValidatesTimeliness::AttributeMethods
|
||||||
|
include ValidatesTimeliness::ORM::ActiveModel
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Hook method for attribute method generation
|
|
||||||
def define_attribute_methods(attr_names)
|
|
||||||
super
|
|
||||||
define_timeliness_methods
|
|
||||||
end
|
|
||||||
|
|
||||||
# Hook into native time zone handling check, if any
|
# Hook into native time zone handling check, if any
|
||||||
def timeliness_attribute_timezone_aware?(attr_name)
|
def timeliness_attribute_timezone_aware?(attr_name)
|
||||||
false
|
false
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::AttributeMethods do
|
||||||
|
|
||||||
describe ValidatesTimeliness::AttributeMethods do
|
|
||||||
it 'should define read_timeliness_attribute_before_type_cast instance method' do
|
it 'should define read_timeliness_attribute_before_type_cast instance method' do
|
||||||
expect(PersonWithShim.new).to respond_to(:read_timeliness_attribute_before_type_cast)
|
expect(PersonWithShim.new).to respond_to(:read_timeliness_attribute_before_type_cast)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Conversion do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Conversion do
|
|
||||||
include ValidatesTimeliness::Conversion
|
include ValidatesTimeliness::Conversion
|
||||||
|
|
||||||
let(:options) { Hash.new }
|
let(:options) { Hash.new }
|
||||||
@@ -176,7 +174,7 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
it 'should return Time value for attribute method symbol which returns string time value' do
|
it 'should return Time value for attribute method symbol which returns string time value' do
|
||||||
value = '2010-01-01 12:00:00'
|
value = '2010-01-01 12:00:00'
|
||||||
person.birth_time = value
|
person.birth_time = value
|
||||||
expect(evaluate_option_value(:birth_time, person)).to eq(Time.zone.local(2010,1,1,12,0,0))
|
expect(evaluate_option_value(:birth_time, person)).to eq(Time.local(2010,1,1,12,0,0))
|
||||||
end
|
end
|
||||||
|
|
||||||
context "restriction shorthand" do
|
context "restriction shorthand" do
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
|
||||||
|
|
||||||
describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
|
|
||||||
include ActionView::Helpers::DateHelper
|
include ActionView::Helpers::DateHelper
|
||||||
attr_reader :person, :params
|
attr_reader :person, :params
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe 'ValidatesTimeliness::Extensions::MultiparameterHandler' do
|
||||||
|
|
||||||
describe 'ValidatesTimeliness::Extensions::MultiparameterHandler' do
|
|
||||||
|
|
||||||
context "time column" do
|
context "time column" do
|
||||||
it 'should assign a string value for invalid date portion' do
|
it 'should assign a string value for invalid date portion' do
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness, 'HelperMethods' do
|
||||||
|
|
||||||
describe ValidatesTimeliness, 'HelperMethods' do
|
|
||||||
let(:record) { Person.new }
|
let(:record) { Person.new }
|
||||||
|
|
||||||
it 'should define class validation methods' do
|
it 'should define class validation methods' do
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
|
||||||
|
|
||||||
describe ValidatesTimeliness, 'ActiveRecord' do
|
|
||||||
|
|
||||||
context "validation methods" do
|
context "validation methods" do
|
||||||
let(:record) { Employee.new }
|
let(:record) { Employee.new }
|
||||||
@@ -241,4 +239,10 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
expect(Employee.define_attribute_methods).to be_falsey
|
expect(Employee.define_attribute_methods).to be_falsey
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "undefine_attribute_methods" do
|
||||||
|
it "returns a falsy value if the attribute methods have already been generated" do
|
||||||
|
expect { Employee.undefine_attribute_methods }.to_not raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator, ":after option" do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator, ":after option" do
|
|
||||||
describe "for date type" do
|
describe "for date type" do
|
||||||
before do
|
before do
|
||||||
Person.validates_date :birth_date, :after => Date.new(2010, 1, 1)
|
Person.validates_date :birth_date, :after => Date.new(2010, 1, 1)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator, ":before option" do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator, ":before option" do
|
|
||||||
describe "for date type" do
|
describe "for date type" do
|
||||||
before do
|
before do
|
||||||
Person.validates_date :birth_date, :before => Date.new(2010, 1, 1)
|
Person.validates_date :birth_date, :before => Date.new(2010, 1, 1)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator, ":is_at option" do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator, ":is_at option" do
|
|
||||||
before do
|
before do
|
||||||
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator, ":on_or_after option" do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator, ":on_or_after option" do
|
|
||||||
describe "for date type" do
|
describe "for date type" do
|
||||||
before do
|
before do
|
||||||
Person.validates_date :birth_date, :on_or_after => Date.new(2010, 1, 1)
|
Person.validates_date :birth_date, :on_or_after => Date.new(2010, 1, 1)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator, ":on_or_before option" do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator, ":on_or_before option" do
|
|
||||||
describe "for date type" do
|
describe "for date type" do
|
||||||
before do
|
before do
|
||||||
Person.validates_date :birth_date, :on_or_before => Date.new(2010, 1, 1)
|
Person.validates_date :birth_date, :on_or_before => Date.new(2010, 1, 1)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness::Validator do
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validator do
|
|
||||||
before do
|
before do
|
||||||
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
require 'spec_helper'
|
RSpec.describe ValidatesTimeliness do
|
||||||
|
|
||||||
describe ValidatesTimeliness do
|
|
||||||
|
|
||||||
it 'should alias use_euro_formats to remove_us_formats on Timeliness gem' do
|
it 'should alias use_euro_formats to remove_us_formats on Timeliness gem' do
|
||||||
expect(Timeliness).to respond_to(:remove_us_formats)
|
expect(Timeliness).to respond_to(:remove_us_formats)
|
||||||
|
|||||||
Reference in New Issue
Block a user