5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/controllers/api/v1/biz_txn_types_controller.rb', line 5
def index
if !params[:parent].blank?
parent = nil
params[:parent].split(',').each do |parent_iid|
if parent
parent = BizTxnType.find_or_create(parent_iid, parent_iid.humanize, parent)
else
parent = BizTxnType.find_or_create(parent_iid, parent_iid.humanize)
end
end
respond_to do |format|
format.tree do
render :json => {success: true, biz_txn_types: parent.children_to_tree_hash}
end
format.json do
render :json => {success: true, biz_txn_types: BizTxnType.to_all_representation(parent)}
end
end
elsif params[:ids]
ids = params[:ids].split(',').compact
biz_txn_types = []
ids.each do |id|
if id.is_integer?
biz_txn_type = BizTxnType.find(id)
else
biz_txn_type = BizTxnType.iid(id)
end
if biz_txn_type
respond_to do |format|
format.tree do
data = biz_txn_type.to_hash({
only: [:id, :parent_id, :internal_identifier, :description],
leaf: biz_txn_type.leaf?,
text: biz_txn_type.to_label,
children: []
})
parent = nil
biz_txn_types.each do |biz_txn_type_hash|
if biz_txn_type_hash[:id] == data[:parent_id]
parent = biz_txn_type_hash
end
end
if parent
parent[:children].push(data)
else
biz_txn_types.push(data)
end
end
format.json do
biz_txn_types.push(biz_txn_type.to_hash(only: [:id, :description, :internal_identifier]))
end
end
end
end
render :json => {success: true, biz_txn_types: biz_txn_types}
else
respond_to do |format|
format.tree do
nodes = [].tap do |nodes|
BizTxnType.roots.each do |root|
nodes.push(root.to_tree_hash)
end
end
render :json => {success: true, biz_txn_types: nodes}
end
format.json do
render :json => {success: true, biz_txn_types: BizTxnType.to_all_representation}
end
end
end
end
|