/
query_attribute.rb
45 lines (37 loc) · 1.12 KB
/
query_attribute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
# frozen_string_literal: true
require "active_model/attribute"
module ActiveRecord
class Relation
class QueryAttribute < ActiveModel::Attribute # :nodoc:
def type_cast(value)
value
end
def value_for_database
@value_for_database = _value_for_database unless defined?(@value_for_database)
@value_for_database
end
def with_cast_value(value)
QueryAttribute.new(name, value, type)
end
def nil?
unless value_before_type_cast.is_a?(StatementCache::Substitute)
value_before_type_cast.nil? ||
type.respond_to?(:subtype) && serializable? && value_for_database.nil?
end
end
def infinite?
infinity?(value_before_type_cast) || serializable? && infinity?(value_for_database)
end
def unboundable?
unless defined?(@_unboundable)
serializable? { |value| @_unboundable = value <=> 0 } && @_unboundable = nil
end
@_unboundable
end
private
def infinity?(value)
value.respond_to?(:infinite?) && value.infinite?
end
end
end
end