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 runtime #3207

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions lib/avo/concerns/model_class_constantized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ 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
@model_class = value
end

# Cast the model class to a constantized version
def constantized_model_class
@constantized_model_class ||= case @model_class
when Class
value
@model_class
when String, Symbol
value.to_s.safe_constantize
@model_class.to_s.safe_constantize
else
raise ArgumentError.new "Failed to find a proper model class for #{self}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ 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?
return constantized_model_class if @model_class.present?

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