mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 23:33:00 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c698be4c4 | ||
|
|
091e7ecfa0 | ||
|
|
8168bcbd3a | ||
|
|
973bbfa82c | ||
|
|
62557e7e04 | ||
|
|
fd73c4eccd | ||
|
|
7bcdea1738 | ||
|
|
8898b8686c | ||
|
|
aad2db8662 | ||
|
|
8e08cbf6e4 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ pkg/
|
||||
.bundle/
|
||||
.rvmrc
|
||||
Gemfile.lock
|
||||
gemfiles/*.lock
|
||||
|
||||
11
Appraisals
Normal file
11
Appraisals
Normal file
@@ -0,0 +1,11 @@
|
||||
appraise "rails_3_0" do
|
||||
gem "rails", "~> 3.0.0"
|
||||
end
|
||||
|
||||
appraise "rails_3_1" do
|
||||
gem "rails", "~> 3.1.0"
|
||||
end
|
||||
|
||||
appraise "rails_3_2" do
|
||||
gem "rails", "~> 3.2.0"
|
||||
end
|
||||
@@ -1,3 +1,10 @@
|
||||
= 3.0.13 [2012-08-21]
|
||||
* Fix ActiveRecord issues with using plugin parser by using old way of caching values.
|
||||
* Allow any ActiveRecord non-column attribute to be validated
|
||||
|
||||
= 3.0.12 [2012-06-23]
|
||||
* Fix load order issue when relying on Railtie to load ActiveRecord extension
|
||||
|
||||
= 3.0.11 [2012-04-01]
|
||||
* Change dependency on Timeliness version due to a broken release
|
||||
|
||||
|
||||
6
Gemfile
6
Gemfile
@@ -2,13 +2,15 @@ source 'http://rubygems.org'
|
||||
|
||||
gemspec
|
||||
|
||||
gem 'rails', '~> 3.2.1'
|
||||
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 'ruby-debug19', :platforms => [:ruby_19]
|
||||
gem 'debugger', :platforms => [:ruby_19]
|
||||
gem 'appraisal'
|
||||
gem 'sqlite3'
|
||||
|
||||
group :mongoid do
|
||||
gem 'mongoid', '~> 2.3.0'
|
||||
|
||||
23
README.rdoc
23
README.rdoc
@@ -1,7 +1,7 @@
|
||||
= ValidatesTimeliness
|
||||
|
||||
* Source: http://github.com/adzap/validates_timeliness
|
||||
* Bugs: http://github.com/adzap/validates_timeliness/issues
|
||||
* Issues: http://github.com/adzap/validates_timeliness/issues
|
||||
|
||||
== Description
|
||||
|
||||
@@ -18,24 +18,19 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
|
||||
|
||||
* Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
|
||||
|
||||
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
||||
|
||||
* Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
|
||||
|
||||
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
||||
|
||||
* Supports I18n for the error messages
|
||||
|
||||
* Supports Ruby 1.8.x, 1.9.x and Rubinius.
|
||||
* Supports all the Rubies (that any sane person would be using in production).
|
||||
|
||||
|
||||
== Installation
|
||||
|
||||
As plugin (from master)
|
||||
|
||||
rails plugin install git://github.com/adzap/validates_timeliness.git
|
||||
|
||||
As gem
|
||||
|
||||
# in Gemfile
|
||||
gem 'validates_timeliness', '~> 3.0.2'
|
||||
gem 'validates_timeliness', '~> 3.0'
|
||||
|
||||
# Run bundler
|
||||
$ bundle install
|
||||
@@ -55,7 +50,7 @@ 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"
|
||||
:before_message => "must be at least 18 years old"
|
||||
|
||||
validates_datetime :finish_time, :after => :start_time # Method symbol
|
||||
|
||||
@@ -79,7 +74,7 @@ validation method
|
||||
validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
|
||||
end
|
||||
|
||||
# or even on a specific record, per ActiveModel API.
|
||||
or even on a specific record, per ActiveModel API.
|
||||
|
||||
@person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
||||
|
||||
@@ -298,4 +293,4 @@ To see the generous people who have contributed code, take a look at the {contri
|
||||
|
||||
== License
|
||||
|
||||
Copyright (c) 2008-2010 Adam Meehan, released under the MIT license
|
||||
Copyright (c) 2008 Adam Meehan, released under the MIT license
|
||||
|
||||
6
Rakefile
6
Rakefile
@@ -1,7 +1,9 @@
|
||||
require 'bundler'
|
||||
Bundler::GemHelper.install_tasks
|
||||
require 'bundler/setup'
|
||||
|
||||
Bundler.setup
|
||||
require 'appraisal'
|
||||
|
||||
Bundler::GemHelper.install_tasks
|
||||
|
||||
require 'rake/rdoctask'
|
||||
require 'rspec/core/rake_task'
|
||||
|
||||
15
gemfiles/rails_3_0.gemfile
Normal file
15
gemfiles/rails_3_0.gemfile
Normal file
@@ -0,0 +1,15 @@
|
||||
# This file was generated by Appraisal
|
||||
|
||||
source "http://rubygems.org"
|
||||
|
||||
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 "rails", "~> 3.0.0"
|
||||
|
||||
gemspec :path=>"../"
|
||||
15
gemfiles/rails_3_1.gemfile
Normal file
15
gemfiles/rails_3_1.gemfile
Normal file
@@ -0,0 +1,15 @@
|
||||
# This file was generated by Appraisal
|
||||
|
||||
source "http://rubygems.org"
|
||||
|
||||
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 "rails", "~> 3.1.0"
|
||||
|
||||
gemspec :path=>"../"
|
||||
15
gemfiles/rails_3_2.gemfile
Normal file
15
gemfiles/rails_3_2.gemfile
Normal file
@@ -0,0 +1,15 @@
|
||||
# This file was generated by Appraisal
|
||||
|
||||
source "http://rubygems.org"
|
||||
|
||||
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 "rails", "~> 3.2.0"
|
||||
|
||||
gemspec :path=>"../"
|
||||
@@ -3,7 +3,6 @@ module ValidatesTimeliness
|
||||
class InstallGenerator < Rails::Generators::Base
|
||||
desc "Copy ValidatesTimeliness default files"
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
class_option :template_engine
|
||||
|
||||
def copy_initializers
|
||||
copy_file 'validates_timeliness.rb', 'config/initializers/validates_timeliness.rb'
|
||||
|
||||
@@ -58,6 +58,10 @@ module ValidatesTimeliness
|
||||
# Setup method for plugin configuration
|
||||
def self.setup
|
||||
yield self
|
||||
load_orms
|
||||
end
|
||||
|
||||
def self.load_orms
|
||||
extend_orms.each {|orm| require "validates_timeliness/orm/#{orm}" }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,61 +3,32 @@ module ValidatesTimeliness
|
||||
module ActiveRecord
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def self.use_plugin_cache?
|
||||
::ActiveRecord::VERSION::STRING < '3.1.0'
|
||||
end
|
||||
|
||||
included do
|
||||
if ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||
include Reload
|
||||
else
|
||||
# Just use the built-in before_type_cast retrieval
|
||||
alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
public
|
||||
|
||||
def timeliness_attribute_timezone_aware?(attr_name)
|
||||
attr_name = attr_name.to_s
|
||||
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
||||
create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
|
||||
end
|
||||
|
||||
def timeliness_attribute_type(attr_name)
|
||||
columns_hash[attr_name.to_s].type
|
||||
timeliness_column_for_attribute(attr_name).type
|
||||
end
|
||||
|
||||
def timeliness_column_for_attribute(attr_name)
|
||||
columns_hash.fetch(attr_name.to_s) do |attr_name|
|
||||
validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type
|
||||
::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def define_attribute_methods
|
||||
super.tap do |attribute_methods_generated|
|
||||
use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||
define_timeliness_methods use_before_type_cast
|
||||
define_timeliness_methods true
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
|
||||
if before_type_cast
|
||||
define_timeliness_write_method(attr_name)
|
||||
define_timeliness_before_type_cast_method(attr_name)
|
||||
elsif ValidatesTimeliness.use_plugin_parser
|
||||
define_timeliness_write_method_without_cache(attr_name)
|
||||
end
|
||||
end
|
||||
|
||||
def define_timeliness_write_method_without_cache(attr_name)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}=(value)
|
||||
original_value = value
|
||||
if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend
|
||||
super(value)
|
||||
@attributes['#{attr_name}'] = original_value
|
||||
end
|
||||
EOV
|
||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def timeliness_type_cast_code(attr_name, var_name)
|
||||
type = timeliness_attribute_type(attr_name)
|
||||
|
||||
@@ -67,11 +38,9 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
module Reload
|
||||
def reload(*args)
|
||||
_clear_timeliness_cache
|
||||
super
|
||||
end
|
||||
def reload(*args)
|
||||
_clear_timeliness_cache
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,7 +3,8 @@ module ValidatesTimeliness
|
||||
initializer "validates_timeliness.initialize_active_record", :after => 'active_record.initialize_timezone' do
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ValidatesTimeliness.default_timezone = ActiveRecord::Base.default_timezone
|
||||
ValidatesTimeliness.extend_orms = [ :active_record ]
|
||||
ValidatesTimeliness.extend_orms << :active_record
|
||||
ValidatesTimeliness.load_orms
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ module ValidatesTimeliness
|
||||
|
||||
RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
|
||||
|
||||
def self.kind
|
||||
:timeliness
|
||||
end
|
||||
|
||||
def initialize(options)
|
||||
@type = options.delete(:type) || :datetime
|
||||
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module ValidatesTimeliness
|
||||
VERSION = '3.0.11'
|
||||
VERSION = '3.0.13'
|
||||
end
|
||||
|
||||
@@ -92,10 +92,7 @@ RSpec.configure do |c|
|
||||
reset_validation_setup_for(PersonWithShim)
|
||||
end
|
||||
|
||||
RSpec.configure do |c|
|
||||
c.filter_run_excluding :active_record => lambda {|version|
|
||||
!(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
|
||||
}
|
||||
end
|
||||
|
||||
c.filter_run_excluding :active_record => lambda {|version|
|
||||
!(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
|
||||
}
|
||||
end
|
||||
|
||||
@@ -41,13 +41,44 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
end
|
||||
|
||||
it 'should determine type for attribute' do
|
||||
Employee.timeliness_attribute_type(:birth_date).should == :date
|
||||
Employee.timeliness_attribute_type(:birth_date).should eq :date
|
||||
end
|
||||
|
||||
context 'attribute timezone awareness' do
|
||||
let(:klass) {
|
||||
Class.new(ActiveRecord::Base) do
|
||||
self.table_name = 'employees'
|
||||
attr_accessor :some_date
|
||||
attr_accessor :some_time
|
||||
attr_accessor :some_datetime
|
||||
validates_date :some_date
|
||||
validates_time :some_time
|
||||
validates_datetime :some_datetime
|
||||
end
|
||||
}
|
||||
|
||||
context 'for column attribute' do
|
||||
it 'should be detected from column type' do
|
||||
klass.timeliness_attribute_timezone_aware?(:birth_date).should be_false
|
||||
klass.timeliness_attribute_timezone_aware?(:birth_time).should be_false
|
||||
klass.timeliness_attribute_timezone_aware?(:birth_datetime).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context 'for non-column attribute' do
|
||||
it 'should be detected from the validation type' do
|
||||
klass.timeliness_attribute_timezone_aware?(:some_date).should be_false
|
||||
klass.timeliness_attribute_timezone_aware?(:some_time).should be_false
|
||||
klass.timeliness_attribute_timezone_aware?(:some_datetime).should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
class EmployeeWithCache < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
self.table_name = 'employees'
|
||||
validates_date :birth_date, :allow_blank => true
|
||||
validates_time :birth_time, :allow_blank => true
|
||||
validates_datetime :birth_datetime, :allow_blank => true
|
||||
end
|
||||
|
||||
@@ -55,8 +86,9 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
context 'for datetime column' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_datetime = date_string = '2010-01-01'
|
||||
r._timeliness_raw_value_for('birth_datetime').should == date_string
|
||||
r.birth_datetime = datetime_string = '2010-01-01 12:30'
|
||||
|
||||
r._timeliness_raw_value_for('birth_datetime').should eq datetime_string
|
||||
end
|
||||
end
|
||||
|
||||
@@ -64,7 +96,17 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_date = date_string = '2010-01-01'
|
||||
r._timeliness_raw_value_for('birth_date').should == date_string
|
||||
|
||||
r._timeliness_raw_value_for('birth_date').should eq date_string
|
||||
end
|
||||
end
|
||||
|
||||
context 'for time column' do
|
||||
it 'should store raw value' do
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_time = time_string = '12:12'
|
||||
|
||||
r._timeliness_raw_value_for('birth_time').should eq time_string
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -73,36 +115,76 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
with_config(:use_plugin_parser, true)
|
||||
|
||||
class EmployeeWithParser < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
self.table_name = 'employees'
|
||||
validates_date :birth_date, :allow_blank => true
|
||||
validates_time :birth_time, :allow_blank => true
|
||||
validates_datetime :birth_datetime, :allow_blank => true
|
||||
end
|
||||
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = '2010-01-01'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = 'not a date'
|
||||
end
|
||||
|
||||
context "for a date column" do
|
||||
it 'should store a date value after parsing string' do
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = '2010-01-01'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = 'not valid'
|
||||
end
|
||||
|
||||
it 'should store a Date value after parsing string' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_date = '2010-01-01'
|
||||
|
||||
r.birth_date.should be_kind_of(Date)
|
||||
r.birth_date.should == Date.new(2010, 1, 1)
|
||||
r.birth_date.should eq Date.new(2010, 1, 1)
|
||||
end
|
||||
end
|
||||
|
||||
context "for a time column" do
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = '12:30'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = 'not valid'
|
||||
end
|
||||
|
||||
it 'should store a Time value after parsing string' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_time = '12:30'
|
||||
|
||||
r.birth_time.should be_kind_of(Time)
|
||||
r.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
|
||||
end
|
||||
end
|
||||
|
||||
context "for a datetime column" do
|
||||
with_config(:default_timezone, 'Australia/Melbourne')
|
||||
|
||||
it 'should parse a string value' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-01-01 12:00'
|
||||
end
|
||||
|
||||
it 'should parse a invalid string value as nil' do
|
||||
Timeliness::Parser.should_receive(:parse)
|
||||
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = 'not valid'
|
||||
end
|
||||
|
||||
it 'should parse string into Time value' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-01-01 12:00'
|
||||
@@ -114,7 +196,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
r = EmployeeWithParser.new
|
||||
r.birth_datetime = '2010-06-01 12:00'
|
||||
|
||||
r.birth_datetime.utc_offset.should == Time.zone.utc_offset
|
||||
r.birth_datetime.utc_offset.should eq Time.zone.utc_offset
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -140,7 +222,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
r = Employee.new
|
||||
r.birth_datetime = date_string = '2010-01-01'
|
||||
|
||||
r.birth_datetime_before_type_cast.should == date_string
|
||||
r.birth_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
|
||||
it 'should return attribute if no attribute assignment has been made' do
|
||||
@@ -158,7 +240,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
r = Employee.new
|
||||
r.birth_datetime = date_string = '2010-01-31'
|
||||
|
||||
r.birth_datetime_before_type_cast.should == date_string
|
||||
r.birth_datetime_before_type_cast.should eq date_string
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user