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

feature: allow params on resource menu item #1957

Merged
merged 2 commits into from
Oct 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= render Avo::Sidebar::LinkComponent.new label: item.name, path: item.path, target: item.target, data: item.data, icon: item.icon %>
<% end %>
<% if item.is_a? Avo::Menu::Resource %>
<%= render Avo::Sidebar::LinkComponent.new label: item.navigation_label, path: helpers.resources_path(resource: resource), data: item.data, icon: item.icon %>
<%= render Avo::Sidebar::LinkComponent.new label: item.navigation_label, path: helpers.resources_path(resource: resource, **item.fetch_params), data: item.data, icon: item.icon %>
<% end %>
<% if item.is_a? Avo::Menu::Dashboard %>
<%= render Avo::Sidebar::LinkComponent.new label: item.navigation_label, path: dashboard.navigation_path, data: item.data, icon: item.icon %>
Expand Down
10 changes: 9 additions & 1 deletion lib/avo/menu/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ class Avo::Menu::Resource < Avo::Menu::BaseItem

option :resource
option :label, optional: true
option :params, default: proc { {} }

def parsed_resource
Avo::App.guess_resource resource.to_s
@parsed_resource ||= Avo::App.guess_resource resource.to_s
end

def entity_label
parsed_resource.navigation_label
end

def fetch_params
Avo::ExecutionContext.new(
target: params,
resource: parsed_resource
).handle
end
end
6 changes: 5 additions & 1 deletion spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@
end

group "People", collapsable: true do
resource "UserResource", visible: -> do
resource "UserResource", params: -> do
decoded_filter = {"IsAdmin"=>["non_admins"]}

{ filters: Avo::Filters::BaseFilter.encode_filters(decoded_filter)}
end, visible: -> do
authorize current_user, User, "index?", raise_exception: false
end
resource :people
Expand Down
7 changes: 6 additions & 1 deletion spec/features/avo/menu_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
end

group "People" do
resource "UserResource"
resource "UserResource", params: { filters: "eyJJc0FkbWluIjpbIm5vbl9hZG1pbnMiXX0%3D%0A" }
resource :people
resource :spouses
end
Expand Down Expand Up @@ -83,5 +83,10 @@
expect(subject.items.last.name).to eq "JSP"
expect(subject.items.last.path).to eq "https://jumpstartrails.com/"
expect(subject.items.last.target).to eq :_blank

# Checking path generated when :params used
users = subject.items.second.items[3].items.first
expect(users.resource).to eq "UserResource"
expect(users.params).to eq filters: "eyJJc0FkbWluIjpbIm5vbl9hZG1pbnMiXX0%3D%0A"
end
end
15 changes: 15 additions & 0 deletions spec/features/avo/menu_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails_helper"

RSpec.describe "menu item behaviour", type: :feature do
it "allows navigation via menu items" do
visit "/"

all("a", text: "Projects")[1].click

expect(page).to have_current_path("/admin/resources/projects")

all("a", text: "Users")[1].click

expect(page).to have_current_path("/admin/resources/users?filters=eyJJc0FkbWluIjpbIm5vbl9hZG1pbnMiXX0%3D%0A")
end
end
Loading