mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +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'
|
||||
|
||||
module ValidatesTimeliness
|
||||
autoload :Parser, 'validates_timeliness/parser'
|
||||
autoload :VERSION, 'validates_timeliness/version'
|
||||
|
||||
# Add plugin to supported ORMs (only :active_record for now)
|
||||
@ -45,7 +46,6 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
require 'validates_timeliness/parser'
|
||||
require 'validates_timeliness/conversion'
|
||||
require 'validates_timeliness/validator'
|
||||
require 'validates_timeliness/helper_methods'
|
||||
|
||||
@ -18,6 +18,7 @@ module ValidatesTimeliness
|
||||
def #{attr_name}=(value)
|
||||
@attributes_cache ||= {}
|
||||
@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
|
||||
end
|
||||
EOV
|
||||
|
||||
@ -56,6 +56,7 @@ class Person
|
||||
define_attribute_methods model_attributes
|
||||
end
|
||||
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Schema.define(:version => 1) do
|
||||
|
||||
@ -6,16 +6,43 @@ describe ValidatesTimeliness::AttributeMethods do
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
class EmployeeCopy < ActiveRecord::Base
|
||||
class EmployeeWithCache < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
validates_datetime :birth_datetime
|
||||
end
|
||||
|
||||
it 'should cache attribute raw value' do
|
||||
r = EmployeeCopy.new
|
||||
r = EmployeeWithCache.new
|
||||
r.birth_datetime = date_string = '2010-01-01'
|
||||
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
||||
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
|
||||
|
||||
context "before_type_cast method" do
|
||||
|
||||
Loading…
Reference in New Issue
Block a user