-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpaths.rb
73 lines (60 loc) · 2.24 KB
/
paths.rb
1
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
# Put this into features/support
#
module NavigationHelpers
# Maps a name to a path. Used by the
#
# When /^I go to (.+)$/ do |page_name|
#
# step definition in web_steps.feature
#
def path_to(page_name)
case page_name
when /^the home\s?page$/
root_path
when /^the (page|form) for the (.*?) above$/
action_prose = Regexp.last_match(1)
model_prose = Regexp.last_match(2)
route = "#{(action_prose == 'form') ? 'edit_' : ''}#{model_prose_to_route_segment(model_prose)}_path"
model = model_prose_to_class(model_prose)
send(route, model.reorder(:id).last!)
when /^the (page|form) for the (.*?) "(.*?)"$/
action_prose = Regexp.last_match(1)
model_prose = Regexp.last_match(2)
identifier = Regexp.last_match(3)
path_to_show_or_edit(action_prose, model_prose, identifier)
when /^the (.*?) (page|form) for "(.*?)"$/
model_prose = Regexp.last_match(1)
action_prose = Regexp.last_match(2)
identifier = Regexp.last_match(3)
path_to_show_or_edit(action_prose, model_prose, identifier)
when /^the (.*?) form$/
model_prose = Regexp.last_match(1)
route = "new_#{model_prose_to_route_segment(model_prose)}_path"
send(route)
else
begin
page_name =~ /^the (.*) page$/
path_components = $1.split(/\s+/)
self.send(path_components.push('path').join('_').to_sym)
rescue NoMethodError, ArgumentError
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
end
private
def path_to_show_or_edit(action_prose, model_prose, identifier)
model = model_prose_to_class(model_prose)
route = "#{action_prose == 'form' ? 'edit_' : ''}#{model_prose_to_route_segment(model_prose)}_path"
# For find_by_anything see https://makandracards.com/makandra/6361-find-an-activerecord-by-any-column-useful-for-cucumber-steps
send(route, model.find_by_anything!(identifier))
end
def model_prose_to_class(model_prose)
model_prose.gsub(' ', '_').classify.constantize
end
def model_prose_to_route_segment(model_prose)
model_prose = model_prose.downcase
model_prose.gsub(/[\ \/]/, '_')
end
end
World(NavigationHelpers)