Module: Diaspora::Shareable

Included in:
Photo, Post
Defined in:
lib/diaspora/shareable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object


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
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/diaspora/shareable.rb', line 8

def self.included(model)
  model.instance_eval do

    has_many :aspect_visibilities, :as => :shareable
    has_many :aspects, :through => :aspect_visibilities

    has_many :share_visibilities, :as => :shareable
    has_many :contacts, :through => :share_visibilities

    belongs_to :author, :class_name => 'Person'

    delegate :id, :name, :first_name, to: :author, prefix: true

    #scopes
    scope :all_public, where(:public => true, :pending => false)

    def self.owned_or_visible_by_user(user)
      self.joins("LEFT OUTER JOIN share_visibilities ON share_visibilities.shareable_id = posts.id AND share_visibilities.shareable_type = 'Post'").
           joins("LEFT OUTER JOIN contacts ON contacts.id = share_visibilities.contact_id").
           where(
              Contact.arel_table[:user_id].eq(user.id).or(
                self.arel_table[:public].eq(true).or(
                  self.arel_table[:author_id].eq(user.person_id)
               )
             )
           ).
           select("DISTINCT #{self.table_name}.*")
    end

    def self.for_visible_shareable_sql(max_time, order, limit = 15, types = Stream::Base::TYPES_OF_POST_IN_STREAM)
      by_max_time(max_time, order).
      where(:type => types).
      limit(limit)
    end

    def self.by_max_time(max_time, order='created_at')
      where("#{self.table_name}.#{order} < ?", max_time).order("#{self.table_name}.#{order} desc")
    end
  end
end

Instance Method Details

#update_reshares_counterInteger


50
51
52
53
# File 'lib/diaspora/shareable.rb', line 50

def update_reshares_counter
  self.class.where(:id => self.id).
    update_all(:reshares_count => self.reshares.count)
end