Module: HasUuid::ActiveRecord::FinderMethods

Defined in:
lib/has_uuid/active_record/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#find_one(id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/has_uuid/active_record/finder_methods.rb', line 6

def find_one(id)
  if id.is_a?(::UUIDTools::UUID) || id.to_s =~ VALID_FORMAT
    id = ::UUIDTools::UUID.parse(id) if id.is_a?(String)
    return self.find_by_uuid!(id)
  end
  super
end

#find_some(ids) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/has_uuid/active_record/finder_methods.rb', line 14

def find_some(ids)
  if ids.length > 0 && ids.inject(true) { |r, id| r && (id.is_a?(::UUIDTools::UUID) || id.to_s =~ VALID_FORMAT) }
    uuids = ids.map do |id|
      id = ::UUIDTools::UUID.parse(id) if id.is_a?(String)
      id
    end

    arr = where(table[:uuid].in(uuids))
    if ::ActiveRecord::VERSION::MAJOR >= 4
      result = arr.load
    else
      result = arr.all
    end

    expected_size =
      if @limit_value && ids.size > @limit_value
        @limit_value
      else
        ids.size
      end

      if @offset_value && (ids.size - @offset_value < expected_size)
        expected_size = ids.size - @offset_value
      end

      if result.size == expected_size
        result
      else
        conditions = arel.where_sql
        conditions = " [#{conditions}]" if conditions
        error = "Couldn't find all #{@klass.name.pluralize} with UUIDs "
        error << "(#{ids.join(", ")})#{conditions} (found #{result.size} results, but was looking for #{expected_size})"
        raise ::ActiveRecord::RecordNotFound, error
      end
  else
    super
  end
end