2
3
4
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
|
# File 'lib/scaffolding/supercharts_routes_file_manipulator.rb', line 2
def apply(base_namespaces, prepend_namespace_to_child: nil)
child_namespaces, child_resource, parent_namespaces, parent_resource = divergent_parts
within = find_or_create_namespaces(base_namespaces)
if parent_namespaces.empty? && child_namespaces.one? && parent_resource == child_namespaces.first
parent_within = find_or_convert_resource_block(parent_resource, within: within)
line = "scope module: '#{parent_resource}' do"
unless (scope_within = find(/#{line}/, parent_within))
scope_within = insert([line, "end"], parent_within)
end
find_or_create_resource([child_resource], options: "only: collection_actions", within: scope_within)
parent_block_start = find_block_parent(parent_within)
namespace_line_within = find_or_create_namespaces(child_namespaces, parent_block_start)
if prepend_namespace_to_child.present?
namespace_line_within = find_or_create_namespaces([prepend_namespace_to_child], namespace_line_within)
end
find_or_create_resource([child_resource], options: "except: collection_actions", within: namespace_line_within)
unless find_namespaces(child_namespaces, within)[child_namespaces.last]
raise "tried to insert `namespace :#{child_namespaces.last}` but it seems we failed"
end
elsif parent_namespaces.any?
top_parent_namespace = find_namespaces(parent_namespaces, within)[parent_namespaces.first]
find_or_create_resource(child_namespaces + [child_resource], within: top_parent_namespace)
block_parent_within = find_block_parent(top_parent_namespace)
parent_namespaces_and_resource = (parent_namespaces + [parent_resource]).join("_")
parent_within = find_or_create_resource_block([parent_namespaces_and_resource], options: "path: '#{parent_namespaces_and_resource.tr("_", "/")}'", within: block_parent_within)
if prepend_namespace_to_child.present?
parent_within = find_or_create_namespaces([prepend_namespace_to_child], parent_within)
end
find_or_create_resource(child_namespaces + [child_resource], within: parent_within)
else
begin
within = find_or_convert_resource_block(parent_resource, within: within)
rescue
within = find_or_convert_resource_block(parent_resource, options: "except: collection_actions", within: within)
end
if prepend_namespace_to_child.present?
within = find_or_create_namespaces([prepend_namespace_to_child], within)
end
find_or_create_resource(child_namespaces + [child_resource], options: define_concerns, within: within)
end
end
|