Skip to content

Commit c04ed9b

Browse files
feature: add search_result_url and search_policy_class (#1634)
Co-authored-by: Adrian <[email protected]>
1 parent 49a6558 commit c04ed9b

File tree

19 files changed

+119
-11
lines changed

19 files changed

+119
-11
lines changed

CONTRIBUTING.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ gem 'avo', path: '../avo'
6868

6969
Avo's assets will not show up by default, resulting in 404 errors on `/avo-assets/avo.base.js` and `/avo-assets/avo.base.css`. To avoid this, you need to compile the asset bundles, and symlink them into `public/avo-assets`.
7070

71+
First, make sure you have `yarn` installed and then install Avo's dependencies:
72+
```bash
73+
yarn install
74+
```
75+
76+
Run the first build to generate the files `app/assets/builds/avo.base.js` and `app/assets/builds/avo.base.css`:
77+
78+
```bash
79+
yarn build
80+
```
81+
7182
Create symlinks for compiled assets into the `public` directory. You'll only need to do this once.
7283

7384
```bash

app/components/avo/views/resource_index_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<% end %>
3232
<% c.body do %>
3333
<div class="flex flex-col xs:flex-row xs:justify-between space-y-2 xs:space-y-0 py-4 <%= 'hidden' if @resource.search_query.nil? && @filters.empty? && available_view_types.count <= 1 %>">
34-
<% unless hide_search_input %>
34+
<% if show_search_input %>
3535
<div class="flex items-center px-4 w-64">
3636
<%= render partial: 'avo/partials/resource_search', locals: {resource: @resource.route_key, via_reflection: via_reflection} %>
3737
</div>

app/components/avo/views/resource_index_component.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,19 @@ def description
124124
@resource.resource_description
125125
end
126126

127-
def hide_search_input
128-
return true unless @resource.search_query.present?
127+
def show_search_input
128+
return false unless authorized_to_search?
129+
return false unless @resource.search_query.present?
130+
return false if field&.hide_search_input
129131

130-
field&.hide_search_input || false
132+
true
133+
end
134+
135+
def authorized_to_search?
136+
# Hide the search if the authorization prevents it
137+
return true unless @resource.authorization.has_action_method?("search")
138+
139+
@resource.authorization.authorize_action("search", raise_exception: false)
131140
end
132141

133142
private

app/controllers/avo/search_controller.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def search_resources(resources)
2727
resources
2828
.map do |resource|
2929
# Apply authorization
30-
next unless @authorization.set_record(resource.model_class).authorize_action(:index, raise_exception: false)
30+
next unless @authorization.set_record(resource.model_class).authorize_action(:search, raise_exception: false)
31+
3132
# Filter out the models without a search_query
3233
next if resource.search_query.nil?
3334

@@ -120,10 +121,16 @@ def apply_search_metadata(models, avo_resource)
120121
models.map do |model|
121122
resource = avo_resource.dup.hydrate(model: model).hydrate_fields(model: model)
122123

124+
record_path = if resource.search_result_path.present?
125+
Avo::Hosts::ResourceRecordHost.new(block: resource.search_result_path, resource: resource, record: model).handle
126+
else
127+
resource.record_path
128+
end
129+
123130
result = {
124131
_id: model.id,
125132
_label: resource.label,
126-
_url: resource.record_path,
133+
_url: record_path
127134
}
128135

129136
if App.license.has_with_trial(:enhanced_search_results)

lib/avo/base_resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BaseResource
3030
class_attribute :description, default: :id
3131
class_attribute :search_query, default: nil
3232
class_attribute :search_query_help, default: ""
33+
class_attribute :search_result_path
3334
class_attribute :includes, default: []
3435
class_attribute :authorization_policy
3536
class_attribute :translation_key

lib/avo/hosts/base_host.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class BaseHost
1010
option :params, default: proc { Avo::App.params }
1111
option :view_context, default: proc { Avo::App.view_context }
1212
option :current_user, default: proc { Avo::App.current_user }
13+
option :main_app, default: proc { view_context.main_app }
14+
option :avo, default: proc { view_context.avo }
1315
# This is optional because we might instantiate the `Host` first and later hydrate it with a block.
1416
option :block, optional: true
1517

lib/avo/services/authorization_service.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,16 @@ def defined_methods(model, **args)
130130
end
131131

132132
def has_method?(method, **args)
133+
method = "#{method}?" unless method.to_s.end_with? "?"
133134
defined_methods(args[:record] || record, **args).include? method.to_sym
134135
end
136+
137+
# Check the received method to see if the user overrode it in their config and then checks if it's present on the policy.
138+
def has_action_method?(method, **args)
139+
method = Avo.configuration.authorization_methods.stringify_keys[method.to_s] || method
140+
141+
has_method? method, **args
142+
end
135143
end
136144
end
137145
end

lib/generators/avo/templates/initializer/avo.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Avo.configure do |config|
2929
# update: 'update?',
3030
# create: 'create?',
3131
# destroy: 'destroy?',
32+
# search: 'search?',
3233
# }
3334
# config.raise_error_on_missing_policy = false
3435
# config.authorization_client = :pundit

spec/dummy/app/avo/resources/city_resource.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class CityResource < Avo::BaseResource
22
self.title = :name
33
self.includes = []
4-
# self.search_query = ->(params:) do
5-
# scope.ransack(id_eq: params[:q], m: "or").result(distinct: false)
6-
# end
4+
self.search_query = -> do
5+
scope.ransack(name_eq: params[:q]).result(distinct: false)
6+
end
7+
self.search_result_path = -> do
8+
avo.resources_city_path record, custom: "yup"
9+
end
710
self.extra_params = [:fish_type, :something_else, properties: [], information: [:name, :history]]
811

912
field :id, as: :id

spec/dummy/app/policies/application_policy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def update?
2626
false
2727
end
2828

29+
def avo_search?
30+
false
31+
end
32+
2933
def edit?
3034
update?
3135
end

0 commit comments

Comments
 (0)