Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: constantize model_class on reader #3207

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions lib/avo/concerns/model_class_constantized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ module ModelClassConstantized
class_methods do
# Cast the model class to a constantized version and memoize it like that
def model_class=(value)
@model_class = case value
when Class
value
when String, Symbol
value.to_s.safe_constantize
else
raise ArgumentError.new "Failed to find a proper model class for #{self}"
end
@model_class = value
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,17 @@ def get_model_by_name(model_name)
# where we figure out the model class from the record
def model_class(record_class: nil)
# get the model class off of the static property
return @model_class if @model_class.present?
if @model_class.present?
# Cast the model class to a constantized version
return case @model_class
when Class
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
@model_class
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
when String, Symbol
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
@model_class.to_s.safe_constantize
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
else
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
raise ArgumentError.new "Failed to find a proper model class for #{self}"
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
end
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
end

# get the model class off of the record for STI models
return record_class if record_class.present?
Expand Down
Loading