Sha256: 4425be6ac61f0f67043809bca4b9d4c104f5bdf548b9c45386d4967e8d8761ea

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

module FlexibleFeeds
  module ActsAsParent
    extend ActiveSupport::Concern
 
    module ClassMethods
      def acts_as_parent(options = {})

        cattr_accessor :permitted_children
        cattr_accessor :unpermitted_children
        self.permitted_children = options[:permitted_children]
        self.unpermitted_children = options[:unpermitted_children]

        def is_parental?
          true
        end

        send :include, InstanceMethods
      end
    end

    module InstanceMethods
      def is_parental?
        true
      end

      def children
        FlexibleFeeds::Event.where(parent: event).includes(:eventable)
      end

      def descendants
        FlexibleFeeds::Event.where(ancestor: event).includes(:eventable)
      end

      def parent_of(child)
        if can_accept_child?(child.eventable)
          ancestor = self.try(:ancestor) || self.event
          child.update_attributes(parent: self.event, ancestor: ancestor)
          child.increment_parent_counter
        end
      end

      private
      def can_accept_child?(child)
        if self.class.permitted_children.present?
          self.class.permitted_children.include?(child.class)
        elsif self.class.unpermitted_children.present?
          !self.class.unpermitted_children.include?(child.class)
        else
          child.try(:is_childish?)
        end
      end
    end
  end
end

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

Version data entries

6 entries across 6 versions & 1 rubygems

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