create module for author association

This commit is contained in:
Benjamin Neff 2016-06-17 22:04:30 +02:00
parent cf5a72719e
commit 67688a6022
9 changed files with 25 additions and 45 deletions

View file

@ -7,6 +7,7 @@ class Comment < ActiveRecord::Base
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Fields::Author
include Diaspora::Relayable
include Diaspora::Taggable
@ -19,10 +20,8 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, :touch => true, :polymorphic => true
alias_attribute :post, :commentable
alias_attribute :parent, :commentable
belongs_to :author, class_name: "Person"
delegate :name, to: :author, prefix: true
delegate :diaspora_handle, to: :author
delegate :comment_email_subject, to: :parent
delegate :author_name, to: :parent, prefix: true
@ -48,10 +47,6 @@ class Comment < ActiveRecord::Base
participation.unparticipate! if participation.present?
end
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
def message
@message ||= Diaspora::MessageRenderer.new text
end

View file

@ -1,14 +1,12 @@
class Conversation < ActiveRecord::Base
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Fields::Author
has_many :conversation_visibilities, dependent: :destroy
has_many :participants, class_name: "Person", through: :conversation_visibilities, source: :person
has_many :messages, -> { order("created_at ASC") }, inverse_of: :conversation
belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author
validate :max_participants
validate :local_recipients
@ -33,10 +31,6 @@ class Conversation < ActiveRecord::Base
self.participants - [self.author]
end
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
def first_unread_message(user)
if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first
self.messages.to_a[-visibility.unread]

View file

@ -1,25 +1,19 @@
class Message < ActiveRecord::Base
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Fields::Author
belongs_to :author, class_name: "Person"
belongs_to :conversation, touch: true
delegate :diaspora_handle, to: :author
delegate :name, to: :author, prefix: true
# TODO: can be removed when messages are not relayed anymore
alias_attribute :parent, :conversation
validates :conversation, presence: true
validates :author, presence: true
validates :text, presence: true
validate :participant_of_parent_conversation
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
def conversation_guid=(guid)
self.conversation_id = Conversation.where(guid: guid).ids.first
end

View file

@ -1,14 +1,11 @@
class PollParticipation < ActiveRecord::Base
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Fields::Author
include Diaspora::Relayable
belongs_to :poll
belongs_to :poll_answer, counter_cache: :vote_count
belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author
alias_attribute :parent, :poll
@ -19,10 +16,6 @@ class PollParticipation < ActiveRecord::Base
self.poll_answer_id = PollAnswer.where(guid: new_poll_answer_guid).ids.first
end
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
def not_already_participated
return if poll.nil?

View file

@ -30,8 +30,6 @@ class Post < ActiveRecord::Base
validates_uniqueness_of :id
validates :author, presence: true
after_create do
self.touch(:interacted_at)
end

View file

@ -0,0 +1,19 @@
module Diaspora
module Fields
module Author
def self.included(model)
model.class_eval do
belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author
validates :author, presence: true
end
end
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
end
end
end

View file

@ -7,7 +7,6 @@ module Diaspora
def self.included(model)
model.class_eval do
validates_associated :parent
validates :author, :presence => true
validate :author_is_not_ignored
delegate :public?, to: :parent

View file

@ -9,15 +9,13 @@ module Diaspora
def self.included(model)
model.instance_eval do
include Diaspora::Guid
include Diaspora::Fields::Author
has_many :aspect_visibilities, as: :shareable, validate: false, dependent: :delete_all
has_many :aspects, through: :aspect_visibilities
has_many :share_visibilities, as: :shareable, dependent: :delete_all
belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author
delegate :id, :name, :first_name, to: :author, prefix: true
# scopes
@ -42,10 +40,6 @@ module Diaspora
ShareVisibility.batch_import(recipient_user_ids, self)
end
def diaspora_handle=(author_handle)
self.author = Person.where(diaspora_handle: author_handle).first
end
# The list of people that should receive this Shareable.
#
# @return [Array<Person>] The list of subscribers to this shareable

View file

@ -4,21 +4,15 @@ module Federated
include Diaspora::Federated::Base
include Diaspora::Guid
include Diaspora::Fields::Author
include Diaspora::Relayable
belongs_to :target, polymorphic: true
belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author
alias_attribute :parent, :target
validates :target_id, uniqueness: {scope: %i(target_type author_id)}
validates :target, presence: true # should be in relayable (pending on fixing Message)
def diaspora_handle=(nh)
self.author = Person.find_or_fetch_by_identifier(nh)
end
end
end