Sha256: 6202c052ea991806efe49ade68670a4db3e7fdf8b7f9cab7efe64f10de5c94dd

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 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 decrement_parent_counter
        event.decrement_parent_counter
      end
    end
  end
end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
flexible_feeds-0.4.0 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.6 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.5 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.4 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.3 lib/flexible_feeds/acts_as_child.rb
flexible_feeds-0.3.2 lib/flexible_feeds/acts_as_child.rb