Commit a7557d54 authored by azul's avatar azul
Browse files

replace relationship observer with init_discussion

We now set the discussion for both relationships at the same time.
parent 2efbb1ae
......@@ -3,7 +3,7 @@
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :contact, class_name: 'User', foreign_key: :contact_id
belongs_to :discussion, dependent: :destroy
belongs_to :discussion, dependent: :destroy, inverse_of: :relationships
# mark as read or unread the discussion on this relationship
def mark!(as)
......@@ -22,12 +22,18 @@ class Relationship < ActiveRecord::Base
self.update_attribute(:unread_count, new_unread_count) if new_unread_count
end
def get_or_create_discussion
unless discussion
self.create_discussion
self.save
def discussion
super || init_discussion
end
def init_discussion
create_discussion.tap do |discuss|
inverse.update_attribute :discussion, discuss
save
end
discussion
end
def inverse
@inverse ||= Relationship.where(user_id: contact, contact_id: user).first
end
end
......@@ -35,7 +35,7 @@ class Discussion < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
# if we are a private discussion (or 'messages')
has_many :relationships do
has_many :relationships, inverse_of: :discussion do
def for_user(user)
self.detect {|relationship| relationship.user_id == user.id}
end
......
class RelationshipObserver < ActiveRecord::Observer
def before_create(relationship)
# Relationship's are created in pairs
mirror_twin = Relationship.find_by_user_id_and_contact_id(relationship.contact_id, relationship.user_id)
# the first one will have no mirror_twin yet and will build a new Discussion
# then that Relationship will get saved
# the second Relationship int the pair will copy the discussion from the first one
if mirror_twin and mirror_twin.discussion
relationship.discussion_id = mirror_twin.discussion.id
else
relationship.create_discussion
end
end
end
......@@ -167,7 +167,7 @@ module UserExtension::Users
#
def send_message_to!(other_user, body, in_reply_to = nil)
relationship = self.relationships.with(other_user) || self.add_contact!(other_user)
discussion = relationship.get_or_create_discussion
discussion = relationship.discussion
if in_reply_to
if in_reply_to.user_id == self.id
......
......@@ -48,7 +48,7 @@ module Crabgrass
# We use strong parameters instead like rails4 does.
#config.active_record.whitelist_attributes = true
config.active_record.observers = :relationship_observer,
config.active_record.observers =
"tracking/page_observer", "tracking/post_observer", "tracking/wiki_observer",
"tracking/user_participation_observer", "tracking/group_participation_observer"
......
require 'test_helper'
class MessageTest < ActiveSupport::TestCase
def setup
@sender = FactoryGirl.create :user
@recipient = FactoryGirl.create :user
end
def test_initial_message
post = @sender.send_message_to! @recipient, 'blablabla'
assert send_rel = @sender.relationships.with(@recipient)
assert discussion = send_rel.discussion
assert_equal 1, discussion.posts_count
assert discussion.unread_by?(@recipient)
assert !discussion.unread_by?(@sender)
end
def test_reply_to_same_discussion
post = @sender.send_message_to! @recipient, 'blablabla'
send_rel = @sender.relationships.with(@recipient)
@recipient.send_message_to! @sender, 'blablabla', post
assert recieve_rel = @recipient.relationships.with(@sender)
assert discussion = recieve_rel.discussion
assert_equal send_rel.discussion, discussion
end
def test_reply_unread_count
post = @sender.send_message_to! @recipient, 'blablabla'
@recipient.send_message_to! @sender, 'blablabla', post
recieve_rel = @recipient.relationships.with(@sender)
discussion = recieve_rel.discussion
assert_equal 2, discussion.posts_count
assert discussion.unread_by?(@recipient)
assert discussion.unread_by?(@sender)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment