Sha256: be78d1f1f25c62c099168cb3a01e68fce57b388969ed9d71bc1d0a79035c973c

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

module Plotline
  module Concerns
    module Family
      extend ActiveSupport::Concern

      included do
        belongs_to :parent, class_name: 'Plotline::Entry'
        has_many :children, class_name: 'Plotline::Entry', foreign_key: :parent_id
      end

      module ClassMethods
        def parent_entry(parent_entry_klass = nil)
          if parent_entry_klass
            @parent_entry ||= parent_entry_klass.to_s.classify
            alias_method parent_entry_klass, :parent
          end
          @parent_entry
        end

        def child_entries(child_entry_klass)
          @child_entry ||= child_entry_klass.to_s.singularize.classify
          alias_method child_entry_klass, :children
        end
      end

      def can_have_parent?
        self.class.parent_entry.present?
      end

      def possible_parents
        Plotline::Entry.where(type: self.class.parent_entry)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
plotline-0.1.1 app/models/plotline/concerns/family.rb
plotline-0.1.0 app/models/plotline/concerns/family.rb