Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
zedtux committed Sep 10, 2021
1 parent a8b2b7f commit 009c340
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions lib/no_brainer/document/association/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def foreign_key
def foreign_type
return nil unless options[:polymorphic]

options[:foreign_type].try(:to_sym) || (target_name && :"#{target_name}_type")
options[:foreign_type].try(:to_sym) || (:"#{target_name}_type")
end

def primary_key
Expand All @@ -42,8 +42,11 @@ def primary_key
def target_model(target_class = nil)
return if options[:polymorphic] && target_class.nil?

model_name = options[:class_name] || target_name.to_s.camelize
model_name = target_class if options[:polymorphic]
model_name = if options[:polymorphic]
target_class
else
options[:class_name] || target_name.to_s.camelize
end

get_model_by_name(model_name)
end
Expand Down Expand Up @@ -121,9 +124,10 @@ def polymorphic_read
return target if loaded?

target_class = owner.read_attribute(foreign_type)
fk = owner.read_attribute(foreign_key)

if fk = owner.read_attribute(foreign_key)
preload(base_criteria(target_class)&.where(primary_key => fk)&.first)
if target_class && fk
preload(base_criteria(target_class).where(primary_key => fk).first)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/no_brainer/document/association/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def hook

if options[:scope]
raise ":scope must be passed a lambda like this: `:scope => ->{ where(...) }'" unless options[:scope].is_a?(Proc)
raise ':dependent and :scope cannot be used together' if options[:dependent]
raise ":dependent and :scope cannot be used together" if options[:dependent]
end

if options[:dependent]
unless [:destroy, :delete, :nullify, :restrict, nil].include?(options[:dependent])
raise "Invalid dependent option: `#{options[:dependent].inspect}'. " \
'Valid options are: :destroy, :delete, :nullify, or :restrict'
"Valid options are: :destroy, :delete, :nullify, or :restrict"
end
add_callback_for(:before_destroy)
end
Expand Down

0 comments on commit 009c340

Please sign in to comment.