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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
class Person
include GlobalID::Identification
HARDCODED_ID_FOR_MISSING_PERSON = '1000'
attr_reader :id
def self.primary_key
:id
end
def self.find(id_or_ids)
if id_or_ids.is_a? Array
ids = id_or_ids
ids.collect { |id| find(id) }
else
id = id_or_ids
if id == HARDCODED_ID_FOR_MISSING_PERSON
raise 'Person missing'
else
new(id)
end
end
end
def self.where(conditions)
(conditions[primary_key] - [HARDCODED_ID_FOR_MISSING_PERSON]).collect { |id| new(id) }
end
def initialize(id = 1)
@id = id
end
def ==(other)
other.is_a?(self.class) && id == other.try(:id)
end
end
class PersonUuid < Person
def self.primary_key
:uuid
end
end
class Person::Scoped < Person
def initialize(*)
super
@find_allowed = false
end
def self.unscoped
@find_allowed = true
yield
end
def self.find(*)
super if @find_allowed
end
end
class Person::ChildWithParent < Person
def self.find(id_or_ids)
if id_or_ids.is_a? Array
ids = id_or_ids
ids.collect { |id| find(id) }
else
id = id_or_ids
if id == HARDCODED_ID_FOR_MISSING_PERSON
raise 'Person missing'
else
Person::Child.new(id, Person.new(id))
end
end
end
def self.where(conditions)
(conditions[:id] - [HARDCODED_ID_FOR_MISSING_PERSON]).collect do |id|
Person::Child.new(id, Person.new(id))
end
end
end
class Person::Child < Person
attr_accessor :parent
def initialize(id = 1, parent = nil)
@id = id
@parent = parent
end
def self.includes(relationships)
return Person::ChildWithParent if relationships == :parent
return self if relationships.nil?
raise StandardError, 'Relationship does not exist'
end
def ==(other)
other.is_a?(self.class) && id == other.try(:id) && parent == other.parent
end
end
class PersonWithoutPrimaryKey
include GlobalID::Identification
attr_reader :id
def self.find(id_or_ids)
if id_or_ids.is_a? Array
ids = id_or_ids
ids.collect { |id| find(id) }
else
id = id_or_ids
new(id)
end
end
def self.where(conditions)
(conditions[:id]).collect { |id| new(id) }
end
def initialize(id = 1)
@id = id
end
end
|