Class: Formed::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/formed/reflection.rb

Overview

Holds all the metadata about an association as it was specified in the Active Record class.

Instance Attribute Summary collapse

Attributes inherited from MacroReflection

#active_form, #name, #options, #plural_name, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #autosave=, #klass, #scope_for

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #inverse_of, #scopes, #strict_loading?, #strict_loading_violation_message, #table_name, #through_reflection?

Constructor Details

#initialize(name, scope, options, active_form) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



349
350
351
352
353
354
355
# File 'lib/formed/reflection.rb', line 349

def initialize(name, scope, options, active_form)
  super
  @type = -(options[:foreign_type].to_s || "#{options[:as]}_type") if options[:as]
  @foreign_type = -(options[:foreign_type].to_s || "#{name}_type") if options[:polymorphic]

  ensure_option_not_given_as_class!(:class_name)
end

Instance Attribute Details

#foreign_typeObject (readonly)

Returns the value of attribute foreign_type.



346
347
348
# File 'lib/formed/reflection.rb', line 346

def foreign_type
  @foreign_type
end

#parent_reflectionObject

Reflection



347
348
349
# File 'lib/formed/reflection.rb', line 347

def parent_reflection
  @parent_reflection
end

#typeObject (readonly)

Returns the value of attribute type.



346
347
348
# File 'lib/formed/reflection.rb', line 346

def type
  @type
end

Instance Method Details

#add_as_polymorphic_through(reflection, seed) ⇒ Object



473
474
475
# File 'lib/formed/reflection.rb', line 473

def add_as_polymorphic_through(reflection, seed)
  seed + [PolymorphicReflection.new(self, reflection)]
end

#add_as_source(seed) ⇒ Object



469
470
471
# File 'lib/formed/reflection.rb', line 469

def add_as_source(seed)
  seed
end

#add_as_through(seed) ⇒ Object



477
478
479
# File 'lib/formed/reflection.rb', line 477

def add_as_through(seed)
  seed + [self]
end

#association_classObject

Raises:

  • (NotImplementedError)


461
462
463
# File 'lib/formed/reflection.rb', line 461

def association_class
  raise NotImplementedError
end

#association_foreign_keyObject



365
366
367
# File 'lib/formed/reflection.rb', line 365

def association_foreign_key
  @association_foreign_key ||= -(options[:association_foreign_key].to_s || class_name.foreign_key)
end

#association_primary_key(klass = nil) ⇒ Object



369
370
371
# File 'lib/formed/reflection.rb', line 369

def association_primary_key(klass = nil)
  primary_key(klass || self.klass)
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


452
453
454
# File 'lib/formed/reflection.rb', line 452

def belongs_to?
  false
end

#check_eager_loadable!Object

Raises:

  • (ArgumentError)


377
378
379
380
381
382
383
384
385
386
387
# File 'lib/formed/reflection.rb', line 377

def check_eager_loadable!
  return unless scope

  return if scope.arity.zero?

  raise ArgumentError, <<-MSG.squish
      The association scope '#{name}' is instance dependent (the scope
      block takes an argument). Eager loading instance dependent scopes
      is not supported.
  MSG
end

#check_validity!Object



373
374
375
# File 'lib/formed/reflection.rb', line 373

def check_validity!
  check_validity_of_inverse!
end

#collect_join_chainObject

A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.



399
400
401
# File 'lib/formed/reflection.rb', line 399

def collect_join_chain
  [self]
end

#collection?Boolean

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

Returns:

  • (Boolean)


434
435
436
# File 'lib/formed/reflection.rb', line 434

def collection?
  false
end

#compute_class(name) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/formed/reflection.rb', line 326

def compute_class(name)
  raise ArgumentError, "Polymorphic associations do not support computing the class." if polymorphic?

  msg = <<-MSG.squish
    Formed couldn't find a valid form for #{name} association.
    Please provide the :class_name option on the association declaration.
    If :class_name is already provided, make sure it's an Formed::Base subclass.
  MSG

  begin
    klass = active_form.send(:compute_type, name)

    raise ArgumentError, msg unless klass < Formed::Base

    klass
  rescue NameError
    raise NameError, msg
  end
end

#extensionsObject



481
482
483
# File 'lib/formed/reflection.rb', line 481

def extensions
  Array(options[:extend])
end

#foreign_keyObject



361
362
363
# File 'lib/formed/reflection.rb', line 361

def foreign_key
  @foreign_key ||= -(options[:foreign_key].to_s || derive_foreign_key)
end

#has_inverse?Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/formed/reflection.rb', line 411

def has_inverse?
  inverse_name
end

#has_one?Boolean

Returns true if self is a has_one reflection.

Returns:

  • (Boolean)


457
458
459
# File 'lib/formed/reflection.rb', line 457

def has_one?
  false
end

#has_scope?Boolean

Returns:

  • (Boolean)


407
408
409
# File 'lib/formed/reflection.rb', line 407

def has_scope?
  scope
end

#join_tableObject



357
358
359
# File 'lib/formed/reflection.rb', line 357

def join_table
  @join_table ||= -(options[:join_table].to_s || derive_join_table)
end

#macroObject

Returns the macro type.

has_many :clients returns :has_many

Raises:

  • (NotImplementedError)


427
428
429
# File 'lib/formed/reflection.rb', line 427

def macro
  raise NotImplementedError
end

#nested?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/formed/reflection.rb', line 403

def nested?
  false
end

#polymorphic?Boolean

Returns:

  • (Boolean)


465
466
467
# File 'lib/formed/reflection.rb', line 465

def polymorphic?
  options[:polymorphic]
end

#polymorphic_inverse_of(associated_class) ⇒ Object



415
416
417
418
419
420
421
422
# File 'lib/formed/reflection.rb', line 415

def polymorphic_inverse_of(associated_class)
  return unless has_inverse?
  unless (inverse_relationship = associated_class._reflect_on_association(options[:inverse_of]))
    raise InverseOfAssociationNotFoundError.new(self, associated_class)
  end

  inverse_relationship
end

#source_reflectionObject



393
394
395
# File 'lib/formed/reflection.rb', line 393

def source_reflection
  self
end

#through_reflectionObject



389
390
391
# File 'lib/formed/reflection.rb', line 389

def through_reflection
  nil
end

#validate?Boolean

Returns whether or not the association should be validated as part of the parent’s validation.

Unless you explicitly disable validation with validate: false, validation will take place when:

  • you explicitly enable validation; validate: true

  • you use autosave; autosave: true

  • the association is a has_many association

Returns:

  • (Boolean)


447
448
449
# File 'lib/formed/reflection.rb', line 447

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?)
end