Skip to content

Commit 1471303

Browse files
committed
Switch ActsAsArModel to be QueryRelation based by default
1 parent cd61157 commit 1471303

File tree

5 files changed

+155
-73
lines changed

5 files changed

+155
-73
lines changed

app/models/pglogical_subscription.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ class PglogicalSubscription < ActsAsArModel
1616
:provider_region_name => :string
1717
)
1818

19-
def self.find(*args)
20-
case args.first
19+
def self.search(mode, args)
20+
case mode
2121
when :all then find_all
22-
when :first, :last then find_one(args.first)
23-
else find_id(args.first)
22+
when :first, :last then find_one(mode)
23+
else find_id(mode)
2424
end
2525
end
2626

2727
def self.lookup_by_id(to_find)
28-
find(to_find)
28+
find_id(to_find)
2929
rescue ActiveRecord::RecordNotFound
3030
nil
3131
end
32-
3332
singleton_class.send(:alias_method, :find_by_id, :lookup_by_id)
34-
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_id => :lookup_by_id)
3533

3634
def save!(reload_failover_monitor = true)
3735
assert_different_region!
@@ -73,7 +71,7 @@ def delete(reload_failover_monitor_service = true)
7371
end
7472

7573
def self.delete_all(list = nil)
76-
(list.nil? ? find(:all) : list)&.each { |sub| sub.delete(false) }
74+
(list.nil? ? all : list)&.each { |sub| sub.delete(false) }
7775
EvmDatabase.restart_failover_monitor_service_queue
7876
nil
7977
end
@@ -189,7 +187,7 @@ def self.find_id(to_find)
189187
subscriptions.each do |s|
190188
return new(subscription_to_columns(s)) if s.symbolize_keys[:subscription_name] == to_find
191189
end
192-
raise ActiveRecord::RecordNotFound, "Coundn't find subscription with id #{to_find}"
190+
raise ActiveRecord::RecordNotFound, "Couldn't find subscription with id #{to_find}"
193191
end
194192
private_class_method :find_id
195193

lib/acts_as_ar_model.rb

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'query_relation'
2+
13
class ActsAsArModel
24
include Vmdb::Logging
35
include ArVisibleAttribute
@@ -121,6 +123,7 @@ def self.set_columns_hash(hash)
121123
include ActiveRecord::VirtualAttributes::VirtualFields
122124

123125
include AttributeBag
126+
extend QueryRelation::Queryable
124127

125128
def self.instances_are_derived?
126129
true
@@ -146,59 +149,49 @@ def self.reflections
146149
#
147150
# Find routines
148151
#
149-
def self.find(*_args)
150-
raise NotImplementedError, _("find must be implemented in a subclass")
152+
def self.find(mode_or_id, *args)
153+
if %i[all first last].include?(mode_or_id)
154+
Vmdb::Deprecation.warn("find(:#{mode_or_id}) is deprecated (use #{mode_or_id} instead)")
155+
search(mode_or_id, from_legacy_options(args.extract_options!))
156+
else
157+
lookup_by_id(mode_or_id, *args).tap do |record|
158+
raise ActiveRecord::RecordNotFound, "Couldn't find #{self} with id=#{mode_or_id}" if record.nil?
159+
end
160+
end
151161
end
152162

153-
# This method is called by QueryRelation upon executing the query.
154-
# Since it will receive non-legacy search options, we need to convert
155-
# them to handle the legacy find.
156-
def self.search(mode, options = {})
157-
find(mode, to_legacy_options(options))
163+
def self.search(mode, options)
164+
raise NotImplementedError, "must be defined in a subclass"
158165
end
159-
private_class_method :search
160166

161-
def self.to_legacy_options(options)
167+
def self.from_legacy_options(options)
162168
{
163-
:conditions => options[:where],
164-
:include => options[:includes],
165-
:limit => options[:limit],
166-
:order => options[:order],
167-
:offset => options[:offset],
168-
:select => options[:select],
169-
:group => options[:group],
169+
:where => options[:conditions],
170+
:includes => options[:include],
171+
:limit => options[:limit],
172+
:order => options[:order],
173+
:offset => options[:offset],
174+
:select => options[:select],
175+
:group => options[:group],
170176
}.delete_blanks
171177
end
172-
private_class_method :to_legacy_options
173-
174-
def self.all(*args)
175-
require 'query_relation'
176-
QueryRelation.new(self, *args)
177-
end
178-
179-
def self.first(*args)
180-
find(:first, *args)
181-
end
182-
183-
def self.last(*args)
184-
find(:last, *args)
185-
end
186-
187-
def self.count(*args)
188-
all(*args).count
189-
end
178+
private_class_method :from_legacy_options
190179

191180
def self.find_by_id(*id)
192181
options = id.extract_options!
193-
options[:conditions] = {:id => id.first}
194-
first(options)
182+
options[:where] = {:id => id.first}
183+
search(:first, options)
195184
end
196185

186+
singleton_class.send(:alias_method, :lookup_by_id, :find_by_id)
187+
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_id => :lookup_by_id)
188+
197189
def self.find_all_by_id(*ids)
198190
options = ids.extract_options!
199-
options[:conditions] = {:id => ids.flatten}
200-
all(options)
191+
options[:where] = {:id => ids.flatten}
192+
search(:all, options)
201193
end
194+
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_all_by_id => "use where(:id => ids) instead")
202195

203196
#
204197
# Methods pulled from ActiveRecord 2.3.8

spec/lib/acts_as_ar_model_spec.rb

Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,133 @@
5555
end
5656

5757
describe ".all" do
58-
it "chains through active query" do
59-
expect(base_class).to receive(:find).with(:all, {}).and_return([])
58+
it "comes from QueryRelation" do
59+
expect(base_class).to receive(:search).with(:all, {}).and_return([])
6060
expect(base_class.all.to_a).to eq([])
6161
end
6262

63-
it "supports where (as an example)" do
64-
expect(base_class).to receive(:find).with(:all, {:conditions => {:id => 5}}).and_return([])
63+
it "supports where from QueryRelation (as an example)" do
64+
expect(base_class).to receive(:search).with(:all, {:where => {:id => 5}}).and_return([])
6565
expect(base_class.all.where(:id => 5).to_a).to eq([])
6666
end
6767
end
6868

6969
describe ".first" do
70-
it "chains through active query" do
71-
expect(base_class).to receive(:find).with(:first).and_return(nil)
70+
it "comes from QueryRelation" do
71+
expect(base_class).to receive(:search).with(:first, {}).and_return(nil)
7272
expect(base_class.first).to eq(nil)
7373
end
7474
end
7575

7676
describe ".last" do
77-
it "chains through active query" do
78-
expect(base_class).to receive(:find).with(:last).and_return(nil)
77+
it "comes from QueryRelation" do
78+
expect(base_class).to receive(:search).with(:last, {}).and_return(nil)
7979
expect(base_class.last).to eq(nil)
8080
end
8181
end
8282

8383
describe ".count" do
84-
it "chains through active query" do
85-
expect(base_class).to receive(:find).with(:all, {}).and_return([])
84+
it "comes from QueryRelation" do
85+
expect(base_class).to receive(:search).with(:all, {}).and_return([])
8686
expect(base_class.count).to eq(0)
8787
end
8888
end
89+
90+
describe ".find (deprecated)" do
91+
around do |example|
92+
Vmdb::Deprecation.silence do
93+
example.run
94+
end
95+
end
96+
97+
describe "find(:all)" do
98+
it "chains through QueryRelation" do
99+
expect(base_class).to receive(:search).with(:all, {}).and_return([])
100+
expect(base_class.find(:all).to_a).to eq([])
101+
end
102+
103+
it "supports :conditions legacy option (as an example)" do
104+
expect(base_class).to receive(:search).with(:all, {:where => {:id => 5}}).and_return([])
105+
expect(base_class.find(:all, :conditions => {:id => 5}).to_a).to eq([])
106+
end
107+
end
108+
109+
describe "find(:first)" do
110+
it "chains through QueryRelation" do
111+
expect(base_class).to receive(:search).with(:first, {}).and_return(nil)
112+
expect(base_class.find(:first)).to eq(nil)
113+
end
114+
end
115+
116+
describe ".find(:last)" do
117+
it "chains through QueryRelation" do
118+
expect(base_class).to receive(:search).with(:last, {}).and_return(nil)
119+
expect(base_class.find(:last)).to eq(nil)
120+
end
121+
end
122+
end
123+
124+
describe ".find" do
125+
it "finds a record by id" do
126+
expected = Object.new
127+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected)
128+
expect(base_class.find(5)).to eq(expected)
129+
end
130+
131+
it "raises when not found" do
132+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil)
133+
expect { base_class.find(5) }.to raise_error(ActiveRecord::RecordNotFound)
134+
end
135+
end
136+
137+
describe ".lookup_by_id" do
138+
it "finds a record by id" do
139+
expected = Object.new
140+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected)
141+
expect(base_class.lookup_by_id(5)).to eq(expected)
142+
end
143+
144+
it "returns nil when not found" do
145+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil)
146+
expect(base_class.lookup_by_id(5)).to be_nil
147+
end
148+
end
149+
150+
describe ".find_by_id (deprecated)" do
151+
around do |example|
152+
Vmdb::Deprecation.silence do
153+
example.run
154+
end
155+
end
156+
157+
it "finds a record by id" do
158+
expected = Object.new
159+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected)
160+
expect(base_class.find_by_id(5)).to eq(expected)
161+
end
162+
163+
it "returns nil when not found" do
164+
expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil)
165+
expect(base_class.find_by_id(5)).to be_nil
166+
end
167+
end
168+
169+
describe ".find_all_by_id (deprecated)" do
170+
around do |example|
171+
Vmdb::Deprecation.silence do
172+
example.run
173+
end
174+
end
175+
176+
it "finds record by ids" do
177+
expected = [Object.new, Object.new]
178+
expect(base_class).to receive(:search).with(:all, {:where => {:id => [5, 6]}}).and_return(expected)
179+
expect(base_class.find_all_by_id(5, 6)).to eq(expected)
180+
end
181+
182+
it "returns empty Array when none found" do
183+
expect(base_class).to receive(:search).with(:all, {:where => {:id => [5, 6]}}).and_return([])
184+
expect(base_class.find_all_by_id(5, 6)).to eq([])
185+
end
186+
end
89187
end

