From 6d867de20f122687247c57e201aa41f8e912ccb4 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Fri, 22 Jan 2021 02:39:58 +0000 Subject: [PATCH 1/4] Add way for controllers with different models to use the only parameter The /emails endpoint was passing in the "Email" model because that's how the emails controller classifies. This was to fix that, and to allow any other such cases in the future. --- app/controllers/application_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 13d1cb4f2..a9fb6e806 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -35,8 +35,10 @@ class ApplicationController < ActionController::Base return end + model = options[0]&.delete(:model) if subject.respond_to?(:includes) && (request.format.json? || request.format.xml?) - associations = ParameterBuilder.includes_parameters(params[:only], model_name) + model ||= model_name + associations = ParameterBuilder.includes_parameters(params[:only], model) subject = subject.includes(associations) end From 569884707755eba45f703dd4b9c696a8dfee4c7e Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Fri, 22 Jan 2021 02:43:53 +0000 Subject: [PATCH 2/4] Add in missing available includes --- app/controllers/emails_controller.rb | 2 +- app/models/email_address.rb | 4 ++++ app/models/forum_topic_visit.rb | 4 ++++ app/models/tag_implication.rb | 4 ++++ app/models/user_event.rb | 4 ++++ 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/emails_controller.rb b/app/controllers/emails_controller.rb index 2f14ad9c3..117567467 100644 --- a/app/controllers/emails_controller.rb +++ b/app/controllers/emails_controller.rb @@ -4,7 +4,7 @@ class EmailsController < ApplicationController def index @email_addresses = authorize EmailAddress.visible(CurrentUser.user).paginated_search(params, count_pages: true) @email_addresses = @email_addresses.includes(:user) - respond_with(@email_addresses) + respond_with(@email_addresses, model: "EmailAddress") end def show diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 7d437751d..b5cdc6ce7 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -82,4 +82,8 @@ class EmailAddress < ApplicationRecord id == verifier.verified(key) end end + + def self.available_includes + [:user] + end end diff --git a/app/models/forum_topic_visit.rb b/app/models/forum_topic_visit.rb index 8fe1ac8ac..e9c3d4382 100644 --- a/app/models/forum_topic_visit.rb +++ b/app/models/forum_topic_visit.rb @@ -10,4 +10,8 @@ class ForumTopicVisit < ApplicationRecord q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic_id, :last_read_at) q.apply_default_order(params) end + + def self.available_includes + [:forum_topic] + end end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index df0e4be3a..c0bf11992 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -150,4 +150,8 @@ class TagImplication < TagRelationship end end end + + def self.available_includes + super + [:child_implications, :parent_implications] + end end diff --git a/app/models/user_event.rb b/app/models/user_event.rb index ce700a3a4..bb9b37006 100644 --- a/app/models/user_event.rb +++ b/app/models/user_event.rb @@ -37,6 +37,10 @@ class UserEvent < ApplicationRecord q end + def self.available_includes + [:user, :user_session] + end + concerning :ConstructorMethods do class_methods do # Build an event but don't save it yet. The caller is expected to update the user, which will save the event. From d06236c66ba57eb037448e5f7aa164133e160884 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Fri, 22 Jan 2021 02:44:47 +0000 Subject: [PATCH 3/4] Remove unneeded available includes The indexes for these only contain instances from the user regardless of user level, so there's no need to show these. --- app/models/saved_search.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index a6efcedc6..4ebe3c056 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -168,8 +168,4 @@ class SavedSearch < ApplicationRecord def disable_labels=(value) user.update(disable_categorized_saved_searches: true) if value.to_s.truthy? end - - def self.available_includes - [:user] - end end From db68644b65a5f8f54ed8bf5fabaa453c93a40a10 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Fri, 22 Jan 2021 02:54:32 +0000 Subject: [PATCH 4/4] Convert forum topic visit association attribute to new format This was added in da3e8e4. --- app/models/forum_topic_visit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/forum_topic_visit.rb b/app/models/forum_topic_visit.rb index e9c3d4382..d484041e6 100644 --- a/app/models/forum_topic_visit.rb +++ b/app/models/forum_topic_visit.rb @@ -7,7 +7,7 @@ class ForumTopicVisit < ApplicationRecord end def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic_id, :last_read_at) + q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic, :last_read_at) q.apply_default_order(params) end