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

Add collection support for belongs_to fields #230

Merged
merged 6 commits into from
Nov 21, 2024
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
4 changes: 2 additions & 2 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
pin "@rails/actiontext", to: "actiontext.esm.js"
pin_all_from Madmin::Engine.root.join("app/javascript/madmin/controllers"), under: "controllers", to: "madmin/controllers"

pin "tom-select", to: "https://unpkg.com/tom-select@2/dist/esm/tom-select.complete.js"
pin "tailwindcss-stimulus-components", to: "https://unpkg.com/tailwindcss-stimulus-components@5/dist/tailwindcss-stimulus-components.module.js"
pin "tom-select", to: "https://ga.jspm.io/npm:tom-select@2.4.1/dist/js/tom-select.complete.js"
pin "tailwindcss-stimulus-components", to: "https://ga.jspm.io/npm:tailwindcss-stimulus-components@6.1.2/dist/tailwindcss-stimulus-components.module.js"
15 changes: 10 additions & 5 deletions lib/madmin/fields/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ module Madmin
module Fields
class BelongsTo < Field
def options_for_select(record)
if (record = record.send(attribute_name))
resource = Madmin.resource_for(record)
[[resource.display_name(record), record.id]]
records = if (record = record.send(attribute_name))
[record]
else
[]
associated_resource.model.first(25)
end

records.map { [Madmin.resource_for(_1).display_name(_1), _1.id] }
end

def to_param
"#{attribute_name}_id"
end

def index_path
Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass).index_path(format: :json)
associated_resource.index_path(format: :json)
end

def associated_resource
Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass)
end
end
end
Expand Down