diff --git a/app/assets/stylesheets/application.css.scss.erb b/app/assets/stylesheets/application.css.scss.erb index 144680c5..c400f1f3 100644 --- a/app/assets/stylesheets/application.css.scss.erb +++ b/app/assets/stylesheets/application.css.scss.erb @@ -318,3 +318,12 @@ $service-colors: ( color: yellow; text-decoration: underline; } + +.glyphicon-flipped { + -ms-transform: translateZ(0); + -webkit-transform: translateZ(0); + transform: translateZ(0); + -ms-transform: scaleX(-1); + -webkit-transform: scaleX(-1); + transform: scaleX(-1); +} diff --git a/app/helpers/agent_helper.rb b/app/helpers/agent_helper.rb index 6299f24f..0d53bf17 100644 --- a/app/helpers/agent_helper.rb +++ b/app/helpers/agent_helper.rb @@ -57,4 +57,46 @@ module AgentHelper 'maybe'.freeze end end + + def agent_type_icon(agent, agents) + receiver_count = links_counter_cache(agents)[:links_as_receiver][agent.id] || 0 + control_count = links_counter_cache(agents)[:control_links_as_controller][agent.id] || 0 + source_count = links_counter_cache(agents)[:links_as_source][agent.id] || 0 + + if control_count > 0 && receiver_count > 0 + content_tag('span') do + concat icon_tag('glyphicon-arrow-right') + concat tag('br') + concat icon_tag('glyphicon-new-window', class: 'glyphicon-flipped') + end + elsif control_count > 0 && receiver_count == 0 + icon_tag('glyphicon-new-window', class: 'glyphicon-flipped') + elsif receiver_count > 0 && source_count == 0 + icon_tag('glyphicon-arrow-right') + elsif receiver_count == 0 && source_count > 0 + icon_tag('glyphicon-arrow-left') + elsif receiver_count > 0 && source_count > 0 + icon_tag('glyphicon-transfer') + else + icon_tag('glyphicon-unchecked') + end + end + + private + + def links_counter_cache(agents) + @counter_cache ||= {} + @counter_cache[agents.__id__] ||= {}.tap do |cache| + agent_ids = agents.map(&:id) + cache[:links_as_receiver] = Hash[Link.where(receiver_id: agent_ids) + .group(:receiver_id) + .pluck('receiver_id', 'count(receiver_id) as id')] + cache[:links_as_source] = Hash[Link.where(source_id: agent_ids) + .group(:source_id) + .pluck('source_id', 'count(source_id) as id')] + cache[:control_links_as_controller] = Hash[ControlLink.where(controller_id: agent_ids) + .group(:controller_id) + .pluck('controller_id', 'count(controller_id) as id')] + end + end end diff --git a/app/views/agents/_table.html.erb b/app/views/agents/_table.html.erb index 77dbe5d0..29325030 100644 --- a/app/views/agents/_table.html.erb +++ b/app/views/agents/_table.html.erb @@ -1,6 +1,7 @@
<%= sortable_column 'name', 'asc' %> | <%= sortable_column 'created_at', 'desc', name: 'Age' %> | Schedule | @@ -14,6 +15,9 @@ <% @agents.each do |agent| %>|
---|---|---|---|
+ <%= agent_type_icon(agent, @agents) %> + |
<%= link_to agent.name, agent_path(agent, return: (defined?(return_to) && return_to) || request.path) %>
|