Skip to content

Commit 224e3b0

Browse files
authored
Add model and validation for scoped tags join table
1 parent 23e04e4 commit 224e3b0

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

app/models/event.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ def ancestor_organizer_positions
376376
has_many :tags, -> { includes(:hcb_codes) }
377377
has_and_belongs_to_many :event_tags
378378

379-
has_and_belongs_to_many :event_scoped_tags, class_name: "Event::ScopedTag", association_foreign_key: :event_scoped_tag_id
379+
has_many :event_scoped_tags_events, class_name: "Event::ScopedTagsEvent"
380+
has_many :scoped_tags, through: :event_scoped_tags_events, source: :event_scoped_tag
380381

381382
has_many :pinned_hcb_codes, -> { includes(hcb_code: [:canonical_transactions, :canonical_pending_transactions]) }, class_name: "HcbCode::Pin"
382383

app/models/event/scoped_tag.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#
1212
class Event
1313
class ScopedTag < ApplicationRecord
14-
has_and_belongs_to_many :events, foreign_key: :event_scoped_tag_id, inverse_of: :scoped_tags
14+
has_many :event_scoped_tags_events, foreign_key: :event_scoped_tag_id, inverse_of: :event_scoped_tag, class_name: "Event::ScopedTagsEvent"
15+
has_many :events, through: :event_scoped_tags_events
1516

1617
end
1718

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: event_scoped_tags_events
6+
#
7+
# created_at :datetime not null
8+
# updated_at :datetime not null
9+
# event_id :bigint not null
10+
# event_scoped_tag_id :bigint not null
11+
#
12+
# Indexes
13+
#
14+
# idx_on_event_scoped_tag_id_event_id_4b716d1ac0 (event_scoped_tag_id,event_id) UNIQUE
15+
# index_event_scoped_tags_events_on_event_id (event_id)
16+
# index_event_scoped_tags_events_on_event_scoped_tag_id (event_scoped_tag_id)
17+
#
18+
# Foreign Keys
19+
#
20+
# fk_rails_... (event_id => events.id)
21+
# fk_rails_... (event_scoped_tag_id => event_scoped_tags.id)
22+
#
23+
class Event
24+
class ScopedTagsEvent < ApplicationRecord
25+
belongs_to :event
26+
belongs_to :event_scoped_tag, class_name: "Event::ScopedTag"
27+
28+
validate :event_is_subevent
29+
30+
private
31+
32+
def event_is_subevent
33+
if event.parent_id.nil?
34+
errors.add(:event, "must be a subevent to be tagged with a scoped tag")
35+
end
36+
end
37+
38+
end
39+
40+
end

0 commit comments

Comments
 (0)