From c39a644affdb1fcd499c5ec762a52e5db3b76b8a Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 3 May 2016 14:23:28 -0500 Subject: [PATCH] ContentGroupers concern --- app/models/character.rb | 24 ++++----------------- app/models/concerns/.keep | 0 app/models/concerns/has_content_groupers.rb | 20 +++++++++++++++++ app/models/mothership.rb | 2 -- 4 files changed, 24 insertions(+), 22 deletions(-) delete mode 100644 app/models/concerns/.keep create mode 100644 app/models/concerns/has_content_groupers.rb delete mode 100644 app/models/mothership.rb diff --git a/app/models/character.rb b/app/models/character.rb index 18113d0e..7d4afe56 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -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 diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/app/models/concerns/has_content_groupers.rb b/app/models/concerns/has_content_groupers.rb new file mode 100644 index 00000000..73774da2 --- /dev/null +++ b/app/models/concerns/has_content_groupers.rb @@ -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 \ No newline at end of file diff --git a/app/models/mothership.rb b/app/models/mothership.rb deleted file mode 100644 index a4e346ec..00000000 --- a/app/models/mothership.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Mothership < ActiveRecord::Base -end