Module: FeedableExtensions

Defined in:
lib/joinable/feedable_extensions.rb

Class Method Summary collapse

Class Method Details

.addObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/joinable/feedable_extensions.rb', line 2

def self.add
  Feed.class_eval do
    # Filter feeds about public joinables that you haven't joined, unless the feed is actually about you
    scope :not_from_unjoined, lambda {|joinable_type, user| 
      where("feeds.scoping_object_type IS NULL OR
             feeds.scoping_object_type != '#{joinable_type}' OR
             (feeds.feedable_type = 'User' AND feeds.feedable_id = #{user.id}) OR
             EXISTS (SELECT * FROM memberships WHERE memberships.joinable_type = '#{joinable_type}' AND memberships.joinable_id = feeds.scoping_object_id AND memberships.user_id = ?)", user.id)
    }
    # Filter feeds about public things where the given action took place
    scope :not_unscoped, lambda {|action| where('NOT (feeds.scoping_object_type IS NULL AND feeds.action = ?)', action) }

    acts_as_joinable_component :parent => 'permission_inheritance_target', :polymorphic => true, :view_permission => lambda {|feed| :find if feed.feedable.acts_like?(:joinable) }
    
    # The scoping_object becomes the parent if the feed is delegated to a non-permissable or the feedable is deleted
    # eg. a user (non-permissible) leaves a project, the parent of the feed is the project since a user isn't a permissable
    # eg. a writeboard is destroyed, the parent of the feed is now the project
    def permission_inheritance_target_type
      if feedable.acts_like?(:permissable)
        feedable_type
      else
        scoping_object_type
      end
    end
    
    def permission_inheritance_target_id
      if feedable.acts_like?(:permissable)
        feedable_id
      else
        scoping_object_id
      end
    end
  end
end