From 321834b5ffe6297f03f6e8485aaeaa5edec780e5 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 6 Jan 2016 22:18:54 +1100 Subject: [PATCH] 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 --- lib/validates_timeliness/attribute_methods.rb | 10 ++-------- lib/validates_timeliness/orm/active_model.rb | 20 +++++++++++++++++++ lib/validates_timeliness/orm/active_record.rb | 2 +- spec/spec_helper.rb | 8 ++------ .../orm/active_record_spec.rb | 6 ++++++ 5 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 lib/validates_timeliness/orm/active_model.rb diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 55fa730..9f1a995 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -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 diff --git a/lib/validates_timeliness/orm/active_model.rb b/lib/validates_timeliness/orm/active_model.rb new file mode 100644 index 0000000..ec11032 --- /dev/null +++ b/lib/validates_timeliness/orm/active_model.rb @@ -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 diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index de257a6..b8ac2aa 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1a2025b..a1fc8bc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/validates_timeliness/orm/active_record_spec.rb b/spec/validates_timeliness/orm/active_record_spec.rb index 3afc9ae..c2334ce 100644 --- a/spec/validates_timeliness/orm/active_record_spec.rb +++ b/spec/validates_timeliness/orm/active_record_spec.rb @@ -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