-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathbuttons_helper.rb
More file actions
135 lines (110 loc) · 4.54 KB
/
Copy pathbuttons_helper.rb
File metadata and controls
135 lines (110 loc) · 4.54 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true
module ButtonsHelper
DATA_ATTRIBUTES = [:confirm, :method, :remote, :'disable-with', :disabled]
#TODO: refactoring: move buttons helpers to own helper
def action_link_to(action, url, options = {})
label = options.delete(:label) || action.to_s.titleize
options[:class] = join_dom_classes(options[:class], action.to_s.presence)
fancy_link_to(label, url, options)
end
# Accessible link button ready for fancy styling
#
def fancy_link_to(label, url, options = {})
options[:class] ||= 'delete' if options[:method].try!(:to_sym) == :delete
options[:class] = join_dom_classes('button-to', 'action', options[:class].presence)
data_attr_options!(options)
switch_link label, url, options
end
# Mini form with single <button> element, ready for fancy styling.
#
# == Options
#
# +method+:: HTTP method
# +remote+:: if true, the request will be sent asynchronously (AJAXy)
# +class+:: css class applied to the button element
# +confirm+:: shows a confirmation dialog
def fancy_button_to(label, url, options = {})
form_attributes = {:method => (method = options.delete(:method) || :post),
:class => 'button-to'}
form_attributes[:style] = 'display:none' if options.delete(:visible) == false
form_attributes['data-remote'] = true if options.delete(:remote)
form_attributes['data-confirm'] = options.delete(:confirm)
button_class = options.delete(:class)
button_class ||= 'delete' if method == :delete
button_class = join_dom_classes('action', button_class)
button_attributes = { type: 'submit', class: button_class.strip }.merge(options)
capture do
form_tag url, form_attributes do
tag.button(label, **button_attributes)
end
end
end
def pf_fancy_button_to(label, url, options = {})
form_attributes = { method: options.delete(:method) || :post }
form_attributes['data-remote'] = true if options.delete(:remote)
form_attributes['data-confirm'] = options.delete(:confirm)
button_class = options.delete(:class)
button_attributes = { type: 'submit', class: button_class.strip }.merge(options)
capture do
form_tag url, form_attributes do
tag.button(label, **button_attributes)
end
end
end
# Convenience method for buttons to some action. The action symbol will be used for button's
# label and it's css class by default:
#
# == Example
#
# # Button with label "Vaporize" and class "vaporize", doing a post to
# # vaporize_everything_path when pressed.
# action_button_to :vaporize, vaporize_everything_path
#
def action_button_to(action, url, options = {})
label = options.delete(:label) || action.to_s.titleize
options[:class] = join_dom_classes(options[:class], action.to_s)
fancy_button_to(label, url, options)
end
# DEPRECATED: Replace with form to be independent of rails-ujs (data-method: 'delete')
# Button for deleting stuff.
#
# This is a shortcut for
#
# fancy_button_to("Delete", url, :method => :delete, :class => 'delete')
def delete_button_for(url, options = {})
action_button_to(:delete, url, options.merge(:method => :delete))
end
def delete_link_for(url, options = {})
action_link_to :delete, url, options.reverse_merge(:method => :delete)
end
def link_to_activate_buyer_user(user)
action_link_to('activate', activate_admin_buyers_account_user_path(user.account, user), method: :post, class: 'action')
rescue StandardError => exception
System::ErrorReporting.report_error(exception)
icon("exclamation-triangle", title: "This account is invalid (e.g. missing admin user)")
end
def button_activate_or_suspend(user)
account = user.account
if user.active?
fancy_button_to('Suspend', suspend_admin_buyers_account_user_path(account, user), class: 'action off')
elsif user.suspended?
fancy_button_to('Unsuspend', unsuspend_admin_buyers_account_user_path(account, user), class: 'action ok')
elsif user.pending? && can?(:activate, user)
link_to_activate_buyer_user(user)
end
end
def pf_link_to(label, url, options = {})
variant = options.delete(:variant) || :link
options[:class] = join_dom_classes("pf-c-button pf-m-#{variant}", options[:class])
link_to label, url, options
end
protected
def javascript_alert_url(text)
"javascript:alert('#{escape_javascript(text)}');javascript:void(0);"
end
def data_attr_options! options
DATA_ATTRIBUTES.each do |key|
options["data-#{key}".to_sym] = options.delete(key) if options[key]
end
end
end