Skip to content

enhancement: friendly_id native support #4012

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 15 additions & 2 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@ def current_user
query
}
class_attribute :find_record_method, default: -> {
query.find id
# Check if the model uses FriendlyId and handle accordingly
if model_class.respond_to?(:friendly_id_config)
if id.is_a?(Array)
# For arrays, use the slug column
query.where(model_class.friendly_id_config.slug_column => id)
else
# For single values, use find_by_slug method
query.friendly.find(id)
end
else
# Standard Rails find behavior for non-FriendlyId models
query.find id
end
}
class_attribute :after_create_path, default: :show
class_attribute :after_update_path, default: :show
Expand Down Expand Up @@ -236,7 +248,8 @@ def find_record(id, query: nil, params: nil)
target: find_record_method,
query: query,
id: id,
params: params
params: params,
model_class:
).handle
end

Expand Down
3 changes: 0 additions & 3 deletions spec/dummy/app/avo/resources/compact_user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class Avo::Resources::CompactUser < Avo::BaseResource
self.model_class = ::User
self.find_record_method = -> {
query.friendly.find id
}

def fields
field :personal_information, as: :heading
Expand Down
3 changes: 0 additions & 3 deletions spec/dummy/app/avo/resources/field_discovery_user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
class Avo::Resources::FieldDiscoveryUser < Avo::BaseResource
self.model_class = ::User
self.description = "This is a resource with discovered fields. It will show fields and associations as defined in the model."
self.find_record_method = -> {
query.friendly.find id
}

def fields
main_panel do
Expand Down
8 changes: 2 additions & 6 deletions spec/dummy/app/avo/resources/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ class Avo::Resources::Post < Avo::BaseResource
mobile_user ? :table : :grid
}
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
id.first.to_i == 0 ? query.where(slug: id) : query.where(id: id)
query.where(slug: id)
else
id.to_i == 0 ? query.find_by_slug(id) : query.find(id)
query.find_by_slug(id)
end
}
self.view_types = [:grid, :table]
Expand Down
11 changes: 0 additions & 11 deletions spec/dummy/app/avo/resources/team_user.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
class Avo::Resources::TeamUser < Avo::BaseResource
self.model_class = "User"
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
id.first.to_i == 0 ? query.where(slug: id) : query.where(id: id)
else
id.to_i == 0 ? query.find_by_slug(id) : query.find(id)
end
}

def fields
field :id, as: :id
Expand Down
14 changes: 1 addition & 13 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ class Avo::Resources::User < Avo::BaseResource
self.index_query = -> do
query.order(last_name: :asc)
end
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
first_is_number = true if Float(id.first, exception: false)
first_is_number ? query.where(id: id) : query.where(slug: id)
else
first_is_number = true if Float(id, exception: false)
first_is_number ? query.find(id) : query.find_by_slug(id)
end
}

self.includes = [:posts, :post]
self.attachments = [:cv]
self.devise_password_optional = true
Expand Down
10 changes: 3 additions & 7 deletions spec/dummy/app/avo/resources/z_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ class Avo::Resources::ZPost < Avo::BaseResource
self.default_view_type = :grid
self.model_class = "Post"
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
return id.first.to_i == 0 ? query.where(slug: id) : query.where(id: id)
query.where(slug: id)
else
query.find_by_slug(id)
end

id.to_i == 0 ? query.find_by_slug(id) : query.find(id)
}
self.visible_on_sidebar = false

Expand Down
Loading