lib/ensurance.rb in ensurance-0.1.15 vs lib/ensurance.rb in ensurance-0.1.16
- old
+ new
@@ -8,13 +8,20 @@
module Ensurance
extend ActiveSupport::Concern
class_methods do
- def ensure_by(*args)
+ @_additional_ensure_by = []
+ @_ensure_order = nil
+ @_ensure_by = []
+
+ def ensure_by(*args, order: nil)
@_additional_ensure_by = args
- @_ensure_by = nil
+ @_ensure_order = (order || primary_key).to_s
+ @_ensure_by = [@_additional_ensure_by || primary_key].flatten.compact.uniq
+ # ap "Ensure By: #{self}.#{@_ensure_by} Order: #{self}.#{@_ensure_order}"
+ raise ArgumentError.new("#{self} does not have column[#{@_ensure_order}] to sort by") unless self.column_names.include?(@_ensure_order)
end
def ensure(thing = nil)
return nil unless thing.present?
@@ -27,10 +34,11 @@
elsif thing.is_a?(String) && (found = GlobalID::Locator.locate(thing))
return found
end
@_ensure_by ||= [@_additional_ensure_by || primary_key].flatten.compact.uniq
+ @_ensure_order ||= primary_key
found = []
things = [thing].flatten
things.each do |thing|
record = nil
@@ -40,10 +48,12 @@
value = thing.fetch(ensure_field.to_sym, nil) || thing.fetch(ensure_field.to_s, nil)
end
if ensure_field.to_sym == :id
begin
# Always return the most recent record that matches
- record = where(ensure_field => value).order(created_at: :desc).first
+ query = where(ensure_field => value)
+ query = query.order("#{@_ensure_order}" => 'desc')
+ record = query.first
rescue ActiveRecord::RecordNotFound
nil
end
else
record = find_by(ensure_field => value) if value.present? && !value.is_a?(Hash)