Skip to content

Commit

Permalink
Add collection support for belongs_to fields (#230)
Browse files Browse the repository at this point in the history
* Add collection to belongs to field

* Fix TomSelect integration

* Use ga.jspm.io for tailwindcss-stim-components

* Update importmap.rb

* Update belongs_to.rb

* Update belongs_to.rb

---------

Co-authored-by: Chris Oliver <[email protected]>
  • Loading branch information
kaka-ruto and excid3 authored Nov 21, 2024
1 parent 89ad662 commit 23949f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
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

0 comments on commit 23949f9

Please sign in to comment.