From 924f5c535ba93c669cdaf172d7c2f8ff1c918b0e Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Fri, 22 Feb 2019 17:43:42 -0500 Subject: [PATCH 1/2] Rename `find_records` to allow detection of overrides that will break during upgrade --- .../active_relation_resource_finder.rb | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/jsonapi/active_relation_resource_finder.rb b/lib/jsonapi/active_relation_resource_finder.rb index 8a0406e65..f37774132 100644 --- a/lib/jsonapi/active_relation_resource_finder.rb +++ b/lib/jsonapi/active_relation_resource_finder.rb @@ -25,9 +25,8 @@ def find(filters, options = {}) paginator = options[:paginator] - records = find_records(records: records(options), - sort_criteria: sort_criteria, - filters: filters, + records = apply_request_settings_to_records(records: records(options), + sort_criteria: sort_criteria,filters: filters, join_manager: join_manager, paginator: paginator, options: options) @@ -45,7 +44,7 @@ def count(filters, options = {}) join_manager = JoinManager.new(resource_klass: self, filters: filters) - records = find_records(records: records(options), + records = apply_request_settings_to_records(records: records(options), filters: filters, join_manager: join_manager, options: options) @@ -100,7 +99,7 @@ def find_fragments(filters, options = {}) paginator = options[:paginator] - records = find_records(records: records(options), + records = apply_request_settings_to_records(records: records(options), filters: filters, sort_criteria: sort_criteria, paginator: paginator, @@ -229,7 +228,7 @@ def count_related(source_rid, relationship_name, options = {}) source_relationship: relationship, filters: filters) - records = find_records(records: records(options), + records = apply_request_settings_to_records(records: records(options), resource_klass: related_klass, primary_keys: source_rid.id, join_manager: join_manager, @@ -306,13 +305,13 @@ def to_one_relationships_for_linkage(include_related) end def find_record_by_key(key, options = {}) - record = find_records(records: records(options), primary_keys: key, options: options).first + record = apply_request_settings_to_records(records: records(options), primary_keys: key, options: options).first fail JSONAPI::Exceptions::RecordNotFound.new(key) if record.nil? record end def find_records_by_keys(keys, options = {}) - find_records(records: records(options), primary_keys: keys, options: options) + apply_request_settings_to_records(records: records(options), primary_keys: keys, options: options) end def find_related_monomorphic_fragments(source_rids, relationship, options, connect_source_identity) @@ -337,7 +336,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne paginator = options[:paginator] if source_rids.count == 1 - records = find_records(records: records(options), + records = apply_request_settings_to_records(records: records(options), resource_klass: resource_klass, sort_criteria: sort_criteria, primary_keys: source_ids, @@ -464,7 +463,7 @@ def find_related_polymorphic_fragments(source_rids, relationship, options, conne # Note: We will sort by the source table. Without using unions we can't sort on a polymorphic relationship # in any manner that makes sense - records = find_records(records: records(options), + records = apply_request_settings_to_records(records: records(options), resource_klass: resource_klass, sort_primary: true, primary_keys: source_ids, @@ -615,7 +614,7 @@ def find_related_polymorphic_fragments(source_rids, relationship, options, conne related_fragments end - def find_records(records:, + def apply_request_settings_to_records(records:, join_manager: JoinManager.new(resource_klass: self), resource_klass: self, filters: {}, From dd31121ca464994337245fc2894dbae35babd03d Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Fri, 22 Feb 2019 17:51:14 -0500 Subject: [PATCH 2/2] Add rake task to check the upgrade for orphaned overrides --- lib/jsonapi-resources.rb | 1 + lib/jsonapi/resources/railtie.rb | 9 ++++++ lib/tasks/check_upgrade.rake | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 lib/jsonapi/resources/railtie.rb create mode 100644 lib/tasks/check_upgrade.rake diff --git a/lib/jsonapi-resources.rb b/lib/jsonapi-resources.rb index bafa53788..76580e2a0 100644 --- a/lib/jsonapi-resources.rb +++ b/lib/jsonapi-resources.rb @@ -1,3 +1,4 @@ +require 'jsonapi/resources/railtie' require 'jsonapi/naive_cache' require 'jsonapi/compiled_json' require 'jsonapi/resource' diff --git a/lib/jsonapi/resources/railtie.rb b/lib/jsonapi/resources/railtie.rb new file mode 100644 index 000000000..a2d92c1c5 --- /dev/null +++ b/lib/jsonapi/resources/railtie.rb @@ -0,0 +1,9 @@ +module JSONAPI + module Resources + class Railtie < Rails::Railtie + rake_tasks do + load 'tasks/check_upgrade.rake' + end + end + end +end \ No newline at end of file diff --git a/lib/tasks/check_upgrade.rake b/lib/tasks/check_upgrade.rake new file mode 100644 index 000000000..34ddef4a6 --- /dev/null +++ b/lib/tasks/check_upgrade.rake @@ -0,0 +1,52 @@ +require 'rake' +require 'jsonapi-resources' + +namespace :jsonapi do + namespace :resources do + desc 'Checks application for orphaned overrides' + task :check_upgrade => :environment do + Rails.application.eager_load! + + resource_klasses = ObjectSpace.each_object(Class).select { |klass| klass < JSONAPI::Resource} + + puts "Checking #{resource_klasses.count} resources" + + issues_found = 0 + + klasses_with_deprecated = resource_klasses.select { |klass| klass.methods.include?(:find_records) } + unless klasses_with_deprecated.empty? + puts " Found the following resources the still implement `find_records`:" + klasses_with_deprecated.each { |klass| puts " #{klass}"} + puts " The `find_records` method is no longer called by JR. Please review and ensure your functionality is ported over." + + issues_found = issues_found + klasses_with_deprecated.length + end + + klasses_with_deprecated = resource_klasses.select { |klass| klass.methods.include?(:records_for) } + unless klasses_with_deprecated.empty? + puts " Found the following resources the still implement `records_for`:" + klasses_with_deprecated.each { |klass| puts " #{klass}"} + puts " The `records_for` method is no longer called by JR. Please review and ensure your functionality is ported over." + + issues_found = issues_found + klasses_with_deprecated.length + end + + klasses_with_deprecated = resource_klasses.select { |klass| klass.methods.include?(:apply_includes) } + unless klasses_with_deprecated.empty? + puts " Found the following resources the still implement `apply_includes`:" + klasses_with_deprecated.each { |klass| puts " #{klass}"} + puts " The `apply_includes` method is no longer called by JR. Please review and ensure your functionality is ported over." + + issues_found = issues_found + klasses_with_deprecated.length + end + + if issues_found > 0 + puts "Finished inspection. #{issues_found} issues found that may impact upgrading. Please address these issues. " + else + puts "Finished inspection with no issues found. Note this is only a cursory check for method overrides that will no \n" \ + "longer be called by JSONAPI::Resources. This check in no way assures your code will continue to function as \n" \ + "it did before the upgrade. Please do adequate testing before using in production." + end + end + end +end