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
|
require 'table_builder_helper/url'
module TableBuilderHelper
class CustomLinks
ACTIONS_TO_HTTP_METHODS = {
delete: :delete,
archive: :put,
unarchive: :put
}
attr_reader :actions, :object, :user_context, :referential, :workgroup
def initialize(object, user_context, actions, referential = nil, workgroup = nil)
@object = object
@user_context = user_context
@actions = actions
@referential = referential
@workgroup = workgroup
end
def links
authorized_actions.map do |action|
Link.new(
content: I18n.t("actions.#{action}"),
href: polymorphic_url(action),
method: method_for_action(action)
)
end
end
def polymorphic_url(action)
polymorph_url = []
unless [:show, :delete].include?(action)
polymorph_url << action
end
polymorph_url += URL.polymorphic_url_parts(object, referential, workgroup)
end
def method_for_action(action)
ACTIONS_TO_HTTP_METHODS[action]
end
def authorized_actions
actions.select(&policy.method(:authorizes_action?))
end
private
def policy
@__policy__ ||= Pundit.policy(user_context, object)
end
end
end
|