Fix belongs_to, it is now required by default

This commit is contained in:
Benjamin Neff 2017-08-05 21:29:51 +02:00
parent eb19c327e2
commit d0b634852b
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
29 changed files with 56 additions and 69 deletions

View file

@ -6,8 +6,7 @@ module Api
belongs_to :user belongs_to :user
belongs_to :o_auth_application belongs_to :o_auth_application
validates :user, presence: true, uniqueness: {scope: :o_auth_application} validates :user, uniqueness: {scope: :o_auth_application}
validates :o_auth_application, presence: true
validate :validate_scope_names validate :validate_scope_names
serialize :scopes, JSON serialize :scopes, JSON

View file

@ -29,7 +29,6 @@ module Api
before_validation :setup, on: :create before_validation :setup, on: :create
validates :token, presence: true, uniqueness: true validates :token, presence: true, uniqueness: true
validates :authorization, presence: true
scope :valid, ->(time) { where("expires_at >= ?", time) } scope :valid, ->(time) { where("expires_at >= ?", time) }

View file

@ -26,10 +26,9 @@ module Api
class PairwisePseudonymousIdentifier < ApplicationRecord class PairwisePseudonymousIdentifier < ApplicationRecord
self.table_name = "ppid" self.table_name = "ppid"
belongs_to :o_auth_application belongs_to :o_auth_application, optional: true
belongs_to :user belongs_to :user
validates :user, presence: true
validates :identifier, presence: true, uniqueness: {scope: :user} validates :identifier, presence: true, uniqueness: {scope: :user}
validates :guid, presence: true, uniqueness: true validates :guid, presence: true, uniqueness: true

View file

@ -5,10 +5,8 @@
class AspectVisibility < ApplicationRecord class AspectVisibility < ApplicationRecord
belongs_to :aspect belongs_to :aspect
validates :aspect, :presence => true
belongs_to :shareable, :polymorphic => true belongs_to :shareable, :polymorphic => true
validates :shareable, :presence => true
validates :aspect, uniqueness: {scope: %i(shareable_id shareable_type)} validates :aspect, uniqueness: {scope: %i(shareable_id shareable_type)}
end end

View file

@ -4,8 +4,7 @@ class Block < ApplicationRecord
delegate :name, to: :person, prefix: true delegate :name, to: :person, prefix: true
validates :user_id, :presence => true validates :person_id, uniqueness: {scope: :user_id}
validates :person_id, :presence => true, :uniqueness => { :scope => :user_id }
validate :not_blocking_yourself validate :not_blocking_yourself

View file

@ -26,7 +26,6 @@ class Comment < ApplicationRecord
delegate :author_name, to: :parent, prefix: true delegate :author_name, to: :parent, prefix: true
validates :text, :presence => true, :length => {:maximum => 65535} validates :text, :presence => true, :length => {:maximum => 65535}
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
has_many :reports, as: :item has_many :reports, as: :item

View file

@ -3,5 +3,4 @@ class CommentSignature < ApplicationRecord
self.primary_key = :comment_id self.primary_key = :comment_id
belongs_to :comment belongs_to :comment
validates :comment, presence: true
end end

View file

@ -6,10 +6,7 @@ class Contact < ApplicationRecord
include Diaspora::Federated::Base include Diaspora::Federated::Base
belongs_to :user belongs_to :user
validates :user, presence: true
belongs_to :person belongs_to :person
validates :person, presence: true
validates :person_id, uniqueness: {scope: :user_id} validates :person_id, uniqueness: {scope: :user_id}

View file

@ -1,8 +1,6 @@
class InvitationCode < ApplicationRecord class InvitationCode < ApplicationRecord
belongs_to :user belongs_to :user
validates_presence_of :user
before_create :generate_token, :set_default_invite_count before_create :generate_token, :set_default_invite_count
delegate :name, to: :user, prefix: true delegate :name, to: :user, prefix: true

View file

@ -3,5 +3,4 @@ class LikeSignature < ApplicationRecord
self.primary_key = :like_id self.primary_key = :like_id
belongs_to :like belongs_to :like
validates :like, presence: true
end end

View file

