Class: AdjustableSchema::Relationship

Inherits:
ApplicationRecord show all
Defined in:
app/models/adjustable_schema/relationship.rb,
app/models/adjustable_schema/relationship/role.rb

Overview

Relationships

‘Relationship` is a core class of Adjustable Schema representing both associations of model classes and connections of individual records.

No constraints are supported yet, so only many-to-many associations are available for now.

Model associations

To represent an association, a ‘Relationship` record should have both `source_type` and `target_type` columns set, with both ID columns set to `NULL`.

Record relationships

To connect individual records, both ‘source` and `target` polymorphic associations shoud be set.

Roles

Many associations with different semantics between the same models can be set using roles (see ‘Relationship::Role`).

Defined Under Namespace

Classes: Role

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](**scopes) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'app/models/adjustable_schema/relationship.rb', line 92

def [] **scopes
	scopes
			.map do
				self # rubocop:disable Style
						.send(Config.shortcuts[:source], _1)
						.send(Config.shortcuts[:target], _2)
			end
			.reduce &:or
end

.seed!Object



102
103
104
105
106
107
108
109
# File 'app/models/adjustable_schema/relationship.rb', line 102

def seed!(...)
	AdjustableSchema.deprecator.warn <<~TEXT.squish
		#{self}.#{__method__} is deprecated and will be removed in v0.12.
		Please use AdjustableSchema.relationship! instead.
	TEXT

	AdjustableSchema.relationship!(...)
end

.with_rolesObject



86
87
88
89
# File 'app/models/adjustable_schema/relationship.rb', line 86

def self.with_roles(&)
	joins(:role)
			.merge Role.instance_eval(&)
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


119
# File 'app/models/adjustable_schema/relationship.rb', line 119

def abstract? = not (source or target)

#inspectObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/adjustable_schema/relationship.rb', line 121

def inspect
	<<~TEXT.chomp.gsub(/-+/, '-')
		#<#{self.class}##{id}: #{
				source_type
		}#{
				source_id&.then { "##{it}" }
		}>-#{name}->#{
				target_type unless target_type == source_type and target_id
		}#{
				target_id&.then { "##{it}" }
		}>
	TEXT
end

#name=(role_name) ⇒ Object



114
115
116
117
# File 'app/models/adjustable_schema/relationship.rb', line 114

def name= role_name
	self.role =
			role_name && Role[role_name]
end