Skip to content

Commit

Permalink
Merge pull request #40 from patterninc/master_rails_7_preloader_api
Browse files Browse the repository at this point in the history
Add patch for the rails 7 preloader api
  • Loading branch information
gaurav-pattern authored Dec 20, 2022
2 parents d57d48e + 057f9c4 commit 00cc7a5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/query_helper/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ def self.process_association_params(associations)
def self.load_associations(payload:, associations: [], as_json_options: {})
as_json_options ||= {}
as_json_options[:include] = as_json_options[:include] || json_associations(associations)
ActiveRecord::Associations::Preloader.new.preload(payload, associations)
begin
# Preloader have been changed in Rails 7, which is giving error on upgrade.
# This should be updated to latest API once all repos have been upgraded to Rails 7
ActiveRecord::Associations::Preloader.new.preload(payload, associations)
rescue
ActiveRecord::Associations::Preloader.new(records: payload, associations: associations).call
end
payload.as_json(as_json_options)
end

def self.preload_associations(payload:, preload: [])
ActiveRecord::Associations::Preloader.new.preload(payload, preload)
begin
# Preloader have been changed in Rails 7, which is giving error on upgrade.
# This should be updated to latest API once all repos have been upgraded to Rails 7
ActiveRecord::Associations::Preloader.new.preload(payload, preload)
rescue
ActiveRecord::Associations::Preloader.new(records: payload, associations: preload).call
end
end

def self.json_associations(associations)
Expand Down

0 comments on commit 00cc7a5

Please sign in to comment.