lib/blind_index/model.rb in blind_index-1.0.0 vs lib/blind_index/model.rb in blind_index-1.0.1
- old
+ new
@@ -31,11 +31,11 @@
attribute = attribute.to_sym
method_name = :"compute_#{name}_bidx"
class_method_name = :"generate_#{name}_bidx"
key = options[:key]
- key ||= -> { BlindIndex.index_key(table: table_name, bidx_attribute: bidx_attribute, master_key: options[:master_key], encode: false) }
+ key ||= -> { BlindIndex.index_key(table: try(:table_name) || collection_name.to_s, bidx_attribute: bidx_attribute, master_key: options[:master_key], encode: false) }
class_eval do
@blind_indexes ||= {}
unless respond_to?(:blind_indexes)
@@ -72,10 +72,18 @@
define_method method_name do
self.send("#{bidx_attribute}=", self.class.send(class_method_name, send(attribute)))
end
if callback
- before_validation method_name, if: -> { changes.key?(attribute.to_s) }
+ 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?") }
+ end
end
# use include so user can override
include InstanceMethods if blind_indexes.size == 1
end