Class: DeepPreloader::WorklistEntry
- Inherits:
-
Object
- Object
- DeepPreloader::WorklistEntry
- Defined in:
- lib/deep_preloader.rb
Instance Attribute Summary collapse
-
#association_reflection ⇒ Object
readonly
Returns the value of attribute association_reflection.
-
#child_spec ⇒ Object
readonly
Returns the value of attribute child_spec.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #association_macro ⇒ Object
- #association_name ⇒ Object
- #belongs_to? ⇒ Boolean
-
#children ⇒ Object
Conceal the difference between singular and collection associations so that
load_childrencan alwaysgroup_bythe key. - #children=(targets) ⇒ Object
- #collection? ⇒ Boolean
-
#initialize(model, association_reflection, child_spec) ⇒ WorklistEntry
constructor
A new instance of WorklistEntry.
- #key ⇒ Object
- #loaded? ⇒ Boolean
- #parent_key_column ⇒ Object
Constructor Details
#initialize(model, association_reflection, child_spec) ⇒ WorklistEntry
Returns a new instance of WorklistEntry.
204 205 206 207 208 |
# File 'lib/deep_preloader.rb', line 204 def initialize(model, association_reflection, child_spec) @model = model @association_reflection = association_reflection @child_spec = child_spec end |
Instance Attribute Details
#association_reflection ⇒ Object (readonly)
Returns the value of attribute association_reflection.
202 203 204 |
# File 'lib/deep_preloader.rb', line 202 def association_reflection @association_reflection end |
#child_spec ⇒ Object (readonly)
Returns the value of attribute child_spec.
202 203 204 |
# File 'lib/deep_preloader.rb', line 202 def child_spec @child_spec end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
202 203 204 |
# File 'lib/deep_preloader.rb', line 202 def model @model end |
Instance Method Details
#association_macro ⇒ Object
214 215 216 |
# File 'lib/deep_preloader.rb', line 214 def association_macro @association_reflection.macro end |
#association_name ⇒ Object
210 211 212 |
# File 'lib/deep_preloader.rb', line 210 def association_name @association_reflection.name end |
#belongs_to? ⇒ Boolean
222 223 224 |
# File 'lib/deep_preloader.rb', line 222 def belongs_to? association_macro == :belongs_to end |
#children ⇒ Object
Conceal the difference between singular and collection associations so that load_children can always group_by the key
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/deep_preloader.rb', line 236 def children target = model.association(association_name).target if collection? target elsif target [target] else [] end end |
#children=(targets) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/deep_preloader.rb', line 248 def children=(targets) if collection? target = targets else if targets.size > 1 raise RuntimeError.new('Internal preloader error: attempted to attach multiple children to a singular association') end target = targets.first end ActiveRecord::Base.logger&.debug("attaching children to #{model.inspect}.#{association_name}: #{targets}") if DEBUG association = model.association(association_name) association.loaded! association.target = target targets.each { |t| association.set_inverse_instance(t) } end |
#collection? ⇒ Boolean
226 227 228 |
# File 'lib/deep_preloader.rb', line 226 def collection? association_macro == :has_many end |
#key ⇒ Object
230 231 232 |
# File 'lib/deep_preloader.rb', line 230 def key model.read_attribute(parent_key_column) end |
#loaded? ⇒ Boolean
218 219 220 |
# File 'lib/deep_preloader.rb', line 218 def loaded? model.association(association_name).loaded? end |
#parent_key_column ⇒ Object
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/deep_preloader.rb', line 267 def parent_key_column case association_macro when :belongs_to @association_reflection.foreign_key when :has_one, :has_many @association_reflection.active_record_primary_key else raise "Unsupported association type #{@association_reflection.macro}" end end |