@@ -59,32 +59,38 @@ def set_parent_attributes(hierarchy)
59
59
parent_level_data = hierarchy [ next_idx ] if hierarchy . count != next_idx
60
60
if parent_level_data
61
61
level_data [ :parent_attribute ] = parent_level_data [ :attribute ]
62
+ level_data [ :parent_id_attribute ] = "#{ level_data [ :parent_attribute ] } _id"
62
63
set_parent_value ( level_data )
63
64
end
64
65
end
65
66
end
66
67
67
68
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
+ )
73
79
end
74
80
end
75
81
76
- def instance_from_attribute_name ( attribute )
77
- return unless attribute
82
+ def instance_from_attribute_id ( attribute_id )
83
+ return unless attribute_id
78
84
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
81
87
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 )
84
90
end
85
91
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" )
88
94
association_name . camelize . constantize
89
95
rescue NameError
90
96
object_class . reflect_on_association ( association_name ) . klass
0 commit comments