lib/ensurance.rb in ensurance-0.1.11 vs lib/ensurance.rb in ensurance-0.1.12

- old
+ new

@@ -1,11 +1,12 @@ -require "ensurance/version" +require 'ensurance/version' require 'active_support' require 'ensurance/date_ensure' require 'ensurance/time_ensure' require 'ensurance/hash_ensure' +require 'ensurance/array_ensure' module Ensurance extend ActiveSupport::Concern class_methods do @@ -25,14 +26,14 @@ return found elsif thing.is_a?(String) && (found = GlobalID::Locator.locate(thing)) return found end - @_ensure_by ||= [@_additional_ensure_by || self.primary_key].flatten.compact.uniq + @_ensure_by ||= [@_additional_ensure_by || primary_key].flatten.compact.uniq found = [] - things = Array(thing) + things = [thing].flatten things.each do |thing| record = nil @_ensure_by.each do |ensure_field| value = thing if thing.is_a?(Hash) @@ -51,18 +52,16 @@ end found << record end found.compact! - thing.is_a?(Array) ? found : found.first end def ensure!(thing = nil) return nil unless thing.present? result = self.ensure(thing) - raise ::ActiveRecord::RecordNotFound.new("#{self} not found") unless result + raise ActiveRecord::RecordNotFound, "#{self} not found" unless result result end - end end