Sha256: fef2f943f16b493a1eb53e97de7dc9394ff0894526c7abcb6099d1310a7183c0

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module GroupedPage

  def self.included(base)
    base.class_eval {
      has_groups
      has_one :homegroup, :foreign_key => 'homepage_id', :class_name => 'Group'
      include InstanceMethods
      alias_method_chain :permitted_groups, :inheritance
    }
  end
  
  module InstanceMethods
    
    # this is all very inefficient recursive stuff
    # but the ancestor pages should be in memory already
    # and the groups check is now a single query

    def inherited_groups
      lineage = self.ancestors
      if lineage.any?
        Group.attached_to(lineage)
      else
        []
      end
    end

    def permitted_groups_with_inheritance
      permitted_groups_without_inheritance + inherited_groups
    end

    # any page with a group-marker is never cached
    # so that we can return cache hits with confidence
    # this call is regrettably expensive
    def cache?
      self.permitted_groups.empty?
    end        

    def has_inherited_group?(group)
      return self.inherited_groups.include?(group)
    end
    
    def group_is_inherited?(group)
      return self.has_inherited_group?(group) && !self.has_group?(group)
    end
    
  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radiant-reader_group-extension-1.2.0 lib/grouped_page.rb