diff --git a/app/models/comment.rb b/app/models/comment.rb index 112aa4fe5..cc0113ea2 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 873cb7f58..e22aab040 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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] diff --git a/app/models/message.rb b/app/models/message.rb index 8138b433e..c4e5ccf0b 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/models/poll_participation.rb b/app/models/poll_participation.rb index a81daa5e9..7a2c0e360 100644 --- a/app/models/poll_participation.rb +++ b/app/models/poll_participation.rb @@ -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? diff --git a/app/models/post.rb b/app/models/post.rb index 6eda1fbe0..fe4ddf6cb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -30,8 +30,6 @@ class Post < ActiveRecord::Base validates_uniqueness_of :id - validates :author, presence: true - after_create do self.touch(:interacted_at) end diff --git a/lib/diaspora/fields/author.rb b/lib/diaspora/fields/author.rb new file mode 100644 index 000000000..eaabbf27f --- /dev/null +++ b/lib/diaspora/fields/author.rb @@ -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 diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index 49fe2be94..247fd827b 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -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 diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 84193951a..5adee7053 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -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] The list of subscribers to this shareable diff --git a/lib/federated/relayable.rb b/lib/federated/relayable.rb index 72c82043f..e3973185e 100644 --- a/lib/federated/relayable.rb +++ b/lib/federated/relayable.rb @@ -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