Skip to content

Commit 77bc642

Browse files
authored
Merge pull request #23617 from kbrock/aaar_scope
Clean up ActsAsArScope and ActsAsArModel for recent rbac changes
2 parents ef03651 + d0ac99d commit 77bc642

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

app/models/chargeback.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# NOTE: Does not implement any search functionality. Implements ActAsArModel to be useable for reports
12
class Chargeback < ActsAsArModel
23
set_columns_hash( # Fields common to any chargeback type
34
:start_date => :datetime,

app/models/manageiq/providers/cloud_manager/vm_or_template.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class ManageIQ::Providers::CloudManager::VmOrTemplate < ActsAsArScope
22
class << self
33
delegate :orphaned, :archived, :to => :aar_scope
4-
delegate :klass, :to => :aar_scope, :prefix => true
54
end
65

76
def self.aar_scope

app/models/manageiq/providers/infra_manager/vm_or_template.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class ManageIQ::Providers::InfraManager::VmOrTemplate < ActsAsArScope
22
class << self
33
delegate :orphaned, :archived, :to => :aar_scope
4-
delegate :klass, :to => :aar_scope, :prefix => true
54
end
65

76
def self.aar_scope

app/models/vim_performance_trend.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# NOTE: Does not implement any search functionality. Implements ActAsArModel to be useable for reports
12
class VimPerformanceTrend < ActsAsArModel
23
set_columns_hash(
34
:resource_name => :string,

lib/acts_as_ar_scope.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
# this is an extension to act as ar_model
2-
# this allows a developer to formulate results as a scope
3-
# all other behavior is handled from there
1+
# This is a form of an AR Model. But the distinction is this is a model based upon an ActiveRecord scope (aar_scope)
2+
# Essentially, this delegates all calls to the scope.
3+
# It may be easier to understand by looking at an implementation like InfraManager::VmOrTemplate
44
class ActsAsArScope < ActsAsArModel
5-
# user required to add aar_scope
65
class << self
76
# Standard query methods are delegated to the core active record classes
87
delegate :includes, :references, :eager_load, :preload, :limit, :order, :offset, :select, :where, :to => :aar_scope
9-
delegate :find, :first, :last, :find_by_id, :find_by, :count, :to => :aar_scope
8+
delegate :find, :first, :last, :find_by_id, :find_by, :count, :all, :to => :aar_scope
109

1110
delegate :klass, :to => :aar_scope, :prefix => true
1211
delegate :table_name, :reflections, :to => :aar_scope_klass
13-
delegate :_virtual_columns_hash, :virtual_reflections, :to => :aar_scope_klass
12+
delegate :virtual_reflections, :to => :aar_scope_klass
1413
end
1514

16-
def self.all(*args)
17-
if args.empty? || args.size == 1 && args.first.respond_to?(:empty?) && args.first.empty?
18-
# avoid warnings
19-
aar_scope
20-
else
21-
aar_scope.all(*args)
22-
end
15+
def self.aar_scope
16+
raise NotImplementedError, _("find must be implemented in a subclass")
2317
end
2418

2519
# TODO: goal is for this to be false

spec/models/manageiq/providers/infra_manager/vm_or_template_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,25 @@
2525
expect(described_class.archived).to match_array([vm, t])
2626
end
2727
end
28+
29+
describe ".aar_scope_klass" do
30+
it "returns the class of the aar_scope" do
31+
expect(described_class.aar_scope_klass).to eq(::VmOrTemplate)
32+
end
33+
end
34+
35+
describe ".orphaned" do
36+
it "delegates" do
37+
# orphaned adds a where clause. lets make sure it works well
38+
# VmOrTemplate is here to tack on a "type" - so we ignore that
39+
expect(described_class.orphaned.where_values_hash.except("type")).to eq(::VmOrTemplate.orphaned.where_values_hash.except("type"))
40+
end
41+
end
42+
43+
describe ".archived" do
44+
it "delegates" do
45+
# VmOrTemplate is here to tack on a "type" - so we ignore that
46+
expect(described_class.archived.where_values_hash.except("type")).to eq(::VmOrTemplate.archived.where_values_hash.except("type"))
47+
end
48+
end
2849
end

0 commit comments

Comments
 (0)