spec/lib/rbac/filterer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,7 @@ def get_rbac_results_for_and_expect_objects(klass, expected_objects)
26602660
end
26612661

26622662
it "support aaarm object" do
2663-
expect(VimPerformanceTrend).to receive(:find).with(:all, {:include => {:a => {}}}).and_return([:good])
2663+
expect(VimPerformanceTrend).to receive(:search).with(:all, {:includes => {:a => {}}, :references => {:a => {}}}).and_return([:good])
26642664
expect(described_class.filtered(VimPerformanceTrend, :include_for_find => {:a => {}}).to_a).to match_array([:good])
26652665
end
26662666

spec/models/pglogical_subscription_spec.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,35 @@
118118
expect(actual_attrs).to match_array(expected_attrs)
119119
end
120120

121-
it "supports find(:all) with records" do
122-
with_records
123-
actual_attrs = described_class.find(:all).map(&:attributes)
124-
expect(actual_attrs).to match_array(expected_attrs)
125-
end
126-
127121
it "retrieves no records with no records" do
128122
with_no_records
129123
expect(described_class.all).to be_empty
130-
expect(described_class.find(:all)).to be_empty
131124
end
132125
end
133126

134127
describe ".first" do
135128
it "retrieves the first record with records" do
136129
with_records
137-
rec = described_class.find(:first)
130+
rec = described_class.first
138131
expect(rec.attributes).to eq(expected_attrs.first)
139132
end
140133

141134
it "returns nil with no records" do
142135
with_no_records
143-
expect(described_class.find(:first)).to be_nil
136+
expect(described_class.first).to be_nil
144137
end
145138
end
146139

147140
describe ".last" do
148141
it "retrieves the last record with :last" do
149142
with_records
150-
rec = described_class.find(:last)
143+
rec = described_class.last
151144
expect(rec.attributes).to eq(expected_attrs.last)
152145
end
153146

154147
it "returns nil with :last" do
155148
with_no_records
156-
expect(described_class.find(:last)).to be_nil
149+
expect(described_class.last).to be_nil
157150
end
158151
end
159152

@@ -264,7 +257,7 @@
264257
with_records
265258
allow(MiqRegionRemote).to receive(:with_remote_connection).and_yield(double(:connection))
266259

267-
sub = described_class.find(:first)
260+
sub = described_class.first
268261
sub.host = "other-host.example.com"
269262
allow(sub).to receive(:assert_different_region!)
270263

@@ -289,7 +282,7 @@
289282
end
290283

291284
it "deletes all subscriptions if no parameter passed" do
292-
allow(described_class).to receive(:find).with(:all).and_return([subscription1, subscription2])
285+
allow(described_class).to receive(:search).with(:all, {}).and_return([subscription1, subscription2])
293286
expect(subscription1).to receive(:delete)
294287
expect(subscription2).to receive(:delete)
295288
described_class.delete_all
@@ -368,7 +361,7 @@
368361
end
369362

370363
describe "#delete" do
371-
let(:sub) { described_class.find(:first) }
364+
let(:sub) { described_class.first }
372365

373366
it "drops the subscription" do
374367
allow(pglogical).to receive(:subscriptions).and_return([subscriptions.first], [])
@@ -444,7 +437,7 @@
444437
it "validates existing subscriptions with new parameters" do
445438
allow(pglogical).to receive(:subscriptions).and_return([subscriptions.first])
446439

447-
sub = described_class.find(:first)
440+
sub = described_class.first
448441
expect(sub.host).to eq "example.com"
449442
expect(sub.port).to be_blank
450443
expect(sub.user).to eq "root"

0 commit comments

Comments
 (0)