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:
Adam Meehan 2016-01-06 22:18:54 +11:00
parent ff131415f7
commit 321834b5ff
5 changed files with 31 additions and 15 deletions

View File

@ -33,15 +33,9 @@ module ValidatesTimeliness
}.tap { |mod| include mod }
end
def undefine_attribute_methods
super.tap { undefine_timeliness_attribute_methods }
end
def undefine_timeliness_attribute_methods
generated_timeliness_methods.synchronize do
generated_timeliness_methods.module_eval do
instance_methods.each { |m| undef_method(m) }
end
generated_timeliness_methods.module_eval do
instance_methods.each { |m| undef_method(m) }
end
end

View 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

View File

@ -14,7 +14,7 @@ module ValidatesTimeliness
timeliness_column_for_attribute(attr_name).type
end
if ActiveModel.version >= Gem::Version.new('4.2')
if ::ActiveModel.version >= Gem::Version.new('4.2')
def timeliness_column_for_attribute(attr_name)
columns_hash.fetch(attr_name.to_s) do |key|
validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s

View File

@ -7,6 +7,7 @@ require 'action_view'
require 'timecop'
require 'validates_timeliness'
require 'validates_timeliness/orm/active_model'
require 'support/test_model'
require 'support/model_helpers'
@ -30,14 +31,9 @@ I18n.available_locales = ['en', 'es']
module TestModelShim
extend ActiveSupport::Concern
include ValidatesTimeliness::AttributeMethods
include ValidatesTimeliness::ORM::ActiveModel
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
def timeliness_attribute_timezone_aware?(attr_name)
false

View File

@ -239,4 +239,10 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
expect(Employee.define_attribute_methods).to be_falsey
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