-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
refactor: reflect_on_association
instead _reflections
#3069
Changes from 2 commits
e7fdc6d
e8b1eb1
d48b380
0025b6e
2677f29
72bfc37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ def create_association | |
association_name = BaseResource.valid_association_name(@record, association_from_params) | ||
|
||
perform_action_and_record_errors do | ||
if reflection_class == "HasManyReflection" | ||
if has_many_reflection? | ||
@record.send(association_name) << @attachment_record | ||
else | ||
@record.send(:"#{association_name}=", @attachment_record) | ||
|
@@ -92,7 +92,7 @@ def destroy | |
|
||
if reflection.instance_of? ActiveRecord::Reflection::ThroughReflection | ||
join_record.destroy! | ||
elsif reflection_class == "HasManyReflection" | ||
elsif has_many_reflection? | ||
@record.send(association_name).delete @attachment_record | ||
else | ||
@record.send(:"#{association_name}=", nil) | ||
|
@@ -106,7 +106,7 @@ def destroy | |
private | ||
|
||
def set_reflection | ||
@reflection = @record._reflections.with_indifferent_access[association_from_params] | ||
@reflection = @record.class.reflect_on_association(association_from_params) | ||
end | ||
|
||
def set_attachment_class | ||
|
@@ -132,7 +132,7 @@ def attachment_id | |
end | ||
|
||
def reflection_class | ||
reflection = @record._reflections.with_indifferent_access[association_from_params] | ||
reflection = @record.class.reflect_on_association(association_from_params) | ||
|
||
klass = reflection.class.name.demodulize.to_s | ||
klass = reflection.through_reflection.class.name.demodulize.to_s if klass == "ThroughReflection" | ||
|
@@ -188,5 +188,9 @@ def join_record | |
reflection.through_reflection.klass.find_by(source_foreign_key => @attachment_record.id, | ||
through_foreign_key => @record.id) | ||
end | ||
|
||
def has_many_reflection? | ||
reflection_class.in? ["HasManyReflection", "HasAndBelongsToManyReflection"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that above some mentions are classes and these are strings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. There is no reason to use |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noice extracting this method! LGTM!