Skip to content

Commit

Permalink
feat(metadata): Refactor MetadataTemplate model to include namespace …
Browse files Browse the repository at this point in the history
…validation and associations
  • Loading branch information
joshsadam committed Jan 8, 2025
1 parent 795e427 commit 2aaedc1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 16 additions & 6 deletions app/models/metadata_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ class MetadataTemplate < ApplicationRecord

has_logidze
acts_as_paranoid
broadcasts_refreshes

# Associations
belongs_to :namespace
belongs_to :created_by, class_name: 'User'

# Validations
validates :name, presence: true, uniqueness: { scope: [:namespace_id] }
validates :description, length: { maximum: 1000 }
# validates :namespace_type,
# inclusion: {
# in: [Group.sti_name, Namespaces::ProjectNamespace.sti_name]
# }

belongs_to :namespace, autosave: true

belongs_to :group, optional: true, foreign_key: :namespace_id # rubocop:disable Rails/InverseOf
belongs_to :project_namespace, optional: true, foreign_key: :namespace_id, class_name: 'Namespaces::ProjectNamespace' # rubocop:disable Rails/InverseOf

validate :validate_namespace

private

def validate_namespace
# Only Groups and Projects should have metadata templates
return if %w[Group Project].include?(namespace.type)

errors.add(namespace.type, 'namespace cannot have metadata templates')
end
end
21 changes: 11 additions & 10 deletions test/models/metadata_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ class MetadataTemplateTest < ActiveSupport::TestCase
end

# Activity Tracking Tests
# test 'tracks activity on create' do
# template = MetadataTemplate.new(
# name: 'Activity Test',
# namespace: @namespace,
# created_by: @user
# )
# assert_difference 'Activity.count' do
# template.save
# end
# end
test 'tracks activity on create' do
skip 'TrackActivity concern is not yet implemented'
template = MetadataTemplate.new(
name: 'Activity Test',
namespace: @namespace,
created_by: @user
)
assert_difference 'Activity.count' do
template.save
end
end

test 'tracks history changes' do
@valid_metadata_template.create_logidze_snapshot!
Expand Down

0 comments on commit 2aaedc1

Please sign in to comment.