mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
actually use the plugin parser in the write method (yikes)
This commit is contained in:
parent
0b067ba27f
commit
27e01486e9
@ -9,6 +9,7 @@ require 'active_support/core_ext/date_time/acts_like'
|
|||||||
require 'active_support/core_ext/date_time/conversions'
|
require 'active_support/core_ext/date_time/conversions'
|
||||||
|
|
||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
|
autoload :Parser, 'validates_timeliness/parser'
|
||||||
autoload :VERSION, 'validates_timeliness/version'
|
autoload :VERSION, 'validates_timeliness/version'
|
||||||
|
|
||||||
# Add plugin to supported ORMs (only :active_record for now)
|
# Add plugin to supported ORMs (only :active_record for now)
|
||||||
@ -45,7 +46,6 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'validates_timeliness/parser'
|
|
||||||
require 'validates_timeliness/conversion'
|
require 'validates_timeliness/conversion'
|
||||||
require 'validates_timeliness/validator'
|
require 'validates_timeliness/validator'
|
||||||
require 'validates_timeliness/helper_methods'
|
require 'validates_timeliness/helper_methods'
|
||||||
|
|||||||
@ -18,6 +18,7 @@ module ValidatesTimeliness
|
|||||||
def #{attr_name}=(value)
|
def #{attr_name}=(value)
|
||||||
@attributes_cache ||= {}
|
@attributes_cache ||= {}
|
||||||
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||||
|
#{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
|
|||||||
@ -56,6 +56,7 @@ class Person
|
|||||||
define_attribute_methods model_attributes
|
define_attribute_methods model_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||||
ActiveRecord::Migration.verbose = false
|
ActiveRecord::Migration.verbose = false
|
||||||
ActiveRecord::Schema.define(:version => 1) do
|
ActiveRecord::Schema.define(:version => 1) do
|
||||||
|
|||||||
@ -6,16 +6,43 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "attribute write method" do
|
context "attribute write method" do
|
||||||
class EmployeeCopy < ActiveRecord::Base
|
class EmployeeWithCache < ActiveRecord::Base
|
||||||
set_table_name 'employees'
|
set_table_name 'employees'
|
||||||
validates_datetime :birth_datetime
|
validates_datetime :birth_datetime
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should cache attribute raw value' do
|
it 'should cache attribute raw value' do
|
||||||
r = EmployeeCopy.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
|
||||||
|
class EmployeeWithParser < ActiveRecord::Base
|
||||||
|
set_table_name 'employees'
|
||||||
|
validates_datetime :birth_date
|
||||||
|
end
|
||||||
|
|
||||||
|
before :all do
|
||||||
|
ValidatesTimeliness.use_plugin_parser = true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should parse a string value' do
|
||||||
|
ValidatesTimeliness::Parser.should_receive(:parse)
|
||||||
|
r = EmployeeWithParser.new
|
||||||
|
r.birth_date = '2010-01-01'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should be strict on day values' do
|
||||||
|
r = EmployeeWithParser.new
|
||||||
|
r.birth_date = '2010-02-31'
|
||||||
|
r.birth_date.should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
ValidatesTimeliness.use_plugin_parser = false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "before_type_cast method" do
|
context "before_type_cast method" do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user