-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheffective_resources_helper.rb
343 lines (273 loc) · 12.3 KB
/
effective_resources_helper.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# frozen_string_literal: true
module EffectiveResourcesHelper
# effective_bootstrap
def effective_submit(form, options = {}, &block)
actions = controller.try(:submits) || raise('controller must be an Effective::CrudController')
actions = actions.select { |k, v| v[:default] != true } if options.delete(:defaults) == false
actions = permitted_resource_actions(form.object, actions)
submits = actions.map { |name, opts| form.save(name, opts.except(:action, :title, 'data-method') ) }.join.html_safe
form.submit('', options) do
(block_given? ? capture(&block) : ''.html_safe) + submits
end
end
# effective_form_inputs
def simple_form_submit(form, options = {}, &block)
actions = controller.try(:submits) || raise('controller must be an Effective::CrudController')
actions = permitted_resource_actions(form.object, actions)
submits = actions.map { |name, opts| form.button(:submit, name, opts.except(:action, :title, 'data-method')) }
# I think this is a bug. I can't override default button class when passing my own class: variable. it merges them.
if (btn_class = SimpleForm.button_class).present?
submits = submits.map { |submit| submit.sub(btn_class, '') }
end
submits = submits.join(' ').html_safe
wrapper_options = { class: 'form-actions' }.merge(options.delete(:wrapper_html) || {})
content_tag(:div, wrapper_options) do
(block_given? ? capture(&block) : ''.html_safe) + submits
end
end
def render_resource_buttons(resource, atts = {}, &block)
effective_resource = find_effective_resource
actions = controller.try(:buttons) || effective_resource.buttons()
actions = if resource.kind_of?(Class)
actions.select { |_, v| effective_resource.collection_get_actions.include?(v[:action]) }
elsif resource.respond_to?(:persisted?) && resource.persisted?
actions.select { |_, v| effective_resource.member_actions.include?(v[:action]) }
else
{}
end
render_resource_actions(resource, atts.merge(actions: actions), &block)
end
# Renders the effective/resource view partial for this resource
# resource is an ActiveRecord thing, an Array of ActiveRecord things, or nil
# Atts are everything else. Interesting ones include:
# partial: :dropleft|:glyphicons|string
# locals: {} render locals
# you can also pass all action names and true/false such as edit: true, show: false
def render_resource_actions(resource, atts = {}, &block)
return ''.html_safe if resource.blank?
unless resource.kind_of?(ActiveRecord::Base) || resource.kind_of?(Class) || resource.kind_of?(Array) || resource.class.ancestors.include?(ActiveModel::Model)
raise 'expected first argument to be an ActiveRecord::Base object or Array of objects'
end
raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
btn_class = atts[:btn_class]
effective_resource = atts[:effective_resource]
namespace = atts[:controller_namespace] || atts[:namespace]
locals = atts[:locals] || {}
partial = atts[:partial]
spacer_template = locals[:spacer_template]
effective_resource ||= find_effective_resource(resource)
namespace ||= (effective_resource.namespace.to_sym if effective_resource.namespace)
# Assign actions
# We filter out any actions passed to us that aren't supported
actions = if atts.key?(:actions) && atts[:actions] == false
{}
elsif atts.key?(:actions)
{}.tap do |actions|
atts[:actions].each do |commit, opts|
actions[commit] = opts if (effective_resource.actions.include?(opts[:action]) || opts[:path]).present?
end
end
else
(resource.kind_of?(Class) ? effective_resource.resource_klass_actions : effective_resource.resource_actions)
end
# Consider only, except, false and proc false
only = Array(atts[:only]) if atts[:only].present?
except = Array(atts[:except]) if atts[:except].present?
actions.select! do |_, opts|
action = opts[:action]
if only.present? && !only.include?(action)
false
elsif except.present? && except.include?(action)
false
elsif atts[action].respond_to?(:call)
Effective::ResourceExec.new(self, resource).instance_exec(&atts[action])
else
atts[action] != false
end
end
# Select Partial
partial = if partial.kind_of?(Symbol)
"effective/resource/actions_#{partial}"
else
partial.presence || 'effective/resource/actions'
end
# Assign Locals
locals = {
resource: resource,
effective_resource: effective_resource,
format_block: (block if block_given?),
namespace: namespace,
actions: actions,
btn_class: (btn_class || '')
}.compact.merge(locals)
if resource.kind_of?(Array)
render(
partial: partial,
formats: [:html],
collection: resource,
as: :resource,
locals: locals.except(:resource),
spacer_template: spacer_template
)
else
render(partial: partial, formats: [:html], locals: locals)
end
end
# When called from /admin/things/new.html.haml this will render 'admin/things/form', or 'things/form', or 'thing/form'
def render_resource_form(resource, atts = {})
unless resource.kind_of?(ActiveRecord::Base) || resource.class.ancestors.include?(ActiveModel::Model)
raise 'expected first argument to be an ActiveRecord or ActiveModel object'
end
raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
effective_resource = (atts.delete(:effective_resource) || find_effective_resource)
action = atts.delete(:action)
safe = atts.delete(:safe)
atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
if lookup_context.template_exists?("form_#{action}", controller._prefixes, :partial)
return render("form_#{action}", atts)
end
if lookup_context.template_exists?('form', controller._prefixes, :partial)
return render('form', atts)
end
effective_resource.view_paths.each do |view_path|
if lookup_context.template_exists?("form_#{action}", [view_path], :partial)
return render(view_path + '/' + "form_#{action}", atts)
end
if lookup_context.template_exists?('form', [view_path], :partial)
return render(view_path + '/' + 'form', atts)
end
end
# Will raise the regular error
return ''.html_safe if safe
render('form', atts)
end
# Similar to render_resource_form
def render_resource_partial(resource, atts = {})
unless resource.kind_of?(ActiveRecord::Base) || resource.class.ancestors.include?(ActiveModel::Model)
raise 'expected first argument to be an ActiveRecord or ActiveModel object'
end
raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
effective_resource = (atts.delete(:effective_resource) || find_effective_resource)
action = atts.delete(:action)
safe = atts.delete(:safe)
atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
partial = (action.present? ? action.to_s : effective_resource.name)
if lookup_context.template_exists?(partial, controller._prefixes, :partial)
return render(partial, atts)
end
effective_resource.view_paths.each do |view_path|
if lookup_context.template_exists?(partial, [view_path], :partial)
return render(view_path + '/' + partial, atts)
end
end
# Will raise the regular error
return ''.html_safe if safe
render(resource, atts)
end
alias_method :render_resource, :render_resource_partial
def render_partial_exists?(partial, atts = {})
raise('expected a path') unless partial.kind_of?(String)
raise('path should not include spaces') if partial.include?(' ')
pieces = partial.to_s.split('/') - [nil, '']
file = pieces.last
path = pieces[0..-2].join('/')
lookup_context.exists?(file, [path], :partial)
end
def render_if_exists(partial, atts = {})
render(partial, atts) if render_partial_exists?(partial, atts)
end
# Tableize attributes
# This is used by effective_orders, effective_logging, effective_trash and effective_mergery
def tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true)
case obj
when Hash
if flatten && obj[:attributes].kind_of?(Hash)
obj = obj[:attributes].merge(obj.except(:attributes))
end
content_tag(:table, class: table.presence) do
content_tag(:tbody) do
obj.map do |key, value|
content_tag(:tr, class: key.to_param) do
content_tag((th == true ? :th : :td), key) +
content_tag(:td) { tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }
end
end.join.html_safe
end
end
when Array
obj.map { |value| tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }.join('<br>')
when Symbol
":#{obj}"
when NilClass
'-'
else
obj.to_s.presence || '""'
end.html_safe
end
def format_resource_value(value)
@format_resource_tags ||= ActionView::Base.sanitized_allowed_tags.to_a + ['table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']
@format_resource_atts ||= ActionView::Base.sanitized_allowed_attributes.to_a + ['colspan', 'rowspan']
simple_format(sanitize(value.to_s, tags: @format_resource_tags, attributes: @format_resource_atts), {}, sanitize: false)
end
def edit_effective_wizard?
controller.class.try(:effective_wizard_controller?) && defined?(resource) && resource.draft?
end
def wizard_card(resource, &block)
raise('expected a block') unless block_given?
raise('expected an acts_as_wizard resource') unless resource.class.respond_to?(:acts_as_wizard?)
step = resource.render_step
raise('expected a render_step') unless step.present?
title = resource.wizard_step_title(step)
raise("expected a title for step #{step}") unless title.present?
link = if edit_effective_wizard? && ['build', 'show', 'update'].include?(controller.action_name) && resource.is_a?(controller.resource_klass) && resource.can_visit_step?(step)
link_to('Edit', wizard_path(step), title: "Edit #{title}", class: 'btn btn-secondary mb-2')
end
content_tag(:div, class: 'card mb-4') do
content_tag(:div, class: 'card-body') do
content_tag(:div, class: 'row') do
content_tag(:div, class: 'col-sm') do
content_tag(:h5, title, class: 'card-title')
end +
content_tag(:div, class: 'col-sm-auto text-right') do
(link || '')
end
end + capture(&block)
end
end
end
def return_to_dashboard_path(fallback: "/")
path = (Tenant.routes.dashboard_path rescue nil) if defined?(Tenant) && Tenant.routes.respond_to?(:dashboard_path)
path ||= (main_app.dashboard_path rescue nil) if main_app.respond_to?(:dashboard_path)
path.presence || fallback
end
# effective_translate
def et(resource, attribute = nil)
EffectiveResources.et(resource, attribute)
end
# effective_translate_plural
def etd(resource, attribute = nil)
EffectiveResources.etd(resource, attribute)
end
# effective_translate_plural
def ets(resource, attribute = nil)
EffectiveResources.ets(resource, attribute)
end
# effective_translate_plural
def etsd(resource, attribute = nil)
EffectiveResources.etsd(resource, attribute)
end
def masked_email(resource)
EffectiveResources.masked_email(resource)
end
def acts_as_published_fields(form)
raise('expected an acts_as_published form object') unless form.object.class.try(:acts_as_published?)
render('effective/acts_as_published/fields', form: form)
end
def acts_as_slugged_fields(form, url: nil)
raise('expected an acts_as_slugged form object') unless form.object.class.try(:acts_as_slugged?)
current_url = if url.present?
'Currently accessible via ' + link_to(url.gsub(form.object.slug, '<strong>' + form.object.slug + '</strong>').html_safe, url, target: :_blank)
end
render('effective/acts_as_slugged/fields', form: form, current_url: current_url)
end
end