From cee338d4e01a5bd0f8d208d1bf1fbc8f04a11b1d Mon Sep 17 00:00:00 2001 From: Kaka Ruto Date: Wed, 20 Nov 2024 16:32:48 +0000 Subject: [PATCH 1/6] Add collection to belongs to field --- lib/madmin/fields/belongs_to.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/madmin/fields/belongs_to.rb b/lib/madmin/fields/belongs_to.rb index 34612ac..283e7cc 100644 --- a/lib/madmin/fields/belongs_to.rb +++ b/lib/madmin/fields/belongs_to.rb @@ -2,7 +2,13 @@ module Madmin module Fields class BelongsTo < Field def options_for_select(record) - if (record = record.send(attribute_name)) + if options[:collection].present? + collection = options[:collection].is_a?(Proc) ? options[:collection].call : options[:collection] + collection.map do |item| + resource = Madmin.resource_for(item) + [resource.display_name(item), item.id] + end + elsif (record = record.send(attribute_name)) resource = Madmin.resource_for(record) [[resource.display_name(record), record.id]] else From 513f69e5d946a70ce12cc004e4f7c49ca64666bd Mon Sep 17 00:00:00 2001 From: Kaka Ruto Date: Wed, 20 Nov 2024 16:36:06 +0000 Subject: [PATCH 2/6] Fix TomSelect integration --- config/importmap.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/importmap.rb b/config/importmap.rb index 493c238..a6be6f5 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -4,7 +4,8 @@ pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true pin "trix" pin "@rails/actiontext", to: "actiontext.esm.js" -pin_all_from Madmin::Engine.root.join("app/javascript/madmin/controllers"), under: "controllers", to: "madmin/controllers" +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 "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://unpkg.com/tailwindcss-stimulus-components@5/dist/tailwindcss-stimulus-components.module.js" From 782279a4b38946c93ead8d38b2b5f82029f2737d Mon Sep 17 00:00:00 2001 From: Kaka Ruto Date: Wed, 20 Nov 2024 17:04:00 +0000 Subject: [PATCH 3/6] Use ga.jspm.io for tailwindcss-stim-components --- config/importmap.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/importmap.rb b/config/importmap.rb index a6be6f5..cd373de 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -8,4 +8,4 @@ to: "madmin/controllers" 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://unpkg.com/tailwindcss-stimulus-components@5/dist/tailwindcss-stimulus-components.module.js" +pin "tailwindcss-stimulus-components", to: "https://ga.jspm.io/npm:tailwindcss-stimulus-components@6.1.2/dist/tailwindcss-stimulus-components.module.js" From 24c33612cc1fcceac685e430f357730ac316cb37 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 21 Nov 2024 11:48:24 -0600 Subject: [PATCH 4/6] Update importmap.rb --- config/importmap.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/importmap.rb b/config/importmap.rb index cd373de..c633fd5 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -4,8 +4,7 @@ pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true pin "trix" pin "@rails/actiontext", to: "actiontext.esm.js" -pin_all_from Madmin::Engine.root.join("app/javascript/madmin/controllers"), under: "controllers", - to: "madmin/controllers" +pin_all_from Madmin::Engine.root.join("app/javascript/madmin/controllers"), under: "controllers", to: "madmin/controllers" 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" From 26987618170f9412b88cfb8180e6cbe8d314eda5 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 21 Nov 2024 11:59:25 -0600 Subject: [PATCH 5/6] Update belongs_to.rb --- lib/madmin/fields/belongs_to.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/madmin/fields/belongs_to.rb b/lib/madmin/fields/belongs_to.rb index 283e7cc..4d6b5b4 100644 --- a/lib/madmin/fields/belongs_to.rb +++ b/lib/madmin/fields/belongs_to.rb @@ -2,18 +2,13 @@ module Madmin module Fields class BelongsTo < Field def options_for_select(record) - if options[:collection].present? - collection = options[:collection].is_a?(Proc) ? options[:collection].call : options[:collection] - collection.map do |item| - resource = Madmin.resource_for(item) - [resource.display_name(item), item.id] - end - elsif (record = record.send(attribute_name)) - resource = Madmin.resource_for(record) - [[resource.display_name(record), record.id]] + records = if (record = record.send(attribute_name)) + [Madmin.resource_for(record)] else - [] + associated_resource.model.first(25) end + + records.map { [resource.display_name(_1), _1.id] } end def to_param @@ -21,7 +16,11 @@ def to_param 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 From e662900b5b976988ad67041ac2bde118ad9b1bdb Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Thu, 21 Nov 2024 12:05:27 -0600 Subject: [PATCH 6/6] Update belongs_to.rb --- lib/madmin/fields/belongs_to.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/madmin/fields/belongs_to.rb b/lib/madmin/fields/belongs_to.rb index 4d6b5b4..bb10c46 100644 --- a/lib/madmin/fields/belongs_to.rb +++ b/lib/madmin/fields/belongs_to.rb @@ -3,12 +3,12 @@ module Fields class BelongsTo < Field def options_for_select(record) records = if (record = record.send(attribute_name)) - [Madmin.resource_for(record)] + [record] else associated_resource.model.first(25) end - records.map { [resource.display_name(_1), _1.id] } + records.map { [Madmin.resource_for(_1).display_name(_1), _1.id] } end def to_param