Skip to content

Commit

Permalink
Remove from_get_recordset behavior to avoid using rest request.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Feb 16, 2023
1 parent 92f4b38 commit f39a529
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
20 changes: 3 additions & 17 deletions 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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions lib/rest_framework/errors.rb
Expand Up @@ -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
@@ -1,6 +1,7 @@
class TestApi::MarblesWithAddedSelectController < TestApiController
include RESTFramework::ModelControllerMixin

self.model = Marble
self.native_serializer_config = {except: [:price]}

def get_recordset
Expand Down

0 comments on commit f39a529

Please sign in to comment.