diaspora/lib/diaspora/relayable.rb
2016-06-26 06:21:01 +02:00

45 lines
1.2 KiB
Ruby

# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Diaspora
module Relayable
def self.included(model)
model.class_eval do
validates_associated :parent
validates :author, :presence => true
validate :author_is_not_ignored
delegate :public?, to: :parent
delegate :author, :diaspora_handle, to: :parent, prefix: true
after_commit :on => :create do
parent.touch(:interacted_at) if parent.respond_to?(:interacted_at)
end
end
end
def author_is_not_ignored
unless new_record? && parent.present? && parent.author.local? &&
parent.author.owner.ignored_people.include?(author)
return
end
errors.add(:author_id, "This relayable author is ignored by the post author")
end
# @return [Array<Person>]
def subscribers
if parent.author.local?
parent.subscribers
else
[parent.author]
end
end
# @abstract
def parent
raise NotImplementedError.new('you must override parent in order to enable relayable on this model')
end
end
end