Skip to content

Commit 1f3f0a2

Browse files
Validation on model
1 parent 806265f commit 1f3f0a2

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

app/controllers/admin/editions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def permitted_edition_attributes
255255
topic_ids: [],
256256
topical_event_ids: [], # LEGACY
257257
topical_event_document_ids: [], # New
258+
topical_event_links: [],
258259
related_detailed_guide_ids: [],
259260
role_appointment_ids: [],
260261
statistical_data_set_document_ids: [],

app/models/concerns/edition/topical_events.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,28 @@ def process_associations_before_save(edition)
2626
has_many :topical_event_memberships, dependent: :destroy, inverse_of: :edition, foreign_key: :edition_id
2727
has_many :topical_events, through: :topical_event_memberships, source: :topical_event
2828
# NEW
29-
has_many :topical_event_links, -> { of_type "topical_event" }, class_name: "EditionLink", dependent: :destroy, inverse_of: :edition, foreign_key: :edition_id
29+
has_many :topical_event_links, -> { of_type "topical_event" }, class_name: "EditionLink", dependent: :destroy, inverse_of: :edition, foreign_key: :edition_id, validate: false
3030
has_many :topical_event_documents, through: :topical_event_links, source: :document
3131

32+
validates_associated :topical_event_links
33+
after_validation :replace_edition_link_errors
34+
3235
add_trait Trait
3336
end
3437

38+
def replace_edition_link_errors
39+
return unless errors[:topical_event_links]
40+
41+
errors.delete(:topical_event_links)
42+
43+
topical_event_links.each do |topical_event_link|
44+
errors.add(:topical_event_document_ids, topical_event_link.errors.full_messages.join(",")) if topical_event_link.errors.present?
45+
end
46+
end
47+
3548
# This method can be deleted when all legacy content types have been migrated to
3649
# being config-driven (which has its own implementation of the method).
50+
3751
def can_be_associated_with_topical_events?
3852
true
3953
end

app/models/edition_link.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ class EditionLink < ApplicationRecord
22
belongs_to :edition
33
belongs_to :document
44
scope :of_type, ->(type) { where(link_type: type) }
5+
validate :is_not_self
6+
7+
def is_not_self
8+
return unless edition.document == document
9+
10+
errors.add(:base, "cannot include an association with this document (\"#{edition.title}\")")
11+
end
512
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class EditionLinkValidator < ActiveModel::Validator
2+
def validate(record); end
3+
end

app/views/admin/configurable_content_blocks/select_with_search_tagging.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
error_items: errors_for(block.edition.errors, block.path.validation_error_attribute.to_sym, block.title),
66
include_blank: true,
77
heading_size: "m",
8-
options: block.exclude_self(block.public_send("taggable_#{block.container}_container", block.value)),
8+
options: block.public_send("taggable_#{block.container}_container", block.value),
99
multiple: true,
1010
} %>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "test_helper"
2+
3+
class EditionLinkTest < ActiveSupport::TestCase
4+
test "should be invalid if linking to itself" do
5+
@standard_edition = create(:standard_edition)
6+
edition_link = EditionLink.new(edition: @standard_edition, document: @standard_edition.document)
7+
assert_not edition_link.valid?
8+
end
9+
end

0 commit comments

Comments
 (0)