mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
A fix for ActiveRecord 3.1.x and plugin parser not working together (issue #52)
This commit is contained in:
parent
f5e2deeb73
commit
f8aeeca0a9
6
Gemfile
6
Gemfile
@ -2,9 +2,9 @@ source 'http://rubygems.org'
|
|||||||
|
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem 'rails', '3.1.0'
|
gem 'rails', '3.1.3'
|
||||||
gem 'rspec', '~> 2.6'
|
gem 'rspec', '~> 2.8'
|
||||||
gem 'rspec-rails', '~> 2.6'
|
gem 'rspec-rails', '~> 2.8'
|
||||||
gem 'timecop'
|
gem 'timecop'
|
||||||
gem 'rspec_tag_matchers'
|
gem 'rspec_tag_matchers'
|
||||||
gem 'ruby-debug', :platforms => [:ruby_18, :jruby]
|
gem 'ruby-debug', :platforms => [:ruby_18, :jruby]
|
||||||
|
|||||||
@ -33,11 +33,13 @@ module ValidatesTimeliness
|
|||||||
def define_timeliness_write_method(attr_name)
|
def define_timeliness_write_method(attr_name)
|
||||||
method_body, line = <<-EOV, __LINE__ + 1
|
method_body, line = <<-EOV, __LINE__ + 1
|
||||||
def #{attr_name}=(value)
|
def #{attr_name}=(value)
|
||||||
|
original_value = value
|
||||||
@timeliness_cache ||= {}
|
@timeliness_cache ||= {}
|
||||||
@timeliness_cache["#{attr_name}"] = value
|
@timeliness_cache["#{attr_name}"] = original_value
|
||||||
|
|
||||||
#{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
|
#{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
|
||||||
super
|
|
||||||
|
super(value)
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||||
|
|||||||
@ -3,12 +3,40 @@ module ValidatesTimeliness
|
|||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
def self.use_plugin_cache?
|
||||||
|
::ActiveRecord::VERSION::STRING < '3.1.0'
|
||||||
|
end
|
||||||
|
|
||||||
|
included do
|
||||||
|
unless ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||||
|
# Just use the built-in before_type_cast retrieval
|
||||||
|
alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def define_attribute_methods
|
def define_attribute_methods
|
||||||
super
|
super
|
||||||
# Define write method and before_type_cast method
|
use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||||
use_before_type_cast = ::ActiveRecord::VERSION::STRING < '3.1.0'
|
|
||||||
define_timeliness_methods(use_before_type_cast)
|
if use_before_type_cast || ValidatesTimeliness.use_plugin_parser
|
||||||
|
define_timeliness_methods(use_before_type_cast)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ActiveRecord >= 3.1.x has correct before_type_cast implementation to support plugin, except parser
|
||||||
|
unless ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
|
||||||
|
def define_timeliness_write_method(attr_name)
|
||||||
|
method_body, line = <<-EOV, __LINE__ + 1
|
||||||
|
def #{attr_name}=(value)
|
||||||
|
original_value = value
|
||||||
|
if original_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
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeliness_attribute_timezone_aware?(attr_name)
|
def timeliness_attribute_timezone_aware?(attr_name)
|
||||||
@ -30,10 +58,14 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload(*args)
|
# ActiveRecord >= 3.1.x needs no cached cleared
|
||||||
_clear_timeliness_cache
|
if use_plugin_cache?
|
||||||
super
|
def reload(*args)
|
||||||
|
_clear_timeliness_cache
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,7 +31,7 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
it 'should cache attribute raw value' do
|
it 'should cache attribute raw value' do
|
||||||
r = PersonWithCache.new
|
r = PersonWithCache.new
|
||||||
r.birth_datetime = date_string = '2010-01-01'
|
r.birth_datetime = date_string = '2010-01-01'
|
||||||
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
r._timeliness_raw_value_for('birth_datetime').should == date_string
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not overwrite user defined methods' do
|
it 'should not overwrite user defined methods' do
|
||||||
|
|||||||
@ -29,7 +29,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
it 'should cache attribute raw value' do
|
it 'should cache attribute raw value' do
|
||||||
r = EmployeeWithCache.new
|
r = EmployeeWithCache.new
|
||||||
r.birth_datetime = date_string = '2010-01-01'
|
r.birth_datetime = date_string = '2010-01-01'
|
||||||
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
r._timeliness_raw_value_for('birth_datetime').should == date_string
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with plugin parser" do
|
context "with plugin parser" do
|
||||||
@ -83,7 +83,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
r.birth_date = '2010-01-01'
|
r.birth_date = '2010-01-01'
|
||||||
r.reload
|
r.reload
|
||||||
|
|
||||||
r._timeliness_raw_value_for(:birth_date).should be_nil
|
r._timeliness_raw_value_for('birth_date').should be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -106,5 +106,17 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
r = Employee.last
|
r = Employee.last
|
||||||
r.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
|
r.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with plugin parser" do
|
||||||
|
with_config(:use_plugin_parser, true)
|
||||||
|
|
||||||
|
it 'should return original value' do
|
||||||
|
r = Employee.new
|
||||||
|
r.birth_datetime = date_string = '2010-01-31'
|
||||||
|
|
||||||
|
r.birth_datetime_before_type_cast.should == date_string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user