Sha256: af251ecc2178eed9f216c64186fa132cf1bc58e7f40649fed0839fa08b6834b4

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module FlexibleFeeds
  module ActsAsChild
    extend ActiveSupport::Concern
 
    module ClassMethods
      def acts_as_child(options = {})

        def is_childish?
          true
        end

        after_destroy :decrement_parent_counter

        send :include, InstanceMethods
      end
    end

    module InstanceMethods
      def is_childish?
        true
      end

      def parent
        event.parent
      end

      def ancestor
        event.ancestor
      end

      def child_of(parent)
        if can_accept_parent?(parent.eventable)
          ancestor = parent.try(:ancestor) || parent
          event.update_attributes(parent: parent, ancestor: ancestor)
          event.increment_parent_counter
        end
      end

      private
      def can_accept_parent?(parent)
        if parent.class.try(:permitted_children).present?
          parent.class.permitted_children.include?(self.class)
        elsif parent.class.try(:unpermitted_children).present?
          !parent.class.unpermitted_children.include?(self.class)
        else
          parent.try(:is_parental?)
        end
      end

      def initialize_event
        event || create_event
      end

      def decrement_parent_counter
        event.decrement_parent_counter
      end
    end
  end
end

ActiveRecord::Base.send :include, FlexibleFeeds::ActsAsChild

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flexible_feeds-0.3.1 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.0 lib/flexible_feeds/acts_as_child.rb