Skip to content

Commit

Permalink
Add a route for a shared agents table
Browse files Browse the repository at this point in the history
Conflicts:
	app/controllers/agents_controller.rb
	app/views/agents/_table.html.erb
	app/views/agents/all.html.erb
  • Loading branch information
albertsun committed Aug 31, 2016
1 parent ce9395d commit 5b157cf
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/controllers/agents_controller.rb
Expand Up @@ -7,6 +7,22 @@ def index
set_table_sort sorts: %w[name created_at last_check_at last_event_at last_receive_at], default: { created_at: :desc }

@agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
@shared_agents = Agent.shared - @agents

if show_only_enabled_agents?
@agents = @agents.where(disabled: false)
end

respond_to do |format|
format.html
format.json { render json: @agents }
end
end

def shared
set_table_sort sorts: %w[name users.username created_at last_check_at last_event_at last_receive_at], default: { created_at: :desc }

@agents = Agent.shared.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])

if show_only_enabled_agents?
@agents = @agents.where(disabled: false)
Expand Down
103 changes: 103 additions & 0 deletions app/views/agents/_shared_table.html.erb
@@ -0,0 +1,103 @@
<div class='table-responsive'>
<table class='table table-striped'>
<tr>
<th></th>
<th><%= sortable_column 'name', 'asc' %></th>
<th><%= sortable_column 'created_at', 'desc', name: 'Age' %></th>
<th>Schedule</th>
<th><%= sortable_column 'last_check_at', name: 'Last Check' %></th>
<th><%= sortable_column 'last_event_at', name: 'Last Event Out' %></th>
<th><%= sortable_column 'last_receive_at', name: 'Last Event In' %></th>
<th>Events Created</th>
<th>Working?</th>
<th><%= sortable_column 'users.username', 'asc', name: 'User' %></th>
<th></th>
</tr>

<% agents.each do |agent| %>
<tr>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<%= agent_type_icon(agent, agents) %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if current_user == agent.user %>
<%= link_to agent.name, agent_path(agent, return: (defined?(return_to) && return_to) || request.path) %>
<% else %>
<span><%= agent.name %></span>
<% end %>
<br/>
<span class='text-muted'><%= agent.short_type.titleize %></span>
<% if current_user == agent.user %>
<% if agent.scenarios.present? %>
<span>
<%= scenario_links(agent) %>
</span>
<% end %>
<% end %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<%= time_ago_in_words agent.created_at %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if agent.can_be_scheduled? %>
<%= agent_schedule(agent, ',<br/>') %>
<% else %>
<span class='not-applicable'></span>
<% end %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if agent.can_be_scheduled? %>
<%= agent.last_check_at ? time_ago_in_words(agent.last_check_at) + " ago" : "never" %>
<% else %>
<span class='not-applicable'></span>
<% end %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if agent.can_create_events? %>
<%= agent.last_event_at ? time_ago_in_words(agent.last_event_at) + " ago" : "never" %>
<% else %>
<span class='not-applicable'></span>
<% end %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if agent.can_receive_events? %>
<%= agent.last_receive_at ? time_ago_in_words(agent.last_receive_at) + " ago" : "never" %>
<% else %>
<span class='not-applicable'></span>
<% end %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if agent.can_create_events? %>
<%= link_to(agent.events_count || 0, agent_events_path(agent, return: (defined?(return_to) && return_to) || request.path)) %>
<% else %>
<span class='not-applicable'></span>
<% end %>
</td>
<td><%= working(agent) %></td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<% if current_user == agent.user %>
<span><%= agent.user.username %></span>
<% else %>
<%= link_to agent.user.username, edit_admin_user_path(agent.user, return: (defined?(return_to) && return_to) || request.path) %>
<% end %>
</td>
<td>
<% if current_user == agent.user %>
<div class="btn-group">
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-th-list"></span> Actions <span class="caret"></span>
</button>
<%= render 'agents/action_menu', agent: agent, return_to: (defined?(return_to) && return_to) || request.path %>
</div>
<% else %>
<%#= link_to 'Become User', switch_user_admin_user_path(agent.user), class: "btn btn-sm btn-default btn-info" %>
<% end %>
</td>
</tr>
<% end %>
</table>
</div>

<% if defined? agents.preload %>
<%= paginate agents, :theme => 'twitter-bootstrap-3' %>
<% end %>
8 changes: 5 additions & 3 deletions app/views/agents/_table.html.erb
Expand Up @@ -13,10 +13,10 @@
<th></th>
</tr>

<% @agents.each do |agent| %>
<% agents.each do |agent| %>
<tr>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<%= agent_type_icon(agent, @agents) %>
<%= agent_type_icon(agent, agents) %>
</td>
<td class='<%= "agent-unavailable" if agent.unavailable? %>'>
<%= link_to agent.name, agent_path(agent, return: (defined?(return_to) && return_to) || request.path) %>
Expand Down Expand Up @@ -80,4 +80,6 @@
</table>
</div>

<%= paginate @agents, :theme => 'twitter-bootstrap-3' %>
<% if defined? agents.preload %>
<%= paginate agents, :theme => 'twitter-bootstrap-3' %>
<% end %>
6 changes: 5 additions & 1 deletion app/views/agents/index.html.erb
Expand Up @@ -5,7 +5,7 @@
<h2>Your Agents</h2>
</div>

<%= render 'agents/table' %>
<%= render partial: 'agents/table', locals: {agents: @agents} %>

<br/>

Expand All @@ -15,6 +15,10 @@
<%= link_to icon_tag('glyphicon-random') + ' View diagram', diagram_path, class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-adjust') + toggle_disabled_text, toggle_visibility_agents_path, method: :put, class: "btn btn-default visibility-enabler" %>
</div>

<div class="btn-group">
<%= link_to 'Shared Agents', shared_agents_path, class: "btn btn-default" %>
</div>
</div>
</div>
</div>
17 changes: 17 additions & 0 deletions app/views/agents/shared.html.erb
@@ -0,0 +1,17 @@
<div class='container'>
<div class='row'>
<div class='col-md-12'>
<div class="page-header">
<h2>Shared Agents</h2>
</div>

<%= render partial: 'agents/shared_table', locals: {agents: @agents} %>

<br/>

<div class="btn-group">
<%= link_to 'My Agents', agents_path, class: "btn btn-default" %>
</div>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/scenarios/show.html.erb
Expand Up @@ -10,7 +10,7 @@
<blockquote><%= markdown(@scenario.description) %></blockquote>
<% end %>
<%= render 'agents/table', :return_to => scenario_path(@scenario) %>
<%= render partial: 'agents/table', locals: {agents: @agents}, :return_to => scenario_path(@scenario) %>

<br/>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -9,6 +9,7 @@
end

collection do
get :shared
put :toggle_visibility
post :propagate
get :type_details
Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/agents_controller_spec.rb
Expand Up @@ -26,6 +26,10 @@ def valid_attributes(options = {})
end
end

describe "GET all" do
pending("it returns all Agents if the current user is an admin")
end

describe "POST handle_details_post" do
it "passes control to handle_details_post on the agent" do
sign_in users(:bob)
Expand Down

0 comments on commit 5b157cf

Please sign in to comment.