Skip to content

Commit

Permalink
fix: don't render detach button when can't detach record
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Jul 19, 2023
1 parent 967b3db commit e1f6caa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 1 addition & 4 deletions app/components/avo/index/resource_controls_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ def initialize(resource: nil, reflection: nil, parent_model: nil, parent_resourc
end

def can_detach?
@reflection.present? &&
@resource.model.present? &&
is_has_many_association &&
authorize_association_for(:detach)
is_has_many_association ? super : false
end

def can_edit?
Expand Down
9 changes: 8 additions & 1 deletion app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ def can_delete?
end

def can_detach?
authorize_association_for(:detach)
return false if @reflection.blank? && @resource.model.blank? && !authorize_association_for(:detach)

# If the inverse_of is a belongs_to, we need to check if it's optional in order to know if we can detach it.
if @reflection.inverse_of.is_a?(ActiveRecord::Reflection::BelongsToReflection)
@reflection.inverse_of.options[:optional]
else
true
end
end

def detach_path
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def destroy
association_name = BaseResource.valid_association_name(@model, params[:related_name])

if reflection_class == "HasManyReflection"
@model.send(association_name).delete @attachment_model
@model.send(association_name).destroy @attachment_model
else
@model.send("#{association_name}=", nil)
end
Expand Down

0 comments on commit e1f6caa

Please sign in to comment.