/
options.rb
129 lines (110 loc) · 3.59 KB
/
options.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
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
128
129
# frozen_string_literal: true
# rubocop:todo all
module Mongoid
module Association
# Options context used for all association types.
module Options
# Returns the name of the parent to a polymorphic child.
#
# @return [ String | Symbol ] The name.
def as
@options[:as]
end
# Specify what happens to the associated object when the owner is destroyed.
#
# @return [ String ] The dependent option.
def dependent
@options[:dependent]
end
# The custom sorting options on the association.
#
# @return [ Criteria::Queryable::Key ] The custom sorting options.
def order
@options[:order]
end
# Whether to index the primary or foreign key field.
#
# @return [ true | false ]
def indexed?
@indexed ||= !!@options[:index]
end
# Whether the association is autobuilding.
#
# @return [ true | false ]
def autobuilding?
!!@options[:autobuild]
end
# Is the association cyclic.
#
# @return [ true | false ] Whether the association is cyclic.
def cyclic?
!!@options[:cyclic]
end
# The name the owning object uses to refer to this association.
#
# @return [ String ] The inverse_of option.
def inverse_of
@options[:inverse_of]
end
# Mongoid assumes that the field used to hold the primary key of the association is id.
# You can override this and explicitly specify the primary key with the :primary_key option.
#
# @return [ Symbol | String ] The primary key.
def primary_key
@primary_key ||= @options[:primary_key] ? @options[:primary_key].to_s : Relatable::PRIMARY_KEY_DEFAULT
end
# Options to save any loaded members and destroy members that are marked for destruction
# when the parent object is saved.
#
# @return [ true | false ] The autosave option.
def autosave
!!@options[:autosave]
end
alias :autosave? :autosave
# Whether the association is counter-cached.
#
# @return [ true | false ]
def counter_cached?
!!@options[:counter_cache]
end
# Whether this association is polymorphic.
#
# @return [ true | false ] Whether the association is polymorphic.
def polymorphic?; false; end
# Whether the association has callbacks cascaded down from the parent.
#
# @return [ true | false ] Whether callbacks are cascaded.
def cascading_callbacks?
!!@options[:cascade_callbacks]
end
# The store_as option.
#
# @return [ nil ] Default is nil.
def store_as; end
# Whether the association has forced nil inverse (So no foreign keys are saved).
#
# @return [ false ] Default is false.
def forced_nil_inverse?; false; end
# The field for saving the associated object's type.
#
# @return [ nil ] Default is nil.
def type; end
# The field for saving the associated object's type.
#
# @return [ nil ] Default is nil.
def touch_field
@touch_field ||= options[:touch] if (options[:touch].is_a?(String) || options[:touch].is_a?(Symbol))
end
# Whether the association object should be automatically touched
# when its inverse object is updated.
#
# @return [ true | false ] returns true if this association is
# automatically touched, false otherwise. The default is false.
#
# @api private
def touchable?
!!@options[:touch]
end
end
end
end