Dynamically store relations on content classes

This commit is contained in:
Andrew Brown 2017-05-01 00:36:59 +01:00
parent 9bdf1d4cdb
commit efe33b445d

View File

@ -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