Class: AdjustableSchema::Relationship

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

Defined Under Namespace

Classes: Role

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](**scopes) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'app/models/adjustable_schema/relationship.rb', line 74

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

.seed!(*models, roles: [], **_models) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/adjustable_schema/relationship.rb', line 84

def seed! *models, roles: [], **_models
	return seed!({ **Hash[*models], **_models }, roles:) if _models.any? # support keyword arguments syntax

	case models
	in [
			String | Symbol | Class => source_type,
			String | Symbol | Class => target_type,
	]
		roles
				.map { |name| Role.find_or_create_by! name: }
				.then { _1.presence or [ nil ] } # no roles => nameless relationship
				.map { |role| create! source_type:, target_type:, role: }
	in [ Hash => models ]
		for sources, targets in models do
			for source, target in Array(sources).product Array(targets) do
				seed! source, target, roles:
			end
		end
	in [ Class => source ]
		seed! source, source, roles: # recursive
	end
end

.with_rolesObject



68
69
70
71
# File 'app/models/adjustable_schema/relationship.rb', line 68

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

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


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

def abstract? = not (source or target)

#name=(role_name) ⇒ Object



110
111
112
113
# File 'app/models/adjustable_schema/relationship.rb', line 110

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