-
Notifications
You must be signed in to change notification settings - Fork 919
[WIP] Switch ActsAsArModel to be QueryRelation based by default #23618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,22 +16,20 @@ class PglogicalSubscription < ActsAsArModel | |
| :provider_region_name => :string | ||
| ) | ||
|
|
||
| def self.find(*args) | ||
| case args.first | ||
| def self.search(mode, args) | ||
| case mode | ||
| when :all then find_all | ||
| when :first, :last then find_one(args.first) | ||
| else find_id(args.first) | ||
| when :first, :last then find_one(mode) | ||
| else find_id(mode) | ||
| end | ||
| end | ||
|
|
||
| def self.lookup_by_id(to_find) | ||
| find(to_find) | ||
| find_id(to_find) | ||
| rescue ActiveRecord::RecordNotFound | ||
| nil | ||
| end | ||
|
|
||
| singleton_class.send(:alias_method, :find_by_id, :lookup_by_id) | ||
| Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_id => :lookup_by_id) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This deprecation was moved into the base class.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we use |
||
|
|
||
| def save!(reload_failover_monitor = true) | ||
| assert_different_region! | ||
|
|
@@ -73,7 +71,7 @@ def delete(reload_failover_monitor_service = true) | |
| end | ||
|
|
||
| def self.delete_all(list = nil) | ||
| (list.nil? ? find(:all) : list)&.each { |sub| sub.delete(false) } | ||
| (list.nil? ? all : list)&.each { |sub| sub.delete(false) } | ||
| EvmDatabase.restart_failover_monitor_service_queue | ||
| nil | ||
| end | ||
|
|
@@ -189,7 +187,7 @@ def self.find_id(to_find) | |
| subscriptions.each do |s| | ||
| return new(subscription_to_columns(s)) if s.symbolize_keys[:subscription_name] == to_find | ||
| end | ||
| raise ActiveRecord::RecordNotFound, "Coundn't find subscription with id #{to_find}" | ||
| raise ActiveRecord::RecordNotFound, "Couldn't find subscription with id #{to_find}" | ||
| end | ||
| private_class_method :find_id | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| require 'query_relation' | ||
|
|
||
| class ActsAsArModel | ||
| include Vmdb::Logging | ||
| include ArVisibleAttribute | ||
|
|
@@ -121,6 +123,7 @@ def self.set_columns_hash(hash) | |
| include ActiveRecord::VirtualAttributes::VirtualFields | ||
|
|
||
| include AttributeBag | ||
| extend QueryRelation::Queryable | ||
|
|
||
| def self.instances_are_derived? | ||
| true | ||
|
|
@@ -146,59 +149,49 @@ def self.reflections | |
| # | ||
| # Find routines | ||
| # | ||
| def self.find(*_args) | ||
| raise NotImplementedError, _("find must be implemented in a subclass") | ||
| def self.find(mode_or_id, *args) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really think we can drop this. The only place we use it is in PgLogical (as mentioned in the previous list). This will remove legacy_options and a lot of other stuff. (Not sure the legacy options even work in current versions of Rails.) |
||
| if %i[all first last].include?(mode_or_id) | ||
| Vmdb::Deprecation.warn("find(:#{mode_or_id}) is deprecated (use #{mode_or_id} instead)") | ||
| search(mode_or_id, from_legacy_options(args.extract_options!)) | ||
| else | ||
| lookup_by_id(mode_or_id, *args).tap do |record| | ||
| raise ActiveRecord::RecordNotFound, "Couldn't find #{self} with id=#{mode_or_id}" if record.nil? | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # This method is called by QueryRelation upon executing the query. | ||
| # Since it will receive non-legacy search options, we need to convert | ||
| # them to handle the legacy find. | ||
| def self.search(mode, options = {}) | ||
| find(mode, to_legacy_options(options)) | ||
| def self.search(mode, options) | ||
| raise NotImplementedError, "must be defined in a subclass" | ||
| end | ||
| private_class_method :search | ||
|
|
||
| def self.to_legacy_options(options) | ||
| def self.from_legacy_options(options) | ||
| { | ||
| :conditions => options[:where], | ||
| :include => options[:includes], | ||
| :limit => options[:limit], | ||
| :order => options[:order], | ||
| :offset => options[:offset], | ||
| :select => options[:select], | ||
| :group => options[:group], | ||
| :where => options[:conditions], | ||
| :includes => options[:include], | ||
| :limit => options[:limit], | ||
| :order => options[:order], | ||
| :offset => options[:offset], | ||
| :select => options[:select], | ||
| :group => options[:group], | ||
| }.delete_blanks | ||
|
Comment on lines
-161
to
176
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, think we can drop support for We have no callers and only class implements this interface |
||
| end | ||
| private_class_method :to_legacy_options | ||
|
|
||
| def self.all(*args) | ||
| require 'query_relation' | ||
| QueryRelation.new(self, *args) | ||
| end | ||
|
|
||
| def self.first(*args) | ||
| find(:first, *args) | ||
| end | ||
|
|
||
| def self.last(*args) | ||
| find(:last, *args) | ||
| end | ||
|
|
||
| def self.count(*args) | ||
| all(*args).count | ||
| end | ||
| private_class_method :from_legacy_options | ||
|
|
||
| def self.find_by_id(*id) | ||
| options = id.extract_options! | ||
| options[:conditions] = {:id => id.first} | ||
| first(options) | ||
| options[:where] = {:id => id.first} | ||
| search(:first, options) | ||
| end | ||
|
|
||
| singleton_class.send(:alias_method, :lookup_by_id, :find_by_id) | ||
| Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_id => :lookup_by_id) | ||
|
|
||
| def self.find_all_by_id(*ids) | ||
| options = ids.extract_options! | ||
| options[:conditions] = {:id => ids.flatten} | ||
| all(options) | ||
| options[:where] = {:id => ids.flatten} | ||
| search(:all, options) | ||
| end | ||
| Vmdb::Deprecation.deprecate_methods(singleton_class, :find_all_by_id => "use where(:id => ids) instead") | ||
|
|
||
| # | ||
| # Methods pulled from ActiveRecord 2.3.8 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.