DG MS; put validates_associated in relayable, removed from receiver logic

This commit is contained in:
Maxwell Salzberg 2011-09-12 15:06:17 -07:00
parent 5177e5b218
commit 0ab0526cd6
8 changed files with 20 additions and 26 deletions

View file

@ -27,7 +27,7 @@ class Comment < ActiveRecord::Base
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
validates :text, :presence => true, :length => { :maximum => 2500 } validates :text, :presence => true, :length => { :maximum => 2500 }
validates :post, :presence => true validates :parent, :presence => true #should be in relayable (pending on fixing Message)
serialize :youtube_titles, Hash serialize :youtube_titles, Hash
@ -56,23 +56,15 @@ class Comment < ActiveRecord::Base
end end
def notification_type(user, person) def notification_type(user, person)
if user.owns?(self.post) if self.post.author == user.person
return Notifications::CommentOnPost return Notifications::CommentOnPost
elsif user_has_commented_on_others_post?(person, self.post, user) elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id
return Notifications::AlsoCommented return Notifications::AlsoCommented
else else
return false return false
end end
end end
def user_has_commented_on_others_post?(author, post, user)
Comment.comments_by_author_on_post_exist?(author, post.id) && self.author_id != user.person.id
end
def self.comments_by_author_on_post_exist?(author, post_id)
Comment.exists?(:author_id => author.id, :post_id => post_id)
end
def parent_class def parent_class
Post Post
end end

View file

@ -20,8 +20,7 @@ class Like < ActiveRecord::Base
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
validates_uniqueness_of :target_id, :scope => [:target_type, :author_id] validates_uniqueness_of :target_id, :scope => [:target_type, :author_id]
validates :author, :presence => true validates :parent, :presence => true #should be in relayable (pending on fixing Message)
validates :target, :presence => true
after_create do after_create do
self.parent.update_likes_counter self.parent.update_likes_counter

View file

@ -15,21 +15,20 @@ class Message < ActiveRecord::Base
belongs_to :conversation, :touch => true belongs_to :conversation, :touch => true
validates :text, :presence => true validates :text, :presence => true
validate :participant_of_parent_conversation
after_create do after_create do
#sign comment as commenter #sign comment as commenter
self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner
if !self.parent.blank? && self.author.owns?(self.parent) if self.author.owns?(self.parent)
#sign comment as post owner #sign comment as post owner
self.parent_author_signature = self.sign_with_key( self.parent.author.owner.encryption_key) if self.parent.author.owner self.parent_author_signature = self.sign_with_key(self.parent.author.owner.encryption_key) if self.parent.author.owner
end end
self.save! self.save!
self self
end end
validate :participant_of_parent_conversation
def diaspora_handle def diaspora_handle
self.author.diaspora_handle self.author.diaspora_handle
end end

View file

@ -158,7 +158,7 @@ class Person < ActiveRecord::Base
end end
def owns?(obj) def owns?(obj)
self == obj.author self.id == obj.author_id
end end
def url def url

View file

@ -12,6 +12,9 @@ module Diaspora
xml_attr :parent_guid xml_attr :parent_guid
xml_attr :parent_author_signature xml_attr :parent_author_signature
xml_attr :author_signature xml_attr :author_signature
validates_associated :parent
validates :author, :presence => true
end end
end end

View file

@ -30,10 +30,6 @@ module Postzord
end end
def receive_relayable def receive_relayable
# unless @object.parent.present?
# raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!")
# end
if @object.parent.author.local? if @object.parent.author.local?
# receive relayable object only for the owner of the parent object # receive relayable object only for the owner of the parent object
@object.receive(@object.parent.author.user, @author) @object.receive(@object.parent.author.user, @author)
@ -50,7 +46,6 @@ module Postzord
@object.save! @object.save!
end end
# @return [Array<Integer>] User ids # @return [Array<Integer>] User ids
def recipient_user_ids def recipient_user_ids
User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id } User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id }

View file

@ -17,7 +17,7 @@ describe Message do
:messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] :messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ]
} }
@cnv = Conversation.create(@create_hash) @cnv = Conversation.create!(@create_hash)
@message = @cnv.messages.first @message = @cnv.messages.first
@xml = @message.to_diaspora_xml @xml = @message.to_diaspora_xml
end end

View file

@ -6,6 +6,12 @@ require 'spec_helper'
describe Diaspora::Relayable do describe Diaspora::Relayable do
shared_examples_for "it is relayable" do shared_examples_for "it is relayable" do
context 'validation' do
it 'ensures an valid associated parent'
it 'ensures the presence of an author'
end
context 'encryption' do context 'encryption' do
describe '#parent_author_signature' do describe '#parent_author_signature' do
it 'should sign the object if the user is the post author' do it 'should sign the object if the user is the post author' do