lib/blind_index/model.rb in blind_index-1.0.2 vs lib/blind_index/model.rb in blind_index-2.0.0

- old
+ new

@@ -62,28 +62,33 @@ define_singleton_method class_method_name do |value| BlindIndex.generate_bidx(value, **blind_indexes[name]) end - define_singleton_method method_name do |value| - ActiveSupport::Deprecation.warn("Use #{class_method_name} instead") - send(class_method_name, value) - end - define_method method_name do self.send("#{bidx_attribute}=", self.class.send(class_method_name, send(attribute))) end if callback - if defined?(ActiveRecord) && self < ActiveRecord::Base - # Active Record - # prevent deprecation warnings - before_validation method_name, if: -> { changes.key?(attribute.to_s) } - else - # Mongoid - # Lockbox only supports attribute_changed? - before_validation method_name, if: -> { send("#{attribute}_changed?") } + activerecord = defined?(ActiveRecord) && self < ActiveRecord::Base + + # TODO reuse module + m = Module.new do + define_method "#{attribute}=" do |value| + result = super(value) + send(method_name) + result + end + + unless activerecord + define_method "reset_#{attribute}!" do + result = super() + send(method_name) + result + end + end end + prepend m end # use include so user can override include InstanceMethods if blind_indexes.size == 1 end