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

refactor: reflect_on_association instead _reflections #3069

Merged
merged 6 commits into from
Jul 30, 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
2 changes: 1 addition & 1 deletion app/components/avo/fields/has_one_field/show_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def can_see_the_create_button?
end

def create_path
association_id = @field.resource.model_class._reflections.with_indifferent_access[@field.association_name].inverse_of.name
association_id = @field.resource.model_class.reflect_on_association(@field.association_name).inverse_of.name

args = {
via_relation: association_id,
Expand Down
6 changes: 5 additions & 1 deletion app/components/avo/index/resource_controls_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ def parent_resource
end

def is_has_many_association?
@reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
@reflection.class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection,
ActiveRecord::Reflection::ThroughReflection
]
end

def referrer_path
Expand Down
15 changes: 13 additions & 2 deletions app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ def can_see_the_create_button?
end

def can_attach?
return false if @reflection.blank?
return false if has_reflection_and_is_read_only

klass = @reflection
klass = @reflection.through_reflection if klass.is_a? ::ActiveRecord::Reflection::ThroughReflection

@reflection.present? && klass.is_a?(::ActiveRecord::Reflection::HasManyReflection) && !has_reflection_and_is_read_only && authorize_association_for(:attach)
return false unless klass.class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]

authorize_association_for(:attach)
end

def create_path
Expand All @@ -82,7 +90,10 @@ def create_path
via_record_id: @parent_record.to_param
}

if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
if @reflection.class.in? [
ActiveRecord::Reflection::ThroughReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]
args[:via_relation] = params[:resource_name]
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def related_resource

return field.use_resource if field&.use_resource.present?

reflection = @record._reflections.with_indifferent_access[field&.for_attribute || params[:related_name]]
reflection = @record.class.reflect_on_association(field&.for_attribute || params[:related_name])

reflected_model = reflection.klass

Expand Down
12 changes: 8 additions & 4 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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?
Copy link
Contributor

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!

reflection_class.in? ["HasManyReflection", "HasAndBelongsToManyReflection"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that above some mentions are classes and these are strings.
Is there anything we can do to make them the same or do we have to know when to call as a string and as a class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. There is no reason to use class.name.demodulize.to_s and check against a string.

end
end
end
4 changes: 2 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def new
def create
# This means that the record has been created through another parent record and we need to attach it somehow.
if params[:via_record_id].present? && params[:via_belongs_to_resource_class].nil?
@reflection = @record._reflections.with_indifferent_access[params[:via_relation]]
@reflection = @record.class.reflect_on_association(params[:via_relation])
# Figure out what kind of association does the record have with the parent record

# Fills in the required info for belongs_to and has_many
Expand All @@ -134,7 +134,7 @@ def create
end

# For when working with has_one, has_one_through, has_many_through, has_and_belongs_to_many, polymorphic
if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) || @reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
# find the record
via_resource = Avo.resource_manager.get_resource_by_model_class(params[:via_relation_class])
@related_record = via_resource.find_record params[:via_record_id], params: params
Expand Down
2 changes: 1 addition & 1 deletion lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def target_resource
else
reflection_key = polymorphic_as || id

reflection_object = @record._reflections.with_indifferent_access[reflection_key]
reflection_object = @record.class.reflect_on_association(reflection_key)

if reflection_object.klass.present?
get_resource_by_model_class(reflection_object.klass.to_s)
Expand Down
2 changes: 1 addition & 1 deletion lib/avo/fields/has_base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def field_label
end

def target_resource
reflection = @record._reflections.with_indifferent_access[association_name]
reflection = @record.class.reflect_on_association(association_name)

if reflection.klass.present?
get_resource_by_model_class(reflection.klass.to_s)
Expand Down
4 changes: 2 additions & 2 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def authorization
end

def valid_association_name(record, association_name)
association_name if record._reflections.with_indifferent_access[association_name].present?
association_name if record.class.reflect_on_association(association_name).present?
end

def valid_attachment_name(record, association_name)
Expand Down Expand Up @@ -509,7 +509,7 @@ def hydrate_model_with_default_values

if field.type == "belongs_to"

reflection = @record._reflections.with_indifferent_access[@params[:via_relation]]
reflection = @record.class.reflect_on_association(@params[:via_relation]) if @params[:via_relation].present?

if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
# set the value to the actual record
Expand Down
Loading