-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathtextual_summary_helper.rb
203 lines (172 loc) · 6.24 KB
/
textual_summary_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
module TextualSummaryHelper
def textual_link(target, **opts, &blk)
case target
when ActiveRecord::Relation, Array
textual_collection_link(target, **opts, &blk)
else
textual_object_link(target, **opts, &blk)
end
end
def expand_textual_summary(summary, context)
case summary
when Hash
summary
when Symbol
result = send("textual_#{summary}")
return result if result.kind_of?(Hash) && result[:label]
automatic_label = context.class.human_attribute_name(summary, :default => summary.to_s.titleize)
case result
when Hash
result.dup.tap do |h|
h[:label] = automatic_label
end
when ActiveRecord::Relation, ActiveRecord::Base
textual_link(result, :label => automatic_label)
when String, Integer, true, false, nil
{:label => automatic_label, :value => result.to_s} unless result.to_s.blank?
end
when nil
nil
else
raise "Unexpected summary type: #{summary.class}"
end
end
def expand_textual_group(summaries, context = @record)
Array.wrap(summaries).map { |summary| expand_textual_summary(summary, context) }.compact
end
def textual_group_render_options(group_symbol)
group = send("textual_group_#{group_symbol}")
return nil unless group
{
:partial => group.template,
:locals => group.locals,
}
end
def textual_key_value_group(items)
res = items.collect { |item| {:label => item.name.to_s, :value => item.value.to_s} }
res.sort_by { |k| k[:label] }
end
def textual_tags
label = _("%{name} Tags") % {:name => session[:customer_name]}
h = {:label => label}
tags = session[:assigned_filters]
if tags.blank?
h[:icon] = "fa fa-tag"
h[:value] = _("No %{label} have been assigned") % {:label => label}
else
h[:value] = tags.sort_by { |category, _assigned| category.downcase }
.collect do |category, assigned|
{:icon => "fa fa-tag",
:label => category,
:value => assigned}
end
end
h
end
private
def textual_object_link(object, as: nil, controller: nil, feature: nil, label: nil)
return if object.nil?
klass = as || ui_base_model(object.class)
controller ||= controller_for_model(klass)
feature ||= "#{controller}_show"
label ||= ui_lookup(:model => klass.name)
value = if block_given?
yield object
else
object.name
end
h = {:label => label, :value => value}.merge(textual_object_icon(object, klass))
if role_allows?(:feature => feature)
if restful_routed?(object)
h[:link] = polymorphic_path(object)
else
h[:link] = url_for_only_path(:controller => controller,
:action => 'show',
:id => object)
end
h[:title] = _("Show %{label} '%{value}'") % {:label => label, :value => value}
end
h
end
def textual_collection_link(collection, as: nil, controller_collection: nil, explorer: false, feature: nil, label: nil, link: nil)
if collection.kind_of?(Array)
unless as && link
raise ArgumentError, ":as and :link are both required when linking to an array",
caller.reject { |x| x =~ /^#{__FILE__}:/ }
end
end
klass = as || ui_base_model(collection.klass)
controller_collection ||= klass.name.underscore
feature ||= "#{controller_collection}_show_list"
label ||= ui_lookup(:models => klass.name)
count = collection.count
h = {:label => label, :value => count.to_s}.merge(textual_collection_icon(collection, klass))
if count > 0 && role_allows?(:feature => feature)
if link
h[:link] = link
elsif collection.respond_to?(:proxy_association)
owner = collection.proxy_association.owner
display = collection.proxy_association.reflection.name
if restful_routed?(owner)
h[:link] = polymorphic_path(owner, :display => display)
else
h[:link] = url_for_only_path(:controller => controller_for_model(owner.class),
:action => 'show',
:id => owner,
:display => display)
end
else
h[:link] = url_for_only_path(:controller => controller_collection,
:action => 'list')
end
h[:title] = _("Show all %{label}") % {:label => label}
h[:explorer] = true if explorer
end
h
end
def textual_object_icon(object, klass)
icon = object.decorate.try(:fonticon)
image = object.decorate.try(:fileicon)
if icon || image
{:icon => icon, :image => image}
else
textual_class_icon(klass)
end
end
def textual_collection_icon(_collection, klass)
textual_class_icon(klass)
end
def textual_class_icon(klass)
icon = klass.decorate.try(:fonticon)
image = klass.decorate.try(:fileicon)
if icon || image
{:icon => icon, :image => image}
elsif klass <= AdvancedSetting
{:image => "100/advancedsetting.png"}
elsif klass <= MiqTemplate
{:image => "100/vm.png"}
else
{:image => "100/#{klass.name.underscore}.png"}
end
end
def textual_authentications(authentications)
return [{:label => _("Default Authentication"), :title => t = _("None"), :value => t}] if authentications.blank?
authentications.collect do |auth|
label = case auth[:authtype]
when "default" then _("Default")
when "metrics" then _("C & U Database")
when "amqp" then _("AMQP")
when "console" then _("VMRC Console")
when "ipmi" then _("IPMI")
when "remote" then _("Remote Login")
when "smartstate_docker" then _("SmartState Docker")
when "ws" then _("Web Services")
when "ssh_keypair" then _("SSH Key Pair")
else; _("<Unknown>")
end
{:label => _("%{label} Credentials") % {:label => label},
:value => auth[:status] || _("None"),
:title => auth[:status_details]}
end
end
end