From f39a529893409194eb271584d6d4550e899310bb Mon Sep 17 00:00:00 2001 From: "Gregory N. Schmit" Date: Thu, 16 Feb 2023 12:41:30 -0600 Subject: [PATCH] Remove from_get_recordset behavior to avoid using rest request. --- .../controller_mixins/models.rb | 20 +++---------------- lib/rest_framework/errors.rb | 16 +++++++++++++++ .../marbles_with_added_select_controller.rb | 1 + 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/rest_framework/controller_mixins/models.rb b/lib/rest_framework/controller_mixins/models.rb index d6a2338..b5ba1e5 100644 --- a/lib/rest_framework/controller_mixins/models.rb +++ b/lib/rest_framework/controller_mixins/models.rb @@ -1,8 +1,6 @@ require_relative "base" require_relative "../filters" -require "action_controller/test_case" - # This module provides the core functionality for controllers based on models. module RESTFramework::BaseModelControllerMixin include RESTFramework::BaseControllerMixin @@ -73,7 +71,7 @@ module ClassMethods IGNORE_VALIDATORS_WITH_KEYS = [:if, :unless].freeze # Get the model for this controller. - def get_model(from_get_recordset: false) + def get_model return @model if @model return (@model = self.model) if self.model @@ -83,19 +81,7 @@ def get_model(from_get_recordset: false) rescue NameError end - # Delegate to the recordset's model, if it's defined. This option prevents infinite recursion. - unless from_get_recordset - # Instantiate a new controller to get the recordset. - controller = self.new - controller.request = ActionController::TestRequest.new({}, {}, controller) - controller.params = {} - - if (recordset = controller.get_recordset) - return @model = recordset.klass - end - end - - return nil + raise RESTFramework::UnknownModelError, self end # Override `get_label` to include ActiveRecord i18n-translated column names. @@ -490,7 +476,7 @@ def get_recordset return (@recordset = self.class.recordset) if self.class.recordset # If there is a model, return that model's default scope (all records by default). - if (model = self.class.get_model(from_get_recordset: true)) + if (model = self.class.get_model) return @recordset = model.all end diff --git a/lib/rest_framework/errors.rb b/lib/rest_framework/errors.rb index 9c39ba6..1280ef3 100644 --- a/lib/rest_framework/errors.rb +++ b/lib/rest_framework/errors.rb @@ -13,3 +13,19 @@ def message MSG end end + +class RESTFramework::UnknownModelError < RESTFramework::Error + def initialize(controller_class) + super() + @controller_class = controller_class + end + + def message + return <<~MSG.split("\n").join(" ") + The model class for `#{@controller_class}` could not be determined. Any controller that + includes `RESTFramework::BaseModelControllerMixin` (directly or indirectly) must either set + the `model` attribute on the controller, or the model must be deducible from the controller + name (e.g., `UsersController` could resolve to the `User` model). + MSG + end +end diff --git a/test/app/controllers/test_api/marbles_with_added_select_controller.rb b/test/app/controllers/test_api/marbles_with_added_select_controller.rb index e9f140b..f3f4900 100644 --- a/test/app/controllers/test_api/marbles_with_added_select_controller.rb +++ b/test/app/controllers/test_api/marbles_with_added_select_controller.rb @@ -1,6 +1,7 @@ class TestApi::MarblesWithAddedSelectController < TestApiController include RESTFramework::ModelControllerMixin + self.model = Marble self.native_serializer_config = {except: [:price]} def get_recordset