Module: SimpleWorkflow::Helper
- Includes:
- Detour
- Defined in:
- lib/simple_workflow/helper.rb
Overview
View helper methods augmented with breadcrumb management.
Instance Method Summary
collapse
-
#back_or_link_to(title, options = nil, html_options = nil, &block) ⇒ Object
-
#detour? ⇒ Boolean
-
#detour_to(title, options = nil, html_options = nil, &block) ⇒ Object
-
#image_button_to(image_source, title, options, html_options = {}) ⇒ Object
-
#image_detour_to(image_source, title, url_options, image_options = nil, link_options = nil) ⇒ Object
-
#image_link_to(image_source, title, url_options, image_options = nil, link_options = nil) ⇒ Object
-
#image_link_to_remote(image_source, title, link_options, image_options = nil, html_options = {}) ⇒ Object
-
#origin_options ⇒ Object
-
#with_detour(options, origin = origin_options) ⇒ Object
Takes a link destination and augments it with the current page as origin.
Methods included from Detour
#pop_detour, #reset_workflow, #store_detour_in_session
Instance Method Details
#back_or_link_to(title, options = nil, html_options = nil, &block) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/simple_workflow/helper.rb', line 89
def back_or_link_to(title, options = nil, html_options = nil, &block)
if block
html_options = options
options = title
title = nil
end
if session[:detours]
link_options = { return_from_detour: true }.update(session[:detours].last)
link_options['id'] ||= nil
logger.debug "linked return from detour: #{link_options.inspect}"
else
link_options = options
end
if link_options
if block
link_to(link_options, html_options, &block)
else
link_to(title, link_options, html_options)
end
end
rescue ActionController::UrlGenerationError => e
if session[:detours]
logger.error "Exception linking to origin: #{e.class} #{e}"
pop_detour(session)
retry
end
raise
end
|
#detour? ⇒ Boolean
85
86
87
|
# File 'lib/simple_workflow/helper.rb', line 85
def detour?
!session[:detours].nil?
end
|
#detour_to(title, options = nil, html_options = nil, &block) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/simple_workflow/helper.rb', line 17
def detour_to(title, options = nil, html_options = nil, &block)
if block
html_options = options
options = title
link_with_detour = link_to(with_detour(options), html_options, &block)
else
link_with_detour = link_to(title, with_detour(options), html_options)
end
if link_with_detour.size > 4096 link_with_detour = if block
link_to(options, html_options, &block)
else
link_to(title, options, html_options)
end
end
link_with_detour
end
|
9
10
11
12
13
14
15
|
# File 'lib/simple_workflow/helper.rb', line 9
def image_button_to(image_source, title, options, html_options = {})
image_submit_tag image_source, {
class: 'image-submit', alt: title, title: title,
id: "#{title}_#{options[:id]}", name: title,
onclick: "form.action='#{url_for(options)}'"
}.update(html_options)
end
|
#image_detour_to(image_source, title, url_options, image_options = nil, link_options = nil) ⇒ Object
54
55
56
57
58
|
# File 'lib/simple_workflow/helper.rb', line 54
def image_detour_to(image_source, title, url_options, image_options = nil, link_options = nil)
image_options ||= { class: 'image-submit' }
image_options.update alt: title, title: title
detour_to image_tag(image_source, image_options), url_options, link_options
end
|
#image_link_to(image_source, title, url_options, image_options = nil, link_options = nil) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/simple_workflow/helper.rb', line 60
def image_link_to(image_source, title, url_options, image_options = nil, link_options = nil)
case link_options
when true
link_options = { method: :post }
when false
link_options = nil
end
image_options ||= { class: 'image-submit' }
image_options.update alt: title, title: title
link_to image_tag(image_source, image_options), url_options, link_options
end
|
#image_link_to_remote(image_source, title, link_options, image_options = nil, html_options = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/simple_workflow/helper.rb', line 72
def image_link_to_remote(image_source, title, link_options, image_options = nil,
html_options = {})
case html_options
when true
html_options = { method: :post }
when false
html_options = {}
end
image_options ||= { class: 'image-submit' }
image_options.update alt: title, title: title
link_to image_tag(image_source, image_options), link_options, html_options.merge(remote: true)
end
|
#origin_options ⇒ Object
50
51
52
|
# File 'lib/simple_workflow/helper.rb', line 50
def origin_options
params.to_unsafe_h.reject { |k, _v| %i[detour return_from_detour].include? k.to_sym }
end
|
#with_detour(options, origin = origin_options) ⇒ Object
Takes a link destination and augments it with the current page as origin. If the optional second argument is given, it is used as the origin. If the given origin is only an anchor, it is added to the current page.
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/simple_workflow/helper.rb', line 38
def with_detour(options, origin = origin_options)
if origin.is_a?(String)
uri = URI(origin)
origin = Rails.application.routes.recognize_path uri.path
origin.update anchor: uri.fragment if uri.fragment.present?
origin.update Rack::Utils.parse_nested_query(uri.query) if uri.query.present?
end
origin.update(origin_options) if origin.keys == [:anchor]
url = url_for(options)
url + (url.include?('?') ? '&' : '?') + origin.to_h.to_param('detour')
end
|