create module for author association
This commit is contained in:
parent
cf5a72719e
commit
67688a6022
9 changed files with 25 additions and 45 deletions
|
|
@ -7,6 +7,7 @@ class Comment < ActiveRecord::Base
|
||||||
include Diaspora::Federated::Base
|
include Diaspora::Federated::Base
|
||||||
|
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
include Diaspora::Relayable
|
include Diaspora::Relayable
|
||||||
|
|
||||||
include Diaspora::Taggable
|
include Diaspora::Taggable
|
||||||
|
|
@ -19,10 +20,8 @@ class Comment < ActiveRecord::Base
|
||||||
belongs_to :commentable, :touch => true, :polymorphic => true
|
belongs_to :commentable, :touch => true, :polymorphic => true
|
||||||
alias_attribute :post, :commentable
|
alias_attribute :post, :commentable
|
||||||
alias_attribute :parent, :commentable
|
alias_attribute :parent, :commentable
|
||||||
belongs_to :author, class_name: "Person"
|
|
||||||
|
|
||||||
delegate :name, to: :author, prefix: true
|
delegate :name, to: :author, prefix: true
|
||||||
delegate :diaspora_handle, to: :author
|
|
||||||
delegate :comment_email_subject, to: :parent
|
delegate :comment_email_subject, to: :parent
|
||||||
delegate :author_name, to: :parent, prefix: true
|
delegate :author_name, to: :parent, prefix: true
|
||||||
|
|
||||||
|
|
@ -48,10 +47,6 @@ class Comment < ActiveRecord::Base
|
||||||
participation.unparticipate! if participation.present?
|
participation.unparticipate! if participation.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def diaspora_handle=(nh)
|
|
||||||
self.author = Person.find_or_fetch_by_identifier(nh)
|
|
||||||
end
|
|
||||||
|
|
||||||
def message
|
def message
|
||||||
@message ||= Diaspora::MessageRenderer.new text
|
@message ||= Diaspora::MessageRenderer.new text
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
class Conversation < ActiveRecord::Base
|
class Conversation < ActiveRecord::Base
|
||||||
include Diaspora::Federated::Base
|
include Diaspora::Federated::Base
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
|
|
||||||
has_many :conversation_visibilities, dependent: :destroy
|
has_many :conversation_visibilities, dependent: :destroy
|
||||||
has_many :participants, class_name: "Person", through: :conversation_visibilities, source: :person
|
has_many :participants, class_name: "Person", through: :conversation_visibilities, source: :person
|
||||||
has_many :messages, -> { order("created_at ASC") }, inverse_of: :conversation
|
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 :max_participants
|
||||||
validate :local_recipients
|
validate :local_recipients
|
||||||
|
|
||||||
|
|
@ -33,10 +31,6 @@ class Conversation < ActiveRecord::Base
|
||||||
self.participants - [self.author]
|
self.participants - [self.author]
|
||||||
end
|
end
|
||||||
|
|
||||||
def diaspora_handle=(nh)
|
|
||||||
self.author = Person.find_or_fetch_by_identifier(nh)
|
|
||||||
end
|
|
||||||
|
|
||||||
def first_unread_message(user)
|
def first_unread_message(user)
|
||||||
if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first
|
if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first
|
||||||
self.messages.to_a[-visibility.unread]
|
self.messages.to_a[-visibility.unread]
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,19 @@
|
||||||
class Message < ActiveRecord::Base
|
class Message < ActiveRecord::Base
|
||||||
include Diaspora::Federated::Base
|
include Diaspora::Federated::Base
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
|
|
||||||
belongs_to :author, class_name: "Person"
|
|
||||||
belongs_to :conversation, touch: true
|
belongs_to :conversation, touch: true
|
||||||
|
|
||||||
delegate :diaspora_handle, to: :author
|
|
||||||
delegate :name, to: :author, prefix: true
|
delegate :name, to: :author, prefix: true
|
||||||
|
|
||||||
# TODO: can be removed when messages are not relayed anymore
|
# TODO: can be removed when messages are not relayed anymore
|
||||||
alias_attribute :parent, :conversation
|
alias_attribute :parent, :conversation
|
||||||
|
|
||||||
validates :conversation, presence: true
|
validates :conversation, presence: true
|
||||||
validates :author, presence: true
|
|
||||||
validates :text, presence: true
|
validates :text, presence: true
|
||||||
validate :participant_of_parent_conversation
|
validate :participant_of_parent_conversation
|
||||||
|
|
||||||
def diaspora_handle=(nh)
|
|
||||||
self.author = Person.find_or_fetch_by_identifier(nh)
|
|
||||||
end
|
|
||||||
|
|
||||||
def conversation_guid=(guid)
|
def conversation_guid=(guid)
|
||||||
self.conversation_id = Conversation.where(guid: guid).ids.first
|
self.conversation_id = Conversation.where(guid: guid).ids.first
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,11 @@
|
||||||
class PollParticipation < ActiveRecord::Base
|
class PollParticipation < ActiveRecord::Base
|
||||||
include Diaspora::Federated::Base
|
include Diaspora::Federated::Base
|
||||||
|
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
include Diaspora::Relayable
|
include Diaspora::Relayable
|
||||||
|
|
||||||
belongs_to :poll
|
belongs_to :poll
|
||||||
belongs_to :poll_answer, counter_cache: :vote_count
|
belongs_to :poll_answer, counter_cache: :vote_count
|
||||||
belongs_to :author, class_name: "Person"
|
|
||||||
|
|
||||||
delegate :diaspora_handle, to: :author
|
|
||||||
|
|
||||||
alias_attribute :parent, :poll
|
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
|
self.poll_answer_id = PollAnswer.where(guid: new_poll_answer_guid).ids.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def diaspora_handle=(nh)
|
|
||||||
self.author = Person.find_or_fetch_by_identifier(nh)
|
|
||||||
end
|
|
||||||
|
|
||||||
def not_already_participated
|
def not_already_participated
|
||||||
return if poll.nil?
|
return if poll.nil?
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,6 @@ class Post < ActiveRecord::Base
|
||||||
|
|
||||||
validates_uniqueness_of :id
|
validates_uniqueness_of :id
|
||||||
|
|
||||||
validates :author, presence: true
|
|
||||||
|
|
||||||
after_create do
|
after_create do
|
||||||
self.touch(:interacted_at)
|
self.touch(:interacted_at)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
19
lib/diaspora/fields/author.rb
Normal file
19
lib/diaspora/fields/author.rb
Normal 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
|
||||||
|
|
@ -7,7 +7,6 @@ module Diaspora
|
||||||
def self.included(model)
|
def self.included(model)
|
||||||
model.class_eval do
|
model.class_eval do
|
||||||
validates_associated :parent
|
validates_associated :parent
|
||||||
validates :author, :presence => true
|
|
||||||
validate :author_is_not_ignored
|
validate :author_is_not_ignored
|
||||||
|
|
||||||
delegate :public?, to: :parent
|
delegate :public?, to: :parent
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,13 @@ module Diaspora
|
||||||
def self.included(model)
|
def self.included(model)
|
||||||
model.instance_eval do
|
model.instance_eval do
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
|
|
||||||
has_many :aspect_visibilities, as: :shareable, validate: false, dependent: :delete_all
|
has_many :aspect_visibilities, as: :shareable, validate: false, dependent: :delete_all
|
||||||
has_many :aspects, through: :aspect_visibilities
|
has_many :aspects, through: :aspect_visibilities
|
||||||
|
|
||||||
has_many :share_visibilities, as: :shareable, dependent: :delete_all
|
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
|
delegate :id, :name, :first_name, to: :author, prefix: true
|
||||||
|
|
||||||
# scopes
|
# scopes
|
||||||
|
|
@ -42,10 +40,6 @@ module Diaspora
|
||||||
ShareVisibility.batch_import(recipient_user_ids, self)
|
ShareVisibility.batch_import(recipient_user_ids, self)
|
||||||
end
|
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.
|
# The list of people that should receive this Shareable.
|
||||||
#
|
#
|
||||||
# @return [Array<Person>] The list of subscribers to this shareable
|
# @return [Array<Person>] The list of subscribers to this shareable
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,15 @@ module Federated
|
||||||
|
|
||||||
include Diaspora::Federated::Base
|
include Diaspora::Federated::Base
|
||||||
include Diaspora::Guid
|
include Diaspora::Guid
|
||||||
|
include Diaspora::Fields::Author
|
||||||
|
|
||||||
include Diaspora::Relayable
|
include Diaspora::Relayable
|
||||||
|
|
||||||
belongs_to :target, polymorphic: true
|
belongs_to :target, polymorphic: true
|
||||||
belongs_to :author, class_name: "Person"
|
|
||||||
|
|
||||||
delegate :diaspora_handle, to: :author
|
|
||||||
|
|
||||||
alias_attribute :parent, :target
|
alias_attribute :parent, :target
|
||||||
|
|
||||||
validates :target_id, uniqueness: {scope: %i(target_type author_id)}
|
validates :target_id, uniqueness: {scope: %i(target_type author_id)}
|
||||||
validates :target, presence: true # should be in relayable (pending on fixing Message)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue