Commit d7586c57 authored by Toon Claes's avatar Toon Claes

Add relation for CustomEmoji to creator

Custom Emoji belongs to a creator, which is a User. And a User has many
created Custom Emoji.
parent bd6faf2b
...@@ -6,6 +6,7 @@ class CustomEmoji < ApplicationRecord ...@@ -6,6 +6,7 @@ class CustomEmoji < ApplicationRecord
belongs_to :namespace, inverse_of: :custom_emoji belongs_to :namespace, inverse_of: :custom_emoji
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id' belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id'
belongs_to :creator, class_name: "User", inverse_of: :created_custom_emoji
# For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467 # For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467
validates :external, inclusion: { in: [true] } validates :external, inclusion: { in: [true] }
...@@ -15,6 +16,7 @@ class CustomEmoji < ApplicationRecord ...@@ -15,6 +16,7 @@ class CustomEmoji < ApplicationRecord
validate :valid_emoji_name validate :valid_emoji_name
validates :group, presence: true validates :group, presence: true
validates :creator, presence: true
validates :name, validates :name,
uniqueness: { scope: [:namespace_id, :name] }, uniqueness: { scope: [:namespace_id, :name] },
presence: true, presence: true,
......
...@@ -179,6 +179,7 @@ class User < ApplicationRecord ...@@ -179,6 +179,7 @@ class User < ApplicationRecord
has_many :merge_request_reviewers, inverse_of: :reviewer has_many :merge_request_reviewers, inverse_of: :reviewer
has_many :assigned_issues, class_name: "Issue", through: :issue_assignees, source: :issue has_many :assigned_issues, class_name: "Issue", through: :issue_assignees, source: :issue
has_many :assigned_merge_requests, class_name: "MergeRequest", through: :merge_request_assignees, source: :merge_request has_many :assigned_merge_requests, class_name: "MergeRequest", through: :merge_request_assignees, source: :merge_request
has_many :created_custom_emoji, class_name: 'CustomEmoji', inverse_of: :creator
has_many :bulk_imports has_many :bulk_imports
......
...@@ -6,5 +6,6 @@ FactoryBot.define do ...@@ -6,5 +6,6 @@ FactoryBot.define do
namespace namespace
group group
file { 'https://gitlab.com/images/partyparrot.png' } file { 'https://gitlab.com/images/partyparrot.png' }
creator { namespace.owner }
end end
end end
...@@ -4,8 +4,10 @@ require 'spec_helper' ...@@ -4,8 +4,10 @@ require 'spec_helper'
RSpec.describe CustomEmoji do RSpec.describe CustomEmoji do
describe 'Associations' do describe 'Associations' do
it { is_expected.to belong_to(:namespace) } it { is_expected.to belong_to(:namespace).inverse_of(:custom_emoji) }
it { is_expected.to belong_to(:creator).inverse_of(:created_custom_emoji) }
it { is_expected.to have_db_column(:file) } it { is_expected.to have_db_column(:file) }
it { is_expected.to validate_presence_of(:creator) }
it { is_expected.to validate_length_of(:name).is_at_most(36) } it { is_expected.to validate_length_of(:name).is_at_most(36) }
it { is_expected.to validate_presence_of(:name) } it { is_expected.to validate_presence_of(:name) }
it { is_expected.to have_db_column(:external) } it { is_expected.to have_db_column(:external) }
...@@ -36,7 +38,7 @@ RSpec.describe CustomEmoji do ...@@ -36,7 +38,7 @@ RSpec.describe CustomEmoji do
new_emoji = build(:custom_emoji, name: old_emoji.name, namespace: old_emoji.namespace, group: group) new_emoji = build(:custom_emoji, name: old_emoji.name, namespace: old_emoji.namespace, group: group)
expect(new_emoji).not_to be_valid expect(new_emoji).not_to be_valid
expect(new_emoji.errors.messages).to eq(name: ["has already been taken"]) expect(new_emoji.errors.messages).to include(name: ["has already been taken"])
end end
it 'disallows non http and https file value' do it 'disallows non http and https file value' do
......
...@@ -101,6 +101,7 @@ RSpec.describe User do ...@@ -101,6 +101,7 @@ RSpec.describe User do
it { is_expected.to have_many(:reviews).inverse_of(:author) } it { is_expected.to have_many(:reviews).inverse_of(:author) }
it { is_expected.to have_many(:merge_request_assignees).inverse_of(:assignee) } it { is_expected.to have_many(:merge_request_assignees).inverse_of(:assignee) }
it { is_expected.to have_many(:merge_request_reviewers).inverse_of(:reviewer) } it { is_expected.to have_many(:merge_request_reviewers).inverse_of(:reviewer) }
it { is_expected.to have_many(:created_custom_emoji).inverse_of(:creator) }
describe "#user_detail" do describe "#user_detail" do
it 'does not persist `user_detail` by default' do it 'does not persist `user_detail` by default' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment