Skip to content

Commit

Permalink
Update tag_set.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
seenanair committed Feb 12, 2025
1 parent 0f43fc7 commit cec6cd1
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion app/models/tag_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,33 @@ class TagSet < ApplicationRecord
validate :tag_group_adapter_types_must_match

scope :dual_index, -> { where.not(tag2_group: nil) }

# This scope retrieves tag sets that are visible.
# - If `tag_group` is present and visible, the tag set is included.
# - If `tag2_group` is present and visible, the tag set is included.
# - If `tag2_group` is not present, the tag set is included.
# - If `tag2_group` is present but not visible, the tag set is excluded.
scope :visible,
-> { joins(:tag_group, :tag2_group).where(tag_group: { visible: true }, tag2_group: { visible: true }) }
-> do
joins(:tag_group)
.joins('LEFT JOIN tag_groups AS tag2_groups ON tag_sets.tag2_group_id = tag2_groups.id')
.where(tag_groups: { visible: true })
.where('tag2_groups.id IS NULL OR tag2_groups.visible = ?', true)
end

# The scoping retrieves the visible tag sets and makes sure they are dual index.
scope :visible_dual_index, -> { dual_index.visible }

scope :single_index, -> { where(tag2_group: nil) }

# The scoping retrieves the visible tag sets and makes sure they are single index.
# Define the visible_single_index scope
# Define the visible_single_index scope
scope :visible_single_index, -> { single_index.visible }

# Define the scope that combines visible_single_index and chromium tag_group
scope :visible_single_index_chromium, -> { visible_single_index.joins(:tag_group).merge(TagGroup.chromium) }

# Dynamic method to determine the visibility of a tag_set based on the visibility of its tag_groups
# TagSet has a method to check if itself is visible by checking
# the visibility of both tag_group and (if not null) tag2_group.
Expand All @@ -45,4 +66,13 @@ def tag_group_name=(name)
def tag2_group_name=(name)
self.tag2_group = TagGroup.find_by!(name:)
end

# Returns all TagGroup records within the visible_single_index_chromium scope
#
# This method retrieves all TagGroup records that are associated with visible single index tag sets
# and have the adapter type 'Chromium'.
# @return [ActiveRecord::Relation] A relation containing the TagGroup records
def self.tag_groups_within_visible_single_index_chromium
TagGroup.where(id: visible_single_index_chromium.select(:tag_group_id))
end
end

0 comments on commit cec6cd1

Please sign in to comment.