mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
Dynamically store relations on content classes
This commit is contained in:
parent
9bdf1d4cdb
commit
efe33b445d
@ -4,6 +4,8 @@ module HasContentGroupers
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
@@relations = {}
|
||||
|
||||
# Example:
|
||||
# relates :siblings, with: :siblingships, where: { alive: true }, type: :two_way
|
||||
# Defines :siblings and :siblingships relations, their inverses, and accepts_nested_attributes for siblingships
|
||||
@ -12,6 +14,13 @@ module HasContentGroupers
|
||||
connecting_class = with.to_s.singularize.camelize.constantize
|
||||
connecting_class_name = with
|
||||
|
||||
# Store all added relations on the class model, so we can dynamically fetch them all whenever needed.
|
||||
# e.g. Character.class_variable_get(:@@relations) => [:mothers, :fathers, :siblings, ...]
|
||||
@@relations[relation] = { # @@relations[:siblings] = {
|
||||
with: with, # with: :siblingships,
|
||||
related_class: connecting_class # related_class: Siblingship
|
||||
} # }
|
||||
|
||||
# Siblingships
|
||||
has_many connecting_class_name
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user