mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
ContentGroupers concern
This commit is contained in:
parent
ffdb9c7b26
commit
c39a644aff
@ -14,27 +14,11 @@ class Character < ActiveRecord::Base
|
||||
|
||||
belongs_to :universe
|
||||
|
||||
# TODO: Rip all this out into a HasSiblings concern (or better yet, generalize it into a HasRelationship concern)
|
||||
has_many :siblingships
|
||||
has_many :siblings, through: :siblingships
|
||||
accepts_nested_attributes_for :siblingships, reject_if: :all_blank, allow_destroy: true
|
||||
has_many :inverse_siblingships, class_name: 'Siblingship', foreign_key: 'sibling_id'
|
||||
has_many :inverse_siblings, through: :inverse_siblingships, source: :character
|
||||
include HasContentGroupers
|
||||
|
||||
# Fathership
|
||||
has_many :fatherships
|
||||
has_many :fathers, through: :fatherships
|
||||
accepts_nested_attributes_for :fatherships, reject_if: :all_blank, allow_destroy: true
|
||||
has_many :inverse_fatherships, class_name: 'Fathership', foreign_key: 'father_id'
|
||||
has_many :inverse_fathers, through: :inverse_fatherships, source: :character
|
||||
|
||||
# TODO: design DSL in concern to condense these 5-line blocks into 1
|
||||
# Mothership
|
||||
has_many :motherships
|
||||
has_many :mothers, through: :motherships
|
||||
accepts_nested_attributes_for :motherships, reject_if: :all_blank, allow_destroy: true
|
||||
has_many :inverse_motherships, class_name: 'Mothership', foreign_key: 'mother_id'
|
||||
has_many :inverse_mothers, through: :inverse_motherships, source: :character
|
||||
relates :siblings, with: :siblingships
|
||||
relates :fathers, with: :fatherships
|
||||
relates :mothers, with: :motherships
|
||||
|
||||
|
||||
|
||||
|
||||
20
app/models/concerns/has_content_groupers.rb
Normal file
20
app/models/concerns/has_content_groupers.rb
Normal file
@ -0,0 +1,20 @@
|
||||
require 'active_support/concern'
|
||||
|
||||
module HasContentGroupers
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# relates :siblings, with: :siblingships
|
||||
# Defines :siblings and :siblingships relations, their inverses, and accepts nested attributes for the connecting class
|
||||
def self.relates relation, with: nil
|
||||
singularized_relation = relation.to_s.singularize
|
||||
connecting_class = with
|
||||
|
||||
has_many connecting_class
|
||||
has_many relation, through: connecting_class
|
||||
accepts_nested_attributes_for connecting_class, reject_if: :all_blank, allow_destroy: true
|
||||
has_many "inverse_#{connecting_class}".to_sym, class_name: "#{singularized_relation.capitalize}", foreign_key: "#{singularized_relation}_id"
|
||||
has_many "inverse_#{relation}".to_sym, through: "inverse_#{connecting_class}".to_sym, source: :character
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,2 +0,0 @@
|
||||
class Mothership < ActiveRecord::Base
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user