-
-
Save derekbrameyer/7328ff45a3db67595712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
field :name, :type => String | |
field :alternate_id, :type => String | |
field :birthday, :type => Date | |
embeds_many :relationships, :class_name => "Relationship" | |
after_destroy :destroy_relationships | |
def follow(user_to_follow) | |
# TODO: establish rel_type for relationships, | |
# add in friending ability | |
unless self.im_following?(user_to_follow) | |
if user_to_follow.im_following?(self) | |
# a relationship already exists between the two, | |
# so establish a friendship by updating it | |
puts("making a friendship") #DEBUG | |
r = relationships.all(:rel_user => user_to_follow).first | |
puts(r.rel_type) #DEBUG | |
r.rel_type = "friends with" | |
r.save | |
r2 = user_to_follow.relationships.all(:rel_user => self).first | |
puts(r2.rel_type) #DEBUG | |
r2.rel_type = "friends with" | |
r2.save | |
else | |
# a relationship does not exist between the two, | |
# so establish a one-way following relationship | |
# This is TWO relationships of different rel_type. | |
puts("making a one-way following") #DEBUG | |
r = Relationship.new(:rel_user => user_to_follow, :rel_type => "following") | |
self.relationships << r | |
r.save | |
r2 = Relationship.new(:rel_user => self, :rel_type => "followed_by") | |
user_to_follow.relationships << r2 | |
r2.save | |
end | |
end | |
end | |
def im_following?(user_in_question) | |
if self.relationships.all(:conditions => {:rel_user => user_in_question, :rel_type => "following" }).count == 1 | |
return true | |
else | |
return false | |
end | |
end | |
def followed_by?(user_in_question) | |
user_in_question.im_following?(self) | |
end | |
def friends_with?(user_in_question) | |
self.im_following?(user_in_question) and self.followed_by?(user_in_question) | |
end | |
protected | |
def destroy_relationships | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment