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
|
# File 'app/helpers/trestle/url_helper.rb', line 5
def admin_link_to(content, instance_or_url=nil, options={}, &block)
if block_given?
instance_or_url, options = content, instance_or_url || {}
content = capture(&block)
end
if instance_or_url.is_a?(String)
link_to(content, instance_or_url, options)
else
if instance_or_url.is_a?(Hash)
instance, options = nil, instance_or_url
else
instance = instance_or_url
end
if options.key?(:admin)
admin = Trestle.lookup(options.delete(:admin))
elsif instance
admin = admin_for(instance)
end
admin ||= self.admin if respond_to?(:admin)
if admin
admin = admin.new(self) if admin.is_a?(Class)
action = options.delete(:action) || :show
params = options.delete(:params) || {}
if admin.respond_to?(:instance_path) && instance
path = admin.instance_path(instance, params.reverse_merge(action: action))
else
params[:id] ||= admin.to_param(instance) if instance
path = admin.path(action, params)
end
options[:data] ||= {}
if MODAL_ACTIONS.include?(action) && admin.respond_to?(:form) && admin.form.modal?
options[:data][:controller] ||= "modal-trigger"
else
options[:data][:turbo_frame] ||= (modal_request? ? "modal" : "_top")
end
link_to(content, path, options)
else
raise ActionController::UrlGenerationError, "An admin could not be inferred. Please specify an admin using the :admin option."
end
end
end
|