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

fix: action visibility on show controls #1833

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 13 additions & 11 deletions app/components/avo/views/resource_show_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@
<% end %>
<% end %>
<% elsif control.action? %>
<%= a_link control.path,
color: control.color,
style: control.style,
icon: control.icon,
title: control.title,
data: {
tippy: control.title ? :tooltip : nil,
'turbo-frame': 'actions_show',
'action': 'click->actions-picker#visitAction',
} do %>
<%= control.label %>
<% if control.action.visible_in_view(parent_resource: @parent_resource) %>
<%= a_link control.path,
color: control.color,
style: control.style,
icon: control.icon,
title: control.title,
data: {
tippy: control.title ? :tooltip : nil,
'turbo-frame': 'actions_show',
'action': 'click->actions-picker#visitAction',
} do %>
<%= control.label %>
<% end %>
<% end %>
<% elsif control.link_to? %>
<%= a_link control.path,
Expand Down
11 changes: 8 additions & 3 deletions lib/avo/resources/controls/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ module Controls
class Action < BaseControl
attr_reader :klass

def initialize(klass, model: nil, resource: nil, view: nil, **args)
def initialize(klass, record: nil, resource: nil, view: nil, **args)
super(**args)

@klass = klass
@resource = resource
@model = model
@record = record
@view = view
end

def action
@instance ||= @klass.new(model: @model, resource: @resource, view: @view)
@instance ||= @klass.new(
model: @record,
resource: @resource,
view: @view,
arguments: @resource.get_action_arguments(klass)
)
end

def path
Expand Down
20 changes: 20 additions & 0 deletions spec/features/avo/custom_resource_controls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,25 @@
expect(page).to have_link "", href: /\/admin\/resources\/fish\/#{fish.id}\/edit/
end
end

it "runs visible block on show controls actions" do
visit "/admin/resources/fish/#{fish.id}"

expect(page).to have_link "Release fish", href: /\/admin\/resources\/fish\/#{fish.id}\/actions\?action_id=ReleaseFish/

ReleaseFish.define_singleton_method(:visible) do
define_method(:visible) do
-> {
false
}
end
end

visit "/admin/resources/fish/#{fish.id}"

expect(page).not_to have_link "Release fish", href: /\/admin\/resources\/fish\/#{fish.id}\/actions\?action_id=ReleaseFish/

ReleaseFish.singleton_class.send(:undef_method, :visible)
end
end
end