mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Fix thread lock on undefine
Move active model methods into active model ORM module to be included manually if not using AR or other ORM
This commit is contained in:
parent
ff131415f7
commit
321834b5ff
@ -33,17 +33,11 @@ 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
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
|||||||
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,7 +14,7 @@ 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 |key|
|
columns_hash.fetch(attr_name.to_s) do |key|
|
||||||
validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -239,4 +239,10 @@ RSpec.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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user