| Class | Sequel::Model::Associations::AssociationReflection |
| In: |
lib/sequel/model/associations.rb
|
| Parent: | Hash |
AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It provides methods to reduce internal code duplication. It should not be instantiated by the user.
Name symbol for the _add internal association method
# File lib/sequel/model/associations.rb, line 21
21: def _add_method
22:
23: "_add_#{singularize(self[:name])}"
24: end
Name symbol for the _remove_all internal association method
# File lib/sequel/model/associations.rb, line 26
26: def _remove_all_method
27:
28: "_remove_all_#{self[:name]}"
29: end
Name symbol for the _remove internal association method
# File lib/sequel/model/associations.rb, line 31
31: def _remove_method
32:
33: "_remove_#{singularize(self[:name])}"
34: end
Name symbol for the _setter association method
# File lib/sequel/model/associations.rb, line 36
36: def _setter_method
37:
38: "_#{self[:name]}="
39: end
Name symbol for the add association method
# File lib/sequel/model/associations.rb, line 41
41: def add_method
42:
43: "add_#{singularize(self[:name])}"
44: end
Apply all non-instance specific changes to the given dataset and return it.
# File lib/sequel/model/associations.rb, line 62
62: def apply_dataset_changes(ds)
63: ds.extend(AssociationDatasetMethods)
64: ds.association_reflection = self
65: self[:extend].each{|m| ds.extend(m)}
66: ds = ds.select(*select) if select
67: if c = self[:conditions]
68: ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c)
69: end
70: ds = ds.order(*self[:order]) if self[:order]
71: ds = ds.limit(*self[:limit]) if self[:limit]
72: ds = ds.limit(1) if !returns_array? && self[:key]
73: ds = ds.eager(*self[:eager]) if self[:eager]
74: ds = ds.distinct if self[:distinct]
75: ds
76: end
The class associated to the current model class via this association
# File lib/sequel/model/associations.rb, line 51
51: def associated_class
52: cached_fetch(:class){constantize(self[:class_name])}
53: end
The dataset associated via this association, with the non-instance specific changes already applied.
# File lib/sequel/model/associations.rb, line 57
57: def associated_dataset
58: cached_fetch(:_dataset){apply_dataset_changes(associated_class.dataset.clone)}
59: end
Name symbol for association method, the same as the name of the association.
# File lib/sequel/model/associations.rb, line 46
46: def association_method
47: self[:name]
48: end
Whether this association can have associated objects, given the current object. Should be false if obj cannot have associated objects because the necessary key columns are NULL.
# File lib/sequel/model/associations.rb, line 81
81: def can_have_associated_objects?(obj)
82: true
83: end
Name symbol for the dataset association method
# File lib/sequel/model/associations.rb, line 86
86: def dataset_method
87:
88: "#{self[:name]}_dataset"
89: end
Whether the dataset needs a primary key to function, true by default.
# File lib/sequel/model/associations.rb, line 91
91: def dataset_need_primary_key?
92: true
93: end
Whether to eagerly graph a lazy dataset, true by default. If this is false, the association won‘t respect the :eager_graph option when loading the association for a single record.
# File lib/sequel/model/associations.rb, line 135
135: def eager_graph_lazy_dataset?
136: true
137: end
The eager limit strategy to use for this dataset.
# File lib/sequel/model/associations.rb, line 96
96: def eager_limit_strategy
97: cached_fetch(:_eager_limit_strategy) do
98: if self[:limit]
99: case s = cached_fetch(:eager_limit_strategy){self[:model].default_eager_limit_strategy || :ruby}
100: when true
101: ds = associated_class.dataset
102: if ds.supports_window_functions?
103: :window_function
104: else
105: :ruby
106: end
107: else
108: s
109: end
110: else
111: nil
112: end
113: end
114: end
The key to use for the key hash when eager loading
# File lib/sequel/model/associations.rb, line 117
117: def eager_loader_key
118: self[:eager_loader_key]
119: end
Alias of predicate_key, only for backwards compatibility.
# File lib/sequel/model/associations.rb, line 128
128: def eager_loading_predicate_key
129: predicate_key
130: end
The limit and offset for this association (returned as a two element array).
# File lib/sequel/model/associations.rb, line 140
140: def limit_and_offset
141: if (v = self[:limit]).is_a?(Array)
142: v
143: else
144: [v, nil]
145: end
146: end
Whether the associated object needs a primary key to be added/removed, false by default.
# File lib/sequel/model/associations.rb, line 150
150: def need_associated_primary_key?
151: false
152: end
The keys to use for loading of the regular dataset, as an array.
# File lib/sequel/model/associations.rb, line 155
155: def predicate_keys
156: cached_fetch(:predicate_keys){Array(predicate_key)}
157: end
Qualify col with the given table name. If col is an array of columns, return an array of qualified columns. Only qualifies Symbols and SQL::Identifier values, other values are not modified.
# File lib/sequel/model/associations.rb, line 162
162: def qualify(table, col)
163: transform(col) do |k|
164: case k
165: when Symbol, SQL::Identifier
166: SQL::QualifiedIdentifier.new(table, k)
167: else
168: Sequel::Qualifier.new(self[:model].dataset, table).transform(k)
169: end
170: end
171: end
Qualify col with the associated model‘s table name.
# File lib/sequel/model/associations.rb, line 174
174: def qualify_assoc(col)
175: qualify(associated_class.table_name, col)
176: end
Qualify col with the current model‘s table name.
# File lib/sequel/model/associations.rb, line 179
179: def qualify_cur(col)
180: qualify(self[:model].table_name, col)
181: end
Returns the reciprocal association variable, if one exists. The reciprocal association is the association in the associated class that is the opposite of the current association. For example, Album.many_to_one :artist and Artist.one_to_many :albums are reciprocal associations. This information is to populate reciprocal associations. For example, when you do this_artist.add_album(album) it sets album.artist to this_artist.
# File lib/sequel/model/associations.rb, line 189
189: def reciprocal
190: cached_fetch(:reciprocal) do
191: possible_recips = []
192: fallback_recips = []
193:
194: associated_class.all_association_reflections.each do |assoc_reflect|
195: if reciprocal_association?(assoc_reflect)
196: if deprecated_reciprocal_association?(assoc_reflect)
197: fallback_recips << assoc_reflect
198: else
199: possible_recips << assoc_reflect
200: end
201: end
202: end
203:
204: Sequel::Deprecation.deprecate("Multiple reciprocal association candidates found for #{self[:name]} association (#{possible_recips.map{|r| r[:name]}.join(', ')}). Choosing the first candidate is", "Please explicitly specify the reciprocal option for the #{self[:name]} association") if possible_recips.size >= 2
205: if possible_recips.empty? && !fallback_recips.empty?
206: possible_recips = fallback_recips
207: Sequel::Deprecation.deprecate("All reciprocal association candidates found for #{self[:name]} association have conditions, blocks, or differing primary keys (#{possible_recips.map{|r| r[:name]}.join(', ')}). Automatic choosing of an reciprocal association with conditions or blocks is", "Please explicitly specify the reciprocal option for the #{self[:name]} association")
208: end
209:
210: unless possible_recips.empty?
211: cached_set(:reciprocal_type, possible_recips.first[:type]) if reciprocal_type.is_a?(Array)
212: possible_recips.first[:name]
213: end
214: end
215: end
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
# File lib/sequel/model/associations.rb, line 219
219: def reciprocal_array?
220: true
221: end
Name symbol for the remove_all_ association method
# File lib/sequel/model/associations.rb, line 224
224: def remove_all_method
225:
226: "remove_all_#{self[:name]}"
227: end
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
# File lib/sequel/model/associations.rb, line 230
230: def remove_before_destroy?
231: true
232: end
Name symbol for the remove_ association method
# File lib/sequel/model/associations.rb, line 235
235: def remove_method
236:
237: "remove_#{singularize(self[:name])}"
238: end
Whether to check that an object to be disassociated is already associated to this object, false by default.
# File lib/sequel/model/associations.rb, line 240
240: def remove_should_check_existing?
241: false
242: end
Whether this association returns an array of objects instead of a single object, true by default.
# File lib/sequel/model/associations.rb, line 246
246: def returns_array?
247: true
248: end
Whether to set the reciprocal association to self when loading associated records, false by default.
# File lib/sequel/model/associations.rb, line 257
257: def set_reciprocal_to_self?
258: false
259: end