Skip to content

Commit 6dba69e

Browse files
committed
feat(nested-select): adjust to work with association name
1 parent 33dddd6 commit 6dba69e

File tree

4 files changed

+117
-105
lines changed

4 files changed

+117
-105
lines changed

app/inputs/nested_level_input.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class NestedLevelInput < ActiveAdminAddons::InputBase
1+
class NestedLevelInput < ActiveAdminAddons::SelectInputBase
22
include ActiveAdminAddons::SelectHelpers
33

44
def render_custom_input
@@ -38,15 +38,17 @@ def load_parent_data_options
3838
return unless @options[:parent_attribute]
3939

4040
load_data_attr(:parent, value: @options[:parent_attribute])
41-
load_data_attr(:parent_id, value: @object.send(@options[:parent_attribute]), default: -1)
41+
load_data_attr(
42+
:parent_id, value: @object.send(@options[:parent_id_attribute]), default: -1
43+
)
4244
end
4345

4446
def load_collection_data
4547
return unless @options[:collection]
4648

4749
collection_options = collection_to_select_options do |item, option|
48-
if !!@options[:parent_attribute]
49-
option[@options[:parent_attribute]] = item.send(@options[:parent_attribute])
50+
if @options[:parent_attribute].present?
51+
option[@options[:parent_id_attribute]] = item.send(@options[:parent_id_attribute])
5052
end
5153
end
5254

app/inputs/nested_select_input.rb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,38 @@ def set_parent_attributes(hierarchy)
5959
parent_level_data = hierarchy[next_idx] if hierarchy.count != next_idx
6060
if parent_level_data
6161
level_data[:parent_attribute] = parent_level_data[:attribute]
62+
level_data[:parent_id_attribute] = "#{level_data[:parent_attribute]}_id"
6263
set_parent_value(level_data)
6364
end
6465
end
6566
end
6667

6768
def set_parent_value(level_data)
68-
parent_attribute = level_data[:parent_attribute]
69-
build_virtual_attr(parent_attribute)
70-
instance = instance_from_attribute_name(level_data[:attribute])
71-
if instance&.respond_to?(parent_attribute)
72-
@object.send("#{parent_attribute}=", instance.send(parent_attribute))
69+
build_virtual_attr(level_data[:parent_attribute])
70+
build_virtual_attr(level_data[:parent_id_attribute])
71+
instance = instance_from_attribute_id("#{level_data[:attribute]}_id")
72+
if instance.respond_to?(level_data[:parent_id_attribute])
73+
@object.send(
74+
"#{level_data[:parent_attribute]}=", instance.send(level_data[:parent_attribute])
75+
)
76+
@object.send(
77+
"#{level_data[:parent_id_attribute]}=", instance.send(level_data[:parent_id_attribute])
78+
)
7379
end
7480
end
7581

76-
def instance_from_attribute_name(attribute)
77-
return unless attribute
82+
def instance_from_attribute_id(attribute_id)
83+
return unless attribute_id
7884

79-
attribute_value = @object.send(attribute)
80-
return unless attribute_value
85+
attribute_id_value = @object.send(attribute_id)
86+
return unless attribute_id_value
8187

82-
klass = class_from_attribute(attribute)
83-
klass.find_by(id: attribute_value)
88+
klass = class_from_attribute_id(attribute_id)
89+
klass.find_by(id: attribute_id_value)
8490
end
8591

86-
def class_from_attribute(attribute)
87-
association_name = attribute.to_s.chomp("_id")
92+
def class_from_attribute_id(attribute_id)
93+
association_name = attribute_id.to_s.chomp("_id")
8894
association_name.camelize.constantize
8995
rescue NameError
9096
object_class.reflect_on_association(association_name).klass

app/javascript/activeadmin_addons/inputs/slim-select-nested.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function ajaxSearch(search, currentData, args) {
1212
});
1313

1414
if (!!args.parent) {
15-
args.query.q[`${args.parent}_eq`] = args.parentId;
15+
args.query.q[`${args.parent}_id_eq`] = args.parentId;
1616
}
1717

1818
args.query.q = { ...args.query.q, ...args.filters };
@@ -48,7 +48,7 @@ function settings(el) {
4848
const { model, association } = el.dataset;
4949
const child = el.closest('.nested_level')
5050
.parentNode
51-
.querySelector(`.nested_level [data-model=${model}][data-parent=${association}_id]`);
51+
.querySelector(`.nested_level [data-model=${model}][data-parent=${association}]`);
5252

5353
if (child) {
5454
child.slim.setSelected();

0 commit comments

Comments
 (0)