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

prefer Active Support's String#safe_constantize to #constantize #297

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
18 changes: 9 additions & 9 deletions lib/associations/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def has_many(association_id, **options)
klass_name = association_id.to_s.classify
klass =
begin
klass_name.constantize
klass_name.safe_constantize
rescue StandardError, LoadError
nil
end
Expand All @@ -35,7 +35,7 @@ def belongs_to(name, scope = nil, **options)
klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize
klass =
begin
klass_name.constantize
klass_name.safe_constantize
rescue StandardError, LoadError
nil
end
Expand All @@ -55,11 +55,11 @@ def belongs_to_active_hash(association_id, options = {})
:shortcuts => []
}.merge(options)
# Define default primary_key with provided class_name if any
options[:primary_key] ||= options[:class_name].constantize.primary_key
options[:primary_key] ||= options[:class_name].safe_constantize.primary_key
options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array)

define_method(association_id) do
options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
options[:class_name].safe_constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
end

define_method("#{association_id}=") do |new_value|
Expand All @@ -72,7 +72,7 @@ def belongs_to_active_hash(association_id, options = {})
end

define_method("#{association_id}_#{shortcut}=") do |new_value|
send "#{association_id}=", new_value ? options[:class_name].constantize.send("find_by_#{shortcut}", new_value) : nil
send "#{association_id}=", new_value ? options[:class_name].safe_constantize.send("find_by_#{shortcut}", new_value) : nil
end
end

Expand Down Expand Up @@ -109,7 +109,7 @@ def belongs_to_active_hash(association_id, options = {})
:belongs_to,
association_id.to_sym,
options,
options[:class_name].constantize
options[:class_name].safe_constantize
)
end
end
Expand All @@ -129,7 +129,7 @@ def has_many(association_id, options = {})
:primary_key => self.class.primary_key
}.merge(options)

klass = options[:class_name].constantize
klass = options[:class_name].safe_constantize
primary_key_value = send(options[:primary_key])
foreign_key = options[:foreign_key].to_sym

Expand All @@ -155,7 +155,7 @@ def has_one(association_id, options = {})
:primary_key => self.class.primary_key
}.merge(options)

scope = options[:class_name].constantize
scope = options[:class_name].safe_constantize

if scope.respond_to?(:scoped) && options[:conditions]
scope = scope.scoped(:conditions => options[:conditions])
Expand All @@ -174,7 +174,7 @@ def belongs_to(association_id, options = {})
field options[:foreign_key].to_sym

define_method(association_id) do
options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
options[:class_name].safe_constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
end

define_method("#{association_id}=") do |new_value|
Expand Down