From d0b634852b440a8c3deac9734270d8577ef5a06b Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 5 Aug 2017 21:29:51 +0200 Subject: [PATCH] Fix belongs_to, it is now `required` by default --- .../api/openid_connect/authorization.rb | 3 +- .../api/openid_connect/o_auth_access_token.rb | 1 - .../pairwise_pseudonymous_identifier.rb | 3 +- app/models/aspect_visibility.rb | 2 -- app/models/block.rb | 3 +- app/models/comment.rb | 1 - app/models/comment_signature.rb | 1 - app/models/contact.rb | 3 -- app/models/invitation_code.rb | 2 -- app/models/like_signature.rb | 1 - app/models/mention.rb | 2 -- app/models/message.rb | 1 - app/models/person.rb | 4 +-- app/models/photo.rb | 2 +- app/models/poll_participation.rb | 1 - app/models/poll_participation_signature.rb | 1 - app/models/post.rb | 4 +-- app/models/report.rb | 4 +-- app/models/reshare.rb | 2 +- app/models/role.rb | 1 - app/models/user.rb | 4 +-- lib/diaspora/federation/receive.rb | 3 +- lib/diaspora/fields/target.rb | 1 - lib/diaspora/relayable.rb | 1 + spec/models/contact_spec.rb | 4 +-- spec/models/location_spec.rb | 14 +++++---- spec/models/poll_participation_spec.rb | 18 +++++------ spec/models/poll_spec.rb | 31 +++++++++---------- spec/models/role_spec.rb | 7 ++++- 29 files changed, 56 insertions(+), 69 deletions(-) diff --git a/app/models/api/openid_connect/authorization.rb b/app/models/api/openid_connect/authorization.rb index 00a373cad..a57231552 100644 --- a/app/models/api/openid_connect/authorization.rb +++ b/app/models/api/openid_connect/authorization.rb @@ -6,8 +6,7 @@ module Api belongs_to :user belongs_to :o_auth_application - validates :user, presence: true, uniqueness: {scope: :o_auth_application} - validates :o_auth_application, presence: true + validates :user, uniqueness: {scope: :o_auth_application} validate :validate_scope_names serialize :scopes, JSON diff --git a/app/models/api/openid_connect/o_auth_access_token.rb b/app/models/api/openid_connect/o_auth_access_token.rb index 176b4d04e..e2089fff8 100644 --- a/app/models/api/openid_connect/o_auth_access_token.rb +++ b/app/models/api/openid_connect/o_auth_access_token.rb @@ -29,7 +29,6 @@ module Api before_validation :setup, on: :create validates :token, presence: true, uniqueness: true - validates :authorization, presence: true scope :valid, ->(time) { where("expires_at >= ?", time) } diff --git a/app/models/api/openid_connect/pairwise_pseudonymous_identifier.rb b/app/models/api/openid_connect/pairwise_pseudonymous_identifier.rb index 0b5643125..520bc782c 100644 --- a/app/models/api/openid_connect/pairwise_pseudonymous_identifier.rb +++ b/app/models/api/openid_connect/pairwise_pseudonymous_identifier.rb @@ -26,10 +26,9 @@ module Api class PairwisePseudonymousIdentifier < ApplicationRecord self.table_name = "ppid" - belongs_to :o_auth_application + belongs_to :o_auth_application, optional: true belongs_to :user - validates :user, presence: true validates :identifier, presence: true, uniqueness: {scope: :user} validates :guid, presence: true, uniqueness: true diff --git a/app/models/aspect_visibility.rb b/app/models/aspect_visibility.rb index 7a0eeba63..be36323cb 100644 --- a/app/models/aspect_visibility.rb +++ b/app/models/aspect_visibility.rb @@ -5,10 +5,8 @@ class AspectVisibility < ApplicationRecord belongs_to :aspect - validates :aspect, :presence => true belongs_to :shareable, :polymorphic => true - validates :shareable, :presence => true validates :aspect, uniqueness: {scope: %i(shareable_id shareable_type)} end diff --git a/app/models/block.rb b/app/models/block.rb index 18f3109c4..57b470e58 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -4,8 +4,7 @@ class Block < ApplicationRecord delegate :name, to: :person, prefix: true - validates :user_id, :presence => true - validates :person_id, :presence => true, :uniqueness => { :scope => :user_id } + validates :person_id, uniqueness: {scope: :user_id} validate :not_blocking_yourself diff --git a/app/models/comment.rb b/app/models/comment.rb index 84fc383c7..4b6baa250 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -26,7 +26,6 @@ class Comment < ApplicationRecord delegate :author_name, to: :parent, prefix: true validates :text, :presence => true, :length => {:maximum => 65535} - validates :parent, :presence => true #should be in relayable (pending on fixing Message) has_many :reports, as: :item diff --git a/app/models/comment_signature.rb b/app/models/comment_signature.rb index a4ef374ab..9e7aa9270 100644 --- a/app/models/comment_signature.rb +++ b/app/models/comment_signature.rb @@ -3,5 +3,4 @@ class CommentSignature < ApplicationRecord self.primary_key = :comment_id belongs_to :comment - validates :comment, presence: true end diff --git a/app/models/contact.rb b/app/models/contact.rb index 440596736..666815d47 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -6,10 +6,7 @@ class Contact < ApplicationRecord include Diaspora::Federated::Base belongs_to :user - validates :user, presence: true - belongs_to :person - validates :person, presence: true validates :person_id, uniqueness: {scope: :user_id} diff --git a/app/models/invitation_code.rb b/app/models/invitation_code.rb index c88434b85..1dd565871 100644 --- a/app/models/invitation_code.rb +++ b/app/models/invitation_code.rb @@ -1,8 +1,6 @@ class InvitationCode < ApplicationRecord belongs_to :user - validates_presence_of :user - before_create :generate_token, :set_default_invite_count delegate :name, to: :user, prefix: true diff --git a/app/models/like_signature.rb b/app/models/like_signature.rb index 1024a5608..bc21533d4 100644 --- a/app/models/like_signature.rb +++ b/app/models/like_signature.rb @@ -3,5 +3,4 @@ class LikeSignature < ApplicationRecord self.primary_key = :like_id belongs_to :like - validates :like, presence: true end diff --git a/app/models/mention.rb b/app/models/mention.rb index 9fab1d195..c5bb859e5 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -5,8 +5,6 @@ class Mention < ApplicationRecord belongs_to :mentions_container, polymorphic: true belongs_to :person - validates :mentions_container, presence: true - validates :person, presence: true scope :local, -> { joins(:person).where.not(people: {owner_id: nil}) diff --git a/app/models/message.rb b/app/models/message.rb index 51ad27867..95992e689 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -7,7 +7,6 @@ class Message < ApplicationRecord delegate :name, to: :author, prefix: true - validates :conversation, presence: true validates :text, presence: true validate :participant_of_parent_conversation diff --git a/app/models/person.rb b/app/models/person.rb index 53eb8ed4d..0fa77b3d7 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -44,8 +44,8 @@ class Person < ApplicationRecord has_many :roles - belongs_to :owner, :class_name => 'User' - belongs_to :pod + belongs_to :owner, class_name: "User", optional: true + belongs_to :pod, optional: true has_many :notification_actors has_many :notifications, :through => :notification_actors diff --git a/app/models/photo.rb b/app/models/photo.rb index 43ab064a3..7fa0c3871 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -37,7 +37,7 @@ class Photo < ApplicationRecord mount_uploader :processed_image, ProcessedImage mount_uploader :unprocessed_image, UnprocessedImage - belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid + belongs_to :status_message, foreign_key: :status_message_guid, primary_key: :guid, optional: true validates_associated :status_message delegate :author_name, to: :status_message, prefix: true diff --git a/app/models/poll_participation.rb b/app/models/poll_participation.rb index c5bb5395e..24f99f9ef 100644 --- a/app/models/poll_participation.rb +++ b/app/models/poll_participation.rb @@ -12,7 +12,6 @@ class PollParticipation < ApplicationRecord alias_attribute :parent, :poll - validates :poll_answer, presence: true validate :not_already_participated def poll_answer_guid=(new_poll_answer_guid) diff --git a/app/models/poll_participation_signature.rb b/app/models/poll_participation_signature.rb index 001d83f7a..a32b8653e 100644 --- a/app/models/poll_participation_signature.rb +++ b/app/models/poll_participation_signature.rb @@ -3,5 +3,4 @@ class PollParticipationSignature < ApplicationRecord self.primary_key = :poll_participation_id belongs_to :poll_participation - validates :poll_participation, presence: true end diff --git a/app/models/post.rb b/app/models/post.rb index fecaaf1bb..4a6c51744 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -24,8 +24,8 @@ class Post < ApplicationRecord has_many :reshares, class_name: "Reshare", foreign_key: :root_guid, primary_key: :guid has_many :resharers, class_name: "Person", through: :reshares, source: :author - belongs_to :o_embed_cache - belongs_to :open_graph_cache + belongs_to :o_embed_cache, optional: true + belongs_to :open_graph_cache, optional: true validates_uniqueness_of :id diff --git a/app/models/report.rb b/app/models/report.rb index 171681977..2f28cfc4c 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -9,8 +9,8 @@ class Report < ApplicationRecord validate :post_or_comment_does_exist, :on => :create belongs_to :user - belongs_to :post - belongs_to :comment + belongs_to :post, optional: true + belongs_to :comment, optional: true belongs_to :item, polymorphic: true after_commit :send_report_notification, :on => :create diff --git a/app/models/reshare.rb b/app/models/reshare.rb index a88208248..bc6d8bfa1 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -3,7 +3,7 @@ # the COPYRIGHT file. class Reshare < Post - belongs_to :root, :class_name => 'Post', :foreign_key => :root_guid, :primary_key => :guid + belongs_to :root, class_name: "Post", foreign_key: :root_guid, primary_key: :guid, optional: true validate :root_must_be_public validates_presence_of :root, :on => :create validates_uniqueness_of :root_guid, :scope => :author_id diff --git a/app/models/role.rb b/app/models/role.rb index 33711ebcf..98601bb7b 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -3,7 +3,6 @@ class Role < ApplicationRecord belongs_to :person - validates :person, presence: true validates :name, uniqueness: {scope: :person_id} validates :name, inclusion: {in: %w(admin moderator spotlight)} diff --git a/app/models/user.rb b/app/models/user.rb index cd8d8e59c..3be7770d3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -51,8 +51,8 @@ class User < ApplicationRecord has_many :aspects, -> { order('order_id ASC') } - belongs_to :auto_follow_back_aspect, :class_name => 'Aspect' - belongs_to :invited_by, :class_name => 'User' + belongs_to :auto_follow_back_aspect, class_name: "Aspect", optional: true + belongs_to :invited_by, class_name: "User", optional: true has_many :aspect_memberships, :through => :aspects diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 65e3dfa99..d4dee7648 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -212,7 +212,8 @@ module Diaspora poll.poll_answers = entity.poll_answers.map do |answer| PollAnswer.new( guid: answer.guid, - answer: answer.answer + answer: answer.answer, + poll: poll ) end end diff --git a/lib/diaspora/fields/target.rb b/lib/diaspora/fields/target.rb index 1fbbf9796..7120c2a84 100644 --- a/lib/diaspora/fields/target.rb +++ b/lib/diaspora/fields/target.rb @@ -6,7 +6,6 @@ module Diaspora belongs_to :target, polymorphic: true validates :target_id, uniqueness: {scope: %i(target_type author_id)} - validates :target, presence: true end end end diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index f7c023291..5753674d4 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -6,6 +6,7 @@ module Diaspora module Relayable def self.included(model) model.class_eval do + validates :parent, presence: true validates_associated :parent validate :author_is_not_ignored diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 1f9318519..08a1b00d8 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -20,12 +20,12 @@ describe Contact, type: :model do it "requires a user" do contact.valid? - expect(contact.errors.full_messages).to include "User can't be blank" + expect(contact.errors.full_messages).to include "User must exist" end it "requires a person" do contact.valid? - expect(contact.errors.full_messages).to include "Person can't be blank" + expect(contact.errors.full_messages).to include "Person must exist" end it "validates uniqueness" do diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index fe3eadfe3..0742ffa50 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -1,12 +1,14 @@ -describe Location, :type => :model do - describe 'before validation' do - it 'should create new location when it has coordinates' do - location = Location.new(coordinates:'1,2') +describe Location, type: :model do + describe "before validation" do + let(:status) { FactoryGirl.create(:status_message) } + + it "should create new location when it has coordinates" do + location = Location.new(coordinates: "1,2", status_message: status) expect(location.save).to be true end - it 'should not create new location when it does not have coordinates' do - location = Location.new() + it "should not create new location when it does not have coordinates" do + location = Location.new(status_message: status) expect(location.save).to be false end end diff --git a/spec/models/poll_participation_spec.rb b/spec/models/poll_participation_spec.rb index 65730481b..677da69f0 100644 --- a/spec/models/poll_participation_spec.rb +++ b/spec/models/poll_participation_spec.rb @@ -1,25 +1,25 @@ describe PollParticipation, type: :model do + let(:status) { bob.post(:status_message, text: "hello", to: bob.aspects.first.id) } + let(:poll) { Poll.new(question: "Who is in charge?") } + before do - @alices_aspect = alice.aspects.first - @status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) - @poll = Poll.new(:question => 'Who is in charge?') - @poll.poll_answers.build(:answer => "a") - @poll.poll_answers.build(:answer => "b") - @status.poll = @poll + poll.poll_answers.build(answer: "a").poll = poll + poll.poll_answers.build(answer: "b").poll = poll + status.poll = poll end describe 'validation' do it 'forbids multiple participations in the same poll' do expect { 2.times do |run| - bob.participate_in_poll!(@status, @poll.poll_answers.first) + bob.participate_in_poll!(status, poll.poll_answers.first) end }.to raise_error ActiveRecord::RecordInvalid end it 'allows a one time participation in a poll' do expect { - bob.participate_in_poll!(@status, @poll.poll_answers.first) + bob.participate_in_poll!(status, poll.poll_answers.first) }.to_not raise_error end end @@ -38,6 +38,6 @@ describe PollParticipation, type: :model do let(:remote_object_on_local_parent) { FactoryGirl.create(:poll_participation, poll_answer: local_parent.poll.poll_answers.first, author: remote_raphael) } - let(:relayable) { PollParticipation::Generator.new(alice, @status, @poll.poll_answers.first).build } + let(:relayable) { PollParticipation::Generator.new(alice, status, poll.poll_answers.first).build } end end diff --git a/spec/models/poll_spec.rb b/spec/models/poll_spec.rb index 30fd872bb..f18eb4fab 100644 --- a/spec/models/poll_spec.rb +++ b/spec/models/poll_spec.rb @@ -1,24 +1,23 @@ -describe Poll, :type => :model do - before do - @poll = Poll.new(:question => "What do you think about apples?") - end +describe Poll, type: :model do + let(:status) { FactoryGirl.create(:status_message) } + let(:poll) { Poll.new(question: "What do you think about apples?", status_message: status) } - describe 'validation' do - it 'should not create a poll when it has less than two answers' do - @poll.poll_answers.build(:answer => '1') - expect(@poll).not_to be_valid + describe "validation" do + it "should not create a poll when it has less than two answers" do + poll.poll_answers.build(answer: "1").poll = poll + expect(poll).not_to be_valid end - it 'should create a poll when it has more than two answers' do - @poll.poll_answers.build(:answer => '1') - @poll.poll_answers.build(:answer => '2') - expect(@poll).to be_valid + it "should create a poll when it has more than two answers" do + poll.poll_answers.build(answer: "1").poll = poll + poll.poll_answers.build(answer: "2").poll = poll + expect(poll).to be_valid end - it 'should not create a poll when question in blank' do - @poll.question = ' ' - @poll.valid? - expect(@poll.errors).to have_key(:question) + it "should not create a poll when question in blank" do + poll.question = " " + poll.valid? + expect(poll.errors).to have_key(:question) end end end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb index 97b4424d1..dad31a04d 100644 --- a/spec/models/role_spec.rb +++ b/spec/models/role_spec.rb @@ -6,7 +6,12 @@ describe Role do let!(:moderator_role) { moderator.roles.create(name: "moderator") } describe "validations" do - it { should validate_presence_of(:person) } + it "validates the presence of the person" do + role = Role.new(name: "admin") + role.valid? + expect(role.errors.full_messages).to include "Person must exist" + end + it { should validate_uniqueness_of(:name).scoped_to(:person_id) } it { should validate_inclusion_of(:name).in_array(%w(admin spotlight moderator)) } end