Skip to content

Commit

Permalink
Reorganization of ties
Browse files Browse the repository at this point in the history
  • Loading branch information
atd committed Sep 23, 2010
1 parent a4b3443 commit c2dfdfa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
24 changes: 16 additions & 8 deletions app/models/activity.rb
@@ -1,4 +1,20 @@
# Activities follow the {Activity Streams}[http://activitystrea.ms/] standard.
#
# == Activities and Ties
# Every activity is attached to a Tie, which defines the sender, the receiver and
# the relation in which the activity is transferred
#
# == Wall
# The Activity.wall(ties) scope provides all the activities attached to a set of ties
#
class Activity < ActiveRecord::Base
scope :wall, lambda { |ties|
select("DISTINCT activities.*").
roots.
where(:tie_id => ties).
order("created_at desc")
}

has_ancestry

belongs_to :activity_verb
Expand Down Expand Up @@ -44,12 +60,4 @@ def liked_by?(user)
liked_by(user).present?
end

class << self
def wall(ties_query)
select( "DISTINCT activities.*").
roots.
where("activities.tie_id IN (#{ ties_query })").
order("created_at desc")
end
end
end
14 changes: 12 additions & 2 deletions app/models/actor.rb
Expand Up @@ -2,14 +2,24 @@
class Actor < ActiveRecord::Base
include SocialStream::Models::Supertype

has_many :ties,
has_many :sent_ties,
:class_name => "Tie",
:foreign_key => 'sender_id',
:include => [ :receiver, :relation ],
:dependent => :destroy

has_many :received_ties,
:class_name => "Tie",
:foreign_key => 'receiver_id',
:dependent => :destroy

# The subject instance for this actor
def subject
subtype_instance ||
activity_object.try(:object)
end

# All the ties sent or received by this actor
def ties
Tie.sent_or_received_by(self)
end
end
27 changes: 14 additions & 13 deletions app/models/tie.rb
Expand Up @@ -14,6 +14,14 @@
# == Inverse ties
# Relations can have its inverse. When a tie is establised, an inverse tie is establised
# as well.
#
# == Scopes
# There are several scopes defined:
# * sent_by(actor), ties whose sender is actor
# * received_by(actor), ties whose receiver is actor
# * sent_or_received_by(actor), the union of the former
# * inverse(tie), the inverse of tie
#
class Tie < ActiveRecord::Base
# Avoids loops at create_inverse after save callback
attr_accessor :_without_inverse
Expand Down Expand Up @@ -42,6 +50,12 @@ class Tie < ActiveRecord::Base
where(:receiver_id => Actor_id(a))
}

scope :sent_or_received_by, lambda { |a|
where(arel_table[:sender_id].eq(Actor_id(a)).
or(arel_table[:receiver_id].eq(Actor_id(a))))

}

scope :inverse, lambda { |t|
sent_by(t.receiver).
received_by(t.sender).
Expand Down Expand Up @@ -187,18 +201,5 @@ def Actor_id(a)
a.actor.id
end
end

def tie_ids_query(actor)
c = arel_table
d = c.alias

c.join(d).
on(d[:sender_id].eq(actor.id).
and(c[:receiver_id].eq(d[:receiver_id])).
and((c[:relation_id].eq(d[:relation_id]).or(c[:relation_id].eq(0))))).
project(c[:id]).
to_sql
"SELECT ties.id FROM ties INNER JOIN ties ties_2 ON ((ties_2.sender_id = #{ actor.id } AND ties_2.receiver_id = ties.receiver_id) AND (ties_2.relation_id = ties.relation_id OR ties.relation_id = #{ Relation.mode('User', 'User').find_by_name('Public').id }))"
end
end
end
2 changes: 1 addition & 1 deletion lib/social_stream/models/actor.rb
Expand Up @@ -15,7 +15,7 @@ module Actor
:email, :email=,
:permalink, :permalink=,
:disabled, :disabled=,
:ties,
:ties, :sent_ties, :received_ties,
:to => :actor!
end

Expand Down

0 comments on commit c2dfdfa

Please sign in to comment.