@ -5,8 +5,6 @@
class Mention < ApplicationRecord class Mention < ApplicationRecord
belongs_to :mentions_container, polymorphic: true belongs_to :mentions_container, polymorphic: true
belongs_to :person belongs_to :person
validates :mentions_container, presence: true
validates :person, presence: true
scope :local, -> { scope :local, -> {
joins(:person).where.not(people: {owner_id: nil}) joins(:person).where.not(people: {owner_id: nil})

View file

@ -7,7 +7,6 @@ class Message < ApplicationRecord
delegate :name, to: :author, prefix: true delegate :name, to: :author, prefix: true
validates :conversation, presence: true
validates :text, presence: true validates :text, presence: true
validate :participant_of_parent_conversation validate :participant_of_parent_conversation

View file

@ -44,8 +44,8 @@ class Person < ApplicationRecord
has_many :roles has_many :roles
belongs_to :owner, :class_name => 'User' belongs_to :owner, class_name: "User", optional: true
belongs_to :pod belongs_to :pod, optional: true
has_many :notification_actors has_many :notification_actors
has_many :notifications, :through => :notification_actors has_many :notifications, :through => :notification_actors

View file

@ -37,7 +37,7 @@ class Photo < ApplicationRecord
mount_uploader :processed_image, ProcessedImage mount_uploader :processed_image, ProcessedImage
mount_uploader :unprocessed_image, UnprocessedImage 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 validates_associated :status_message
delegate :author_name, to: :status_message, prefix: true delegate :author_name, to: :status_message, prefix: true

View file

@ -12,7 +12,6 @@ class PollParticipation < ApplicationRecord
alias_attribute :parent, :poll alias_attribute :parent, :poll
validates :poll_answer, presence: true
validate :not_already_participated validate :not_already_participated
def poll_answer_guid=(new_poll_answer_guid) def poll_answer_guid=(new_poll_answer_guid)

View file

@ -3,5 +3,4 @@ class PollParticipationSignature < ApplicationRecord
self.primary_key = :poll_participation_id self.primary_key = :poll_participation_id
belongs_to :poll_participation belongs_to :poll_participation
validates :poll_participation, presence: true
end end

View file

@ -24,8 +24,8 @@ class Post < ApplicationRecord
has_many :reshares, class_name: "Reshare", foreign_key: :root_guid, primary_key: :guid has_many :reshares, class_name: "Reshare", foreign_key: :root_guid, primary_key: :guid
has_many :resharers, class_name: "Person", through: :reshares, source: :author has_many :resharers, class_name: "Person", through: :reshares, source: :author
belongs_to :o_embed_cache belongs_to :o_embed_cache, optional: true
belongs_to :open_graph_cache belongs_to :open_graph_cache, optional: true
validates_uniqueness_of :id validates_uniqueness_of :id

View file

@ -9,8 +9,8 @@ class Report < ApplicationRecord
validate :post_or_comment_does_exist, :on => :create validate :post_or_comment_does_exist, :on => :create
belongs_to :user belongs_to :user
belongs_to :post belongs_to :post, optional: true
belongs_to :comment belongs_to :comment, optional: true
belongs_to :item, polymorphic: true belongs_to :item, polymorphic: true
after_commit :send_report_notification, :on => :create after_commit :send_report_notification, :on => :create

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class Reshare < Post 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 validate :root_must_be_public
validates_presence_of :root, :on => :create validates_presence_of :root, :on => :create
validates_uniqueness_of :root_guid, :scope => :author_id validates_uniqueness_of :root_guid, :scope => :author_id

View file

@ -3,7 +3,6 @@
class Role < ApplicationRecord class Role < ApplicationRecord
belongs_to :person belongs_to :person
validates :person, presence: true
validates :name, uniqueness: {scope: :person_id} validates :name, uniqueness: {scope: :person_id}
validates :name, inclusion: {in: %w(admin moderator spotlight)} validates :name, inclusion: {in: %w(admin moderator spotlight)}

View file

@ -51,8 +51,8 @@ class User < ApplicationRecord
has_many :aspects, -> { order('order_id ASC') } has_many :aspects, -> { order('order_id ASC') }
belongs_to :auto_follow_back_aspect, :class_name => 'Aspect' belongs_to :auto_follow_back_aspect, class_name: "Aspect", optional: true
belongs_to :invited_by, :class_name => 'User' belongs_to :invited_by, class_name: "User", optional: true
has_many :aspect_memberships, :through => :aspects has_many :aspect_memberships, :through => :aspects

View file

@ -212,7 +212,8 @@ module Diaspora
poll.poll_answers = entity.poll_answers.map do |answer| poll.poll_answers = entity.poll_answers.map do |answer|
PollAnswer.new( PollAnswer.new(
guid: answer.guid, guid: answer.guid,
answer: answer.answer answer: answer.answer,
poll: poll
) )
end end
end end

View file

@ -6,7 +6,6 @@ module Diaspora
belongs_to :target, polymorphic: true belongs_to :target, polymorphic: true
validates :target_id, uniqueness: {scope: %i(target_type author_id)} validates :target_id, uniqueness: {scope: %i(target_type author_id)}
validates :target, presence: true
end end
end end
end end

View file

@ -6,6 +6,7 @@ module Diaspora
module Relayable module Relayable
def self.included(model) def self.included(model)
model.class_eval do model.class_eval do
validates :parent, presence: true
validates_associated :parent validates_associated :parent
validate :author_is_not_ignored validate :author_is_not_ignored

View file

@ -20,12 +20,12 @@ describe Contact, type: :model do
it "requires a user" do it "requires a user" do
contact.valid? 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 end
it "requires a person" do it "requires a person" do
contact.valid? 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 end
it "validates uniqueness" do it "validates uniqueness" do

View file

@ -1,12 +1,14 @@
describe Location, :type => :model do describe Location, type: :model do
describe 'before validation' do describe "before validation" do
it 'should create new location when it has coordinates' do let(:status) { FactoryGirl.create(:status_message) }
location = Location.new(coordinates:'1,2')
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 expect(location.save).to be true
end end
it 'should not create new location when it does not have coordinates' do it "should not create new location when it does not have coordinates" do
location = Location.new() location = Location.new(status_message: status)
expect(location.save).to be false expect(location.save).to be false
end end
end end

View file

@ -1,25 +1,25 @@
describe PollParticipation, type: :model do 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 before do
@alices_aspect = alice.aspects.first poll.poll_answers.build(answer: "a").poll = poll
@status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) poll.poll_answers.build(answer: "b").poll = poll
@poll = Poll.new(:question => 'Who is in charge?') status.poll = poll
@poll.poll_answers.build(:answer => "a")
@poll.poll_answers.build(:answer => "b")
@status.poll = @poll
end end
describe 'validation' do describe 'validation' do
it 'forbids multiple participations in the same poll' do it 'forbids multiple participations in the same poll' do
expect { expect {
2.times do |run| 2.times do |run|
bob.participate_in_poll!(@status, @poll.poll_answers.first) bob.participate_in_poll!(status, poll.poll_answers.first)
end end
}.to raise_error ActiveRecord::RecordInvalid }.to raise_error ActiveRecord::RecordInvalid
end end
it 'allows a one time participation in a poll' do it 'allows a one time participation in a poll' do
expect { expect {
bob.participate_in_poll!(@status, @poll.poll_answers.first) bob.participate_in_poll!(status, poll.poll_answers.first)
}.to_not raise_error }.to_not raise_error
end end
end end
@ -38,6 +38,6 @@ describe PollParticipation, type: :model do
let(:remote_object_on_local_parent) { let(:remote_object_on_local_parent) {
FactoryGirl.create(:poll_participation, poll_answer: local_parent.poll.poll_answers.first, author: remote_raphael) 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
end end

View file

@ -1,24 +1,23 @@
describe Poll, :type => :model do describe Poll, type: :model do
before do let(:status) { FactoryGirl.create(:status_message) }
@poll = Poll.new(:question => "What do you think about apples?") let(:poll) { Poll.new(question: "What do you think about apples?", status_message: status) }
end
describe 'validation' do describe "validation" do
it 'should not create a poll when it has less than two answers' do it "should not create a poll when it has less than two answers" do
@poll.poll_answers.build(:answer => '1') poll.poll_answers.build(answer: "1").poll = poll
expect(@poll).not_to be_valid expect(poll).not_to be_valid
end end
it 'should create a poll when it has more than two answers' do it "should create a poll when it has more than two answers" do
@poll.poll_answers.build(:answer => '1') poll.poll_answers.build(answer: "1").poll = poll
@poll.poll_answers.build(:answer => '2') poll.poll_answers.build(answer: "2").poll = poll
expect(@poll).to be_valid expect(poll).to be_valid
end end
it 'should not create a poll when question in blank' do it "should not create a poll when question in blank" do
@poll.question = ' ' poll.question = " "
@poll.valid? poll.valid?
expect(@poll.errors).to have_key(:question) expect(poll.errors).to have_key(:question)
end end
end end
end end

View file

@ -6,7 +6,12 @@ describe Role do
let!(:moderator_role) { moderator.roles.create(name: "moderator") } let!(:moderator_role) { moderator.roles.create(name: "moderator") }
describe "validations" do 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_uniqueness_of(:name).scoped_to(:person_id) }
it { should validate_inclusion_of(:name).in_array(%w(admin spotlight moderator)) } it { should validate_inclusion_of(:name).in_array(%w(admin spotlight moderator)) }
end end