class Vorpal::Config::AssociationConfig
@private Object association terminology:
-
All object associations are uni-directional
-
The end that holds the association is the ‘Owner’ and the end that is referred to is the ‘Associate’ or ‘Associates’
Relational association terminology:
-
Local end: has FK
-
Remote end: has no FK
Attributes
fk[R]
local_class_config[R]
local_end_config[RW]
Only one of these two attributes needs to be specified If one is specified, then the association is uni-directional. If both are specified, then the association is bi-directional.
remote_class_configs[R]
remote_end_config[RW]
Only one of these two attributes needs to be specified If one is specified, then the association is uni-directional. If both are specified, then the association is bi-directional.
Public Class Methods
new(local_class_config, fk, fk_type)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 26 def initialize(local_class_config, fk, fk_type) @local_class_config = local_class_config @remote_class_configs = {} @fk = fk @fk_type = fk_type end
Public Instance Methods
add_remote_class_config(remote_class_configs)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 42 def add_remote_class_config(remote_class_configs) Array(remote_class_configs).each do |remote_class_config| @remote_class_configs[remote_class_config.domain_class.name] = remote_class_config end end
associate(local_object, remote_object)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 37 def associate(local_object, remote_object) local_end_config.associate(local_object, remote_object) if local_end_config && local_object remote_end_config.associate(remote_object, local_object) if remote_end_config && remote_object end
fk_value(local_db_object)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 33 def fk_value(local_db_object) local_db_object.send(fk) end
foreign_key_info(remote_class_config)
click to toggle source
@return ForeignKeyInfo
# File lib/vorpal/config/association_config.rb, line 67 def foreign_key_info(remote_class_config) ForeignKeyInfo.new(@fk, @fk_type, remote_class_config.domain_class.name, polymorphic?) end
polymorphic?()
click to toggle source
# File lib/vorpal/config/association_config.rb, line 57 def polymorphic? !@fk_type.nil? end
remote_class_config(local_db_object)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 48 def remote_class_config(local_db_object) if polymorphic? fk_type_value = local_db_object.send(@fk_type) @remote_class_configs[fk_type_value] else @remote_class_configs.values.first end end
set_foreign_key(local_db_object, remote_object)
click to toggle source
# File lib/vorpal/config/association_config.rb, line 61 def set_foreign_key(local_db_object, remote_object) local_class_config.set_attribute(local_db_object, @fk, remote_object&.send(unique_key_name)) local_class_config.set_attribute(local_db_object, @fk_type, remote_object.class.name) if polymorphic? end
unique_key_name()
click to toggle source
# File lib/vorpal/config/association_config.rb, line 71 def unique_key_name (@local_end_config || @remote_end_config).unique_key_name end
validate()
click to toggle source
# File lib/vorpal/config/association_config.rb, line 75 def validate if @local_end_config && @remote_end_config if @local_end_config.unique_key_name != @remote_end_config.unique_key_name raise ConfigurationError.new("#{@local_end_config.pretty_name} and #{@remote_end_config.pretty_name} must have the same unique_key_name/primary_key") end end end