Module: SimpleWorkflow::Helper

Includes:
Detour
Defined in:
lib/simple_workflow/helper.rb

Overview

View helper methods augmented with breadcrumb management.

Instance Method Summary collapse

Methods included from Detour

#pop_detour, #reset_workflow, #store_detour_in_session

Instance Method Details



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/simple_workflow/helper.rb', line 79

def back_or_link_to(title, options = nil, html_options = nil)
  if session[:detours]
    link_options = { return_from_detour: true }.update(session[:detours].last)

    # FIXME(uwe): Write a test to prove this line is needed.
    link_options['id'] ||= nil
    # EMXIF

    logger.debug "linked return from detour: #{link_options.inspect}"
  else
    link_options = options
  end

  link_to(title, link_options, html_options) if link_options
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

Returns:

  • (Boolean)


75
76
77
# File 'lib/simple_workflow/helper.rb', line 75

def detour?
  !session[:detours].nil?
end

#detour_to(title, options = nil, html_options = nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_workflow/helper.rb', line 15

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 # URL maximum size overflow
    link_with_detour = if block
                         link_to(options, html_options, &block)
                       else
                         link_to(title, options, html_options)
                       end
  end
  link_with_detour
end

#image_button_to(image_source, title, options, html_options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/simple_workflow/helper.rb', line 7

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



46
47
48
49
50
# File 'lib/simple_workflow/helper.rb', line 46

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


52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_workflow/helper.rb', line 52

def image_link_to(image_source, title, url_options, image_options = nil, link_options = nil)
  if link_options == true
    link_options = { method: :post }
  elsif link_options == 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


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simple_workflow/helper.rb', line 63

def image_link_to_remote(image_source, title, link_options, image_options = nil,
    html_options = {})
  if html_options == true
    html_options = { method: :post }
  elsif html_options == 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_optionsObject



42
43
44
# File 'lib/simple_workflow/helper.rb', line 42

def origin_options
  params.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.



36
37
38
39
40
# File 'lib/simple_workflow/helper.rb', line 36

def with_detour(options, origin = origin_options)
  origin.update(origin_options) if origin.keys == [:anchor]
  url = url_for(options)
  url + (url =~ /\?/ ? '&' : '?') + origin.to_h.to_param('detour')
end