Skip to content

Commit 01dbb53

Browse files
authored
Merge pull request #23607 from kbrock/report_requires3_other_models
Remove references.includes from non Rbac usage
2 parents 7cddb9a + 64bd4a0 commit 01dbb53

File tree

6 files changed

+13
-15
lines changed

6 files changed

+13
-15
lines changed

app/models/classification.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Classification < ApplicationRecord
3131
scope :is_category, -> { where(:parent_id => nil) }
3232
scope :is_entry, -> { where.not(:parent_id => nil) }
3333

34+
# TODO: move over to joins(:parent) or preload(:parent) - need something that works well with load()
3435
scope :with_writable_parents, -> { includes(:parent).where(:parents_classifications => {:read_only => false}) }
3536

3637
DEFAULT_NAMESPACE = "/managed".freeze
@@ -211,13 +212,13 @@ def self.create_category!(options)
211212
end
212213

213214
def self.categories(region_id = my_region_number, ns = DEFAULT_NAMESPACE)
214-
cats = is_category.in_region(region_id).includes(:tag, :children)
215+
cats = is_category.in_region(region_id).preload(:tag, :children)
215216
cats.select { |c| c.ns == ns }
216217
end
217218

218219
def self.category_names_for_perf_by_tag(region_id = my_region_number, ns = DEFAULT_NAMESPACE)
219220
in_region(region_id).is_category.where(:perf_by_tag => true)
220-
.includes(:tag)
221+
.preload(:tag)
221222
.collect { |c| c.name if c.tag2ns(c.tag.name) == ns }
222223
.compact
223224
end
@@ -513,7 +514,7 @@ def self.add_entries_from_hash(cat, entries)
513514
def validate_uniqueness_on_tag_name
514515
tag_name = Classification.name2tag(name, parent, ns)
515516
exist_scope = Classification.default_scoped
516-
.includes(:tag)
517+
.left_outer_joins(:tag)
517518
.where(:tags => {:name => tag_name})
518519
.merge(Tag.in_region(region_id))
519520
exist_scope = exist_scope.where.not(:id => id) unless new_record?

app/models/mixins/assignment_mixin.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def assignments
173173
records = kind_of?(Class) ? all : self
174174
assignment_map = records.index_by { |a| a.id }
175175
Tag
176-
.includes(:taggings).references(:taggings)
177-
.where("taggings.taggable_type = ? and tags.name like ?", name, "#{namespace}/%")
176+
.eager_load(:taggings).where(:taggings => {:taggable_type => name})
177+
.where("tags.name like ?", "#{namespace}/%")
178178
.each_with_object(Hash.new { |h, k| h[k] = [] }) do |tag, ret|
179179
tag.taggings.each do |tagging|
180180
tag_name = Tag.filter_ns([tag], namespace).first
@@ -221,8 +221,7 @@ def get_assigned_for_target(target, options = {})
221221
# look for alert_set running off of tags (not individual tags)
222222
# TODO: we may need to change taggings-related code to use base_model too
223223
tlist = Tagging.where("tags.name like '/managed/%'")
224-
.where(:taggable => parents)
225-
.references(:tag).includes(:tag).map do |t|
224+
.where(:taggable => parents).eager_load(:tag).map do |t|
226225
"#{tag_class(t.taggable_type)}/tag#{t.tag.name}"
227226
end
228227

lib/extensions/ar_taggable.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ def tag_remove(list, options = {})
166166
end
167167

168168
def tagged_with(options = {})
169-
tagging = Tagging.arel_table
170-
query = Tag.includes(:taggings).references(:taggings)
171-
query = query.where(tagging[:taggable_type].eq(self.class.base_class.name))
172-
query = query.where(tagging[:taggable_id].eq(id))
169+
query = Tag.eager_load(:taggings).where(:taggings => {:taggable_type => self.class.base_class.name, :taggable_id => id})
173170
ns = Tag.get_namespace(options)
174171
query = query.where(Tag.arel_table[:name].matches("#{ns}%")) if ns
175172
query

spec/lib/extensions/ar_order_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
# the string munging gives us issues.
88
it "supports order when distinct is present for has_many virtual column" do
99
expect do
10-
VmOrTemplate.includes(:disks).references(:disks).order(:last_compliance_status).first
10+
VmOrTemplate.eager_load(:disks).order(:last_compliance_status).first
1111
end.not_to raise_error
1212
end
1313

1414
it "supports order when distinct is present for basic column" do
1515
expect do
16-
VmOrTemplate.includes(:disks).references(:disks).order(:id).first
16+
VmOrTemplate.eager_load(:disks).order(:id).first
1717
end.not_to raise_error
1818
end
1919
end

spec/models/container_group_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
it "preloads the conditions" do
5454
condition_other
5555
cr = condition_ready
56-
cg = ContainerGroup.includes(:ready_condition_status).references(:ready_condition_status).find_by(:id => container_group.id)
56+
cg = ContainerGroup.preload(:ready_condition_status).find_by(:id => container_group.id)
5757

5858
expect { expect(cg.ready_condition).to eq(cr) }.to_not make_database_queries
5959
end

tools/convert_mapped_tags.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
condition_for_mapped_tags = ProviderTagMapping::TAG_PREFIXES.map { "tags.name LIKE ?" }.join(' OR ')
2929
tag_values = ProviderTagMapping::TAG_PREFIXES.map { |x| "#{x}%:%" }
3030

31+
# TODO: do we want eager_load, or left join these
3132
Classification.where.not(:id => Classification.region_to_range) # only other regions(not current, we expected that current region is global)
3233
.is_category
33-
.includes(:tag, :children).references(:tag, :children)
34+
.eager_load(:tag, :children)
3435
.where(condition_for_mapped_tags, *tag_values) # only mapped categories
3536
.find_each do |category|
3637
new_parent_category = Classification.in_my_region.find_by(:description => category.description)

0 commit comments

Comments
 (0)