Browse Source

Give user an option to drop pending events when enabling an agent.

This is shown in a confirmation dialog introduced for enable/disable
actions.
Akinori MUSHA 10 years ago
parent
commit
7edbf92f01

+ 7 - 1
app/controllers/agents_controller.rb

@@ -122,7 +122,13 @@ class AgentsController < ApplicationController
     @agent = current_user.agents.find(params[:id])
 
     respond_to do |format|
-      if @agent.update_attributes(params[:agent])
+      if @agent.with_transaction_returning_status {
+          @agent.attributes = params[:agent]
+          if params[:drop_pending_events] && @agent.can_receive_events?
+            @agent.set_last_checked_event_id
+          end
+          @agent.save
+        }
         format.html { redirect_back "'#{@agent.name}' was successfully updated." }
         format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
       else

+ 56 - 2
app/views/agents/_action_menu.html.erb

@@ -21,9 +21,13 @@
 
   <li>
     <% if agent.disabled? %>
-      <%= link_to '<i class="glyphicon glyphicon-play"></i> Enable agent'.html_safe, agent_path(agent, :agent => { :disabled => false }, :return => returnTo), :method => :put %>
+      <%= link_to '#', 'data-toggle' => 'modal', 'data-target' => "#confirm-enable-agent#{agent.id}" do %>
+        <i class="glyphicon glyphicon-play"></i> Enable agent
+      <% end %>
     <% else %>
-      <%= link_to '<i class="glyphicon glyphicon-pause"></i> Disable agent'.html_safe, agent_path(agent, :agent => { :disabled => true }, :return => returnTo), :method => :put %>
+      <%= link_to '#', 'data-toggle' => 'modal', 'data-target' => "#confirm-disable-agent#{agent.id}" do %>
+        <i class="glyphicon glyphicon-pause"></i> Disable agent
+      <% end %>
     <% end %>
   </li>
 
@@ -49,3 +53,53 @@
     <%= link_to '<span class="color-danger glyphicon glyphicon-remove"></span> Delete agent'.html_safe, agent_path(agent, :return => returnTo), method: :delete, data: { confirm: 'Are you sure that you want to permanently delete this Agent?' }, :tabindex => "-1" %>
   </li>
 </ul>
+
+<% if agent.disabled? %>
+<div id="confirm-enable-agent<%= agent.id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmEnableAgentLabel" aria-hidden="true">
+  <div class="modal-dialog modal-sm">
+    <div class="modal-content">
+      <div class="modal-header">
+        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
+        <h4 class="modal-title">Confirm</h4>
+      </div>
+      <div class="modal-body">
+        <p>Enable &quot;<%= agent.name %>&quot;?</p>
+      </div>
+      <div class="modal-footer">
+        <%= form_for(agent, as: :agent, url: agent_path(agent, return: returnTo), method: 'PUT') do |f| %>
+          <% if agent.can_receive_events? || true %>
+            <div class="form-group">
+              <%= check_box_tag check_box_id = "agent#{agent.id}_drop_pending_events", 'true', false, name: :drop_pending_events %>
+              <%= label_tag check_box_id, 'Drop pending events' %>
+            </div>
+          <% end %>
+          <%= f.hidden_field :disabled, value: 'false' %>
+          <%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
+          <%= f.submit 'Yes', class: 'btn btn-primary' %>
+        <% end %>
+      </div>
+    </div>
+  </div>
+</div>
+<% else %>
+<div id="confirm-disable-agent<%= agent.id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmDisableAgentLabel" aria-hidden="true">
+  <div class="modal-dialog modal-sm">
+    <div class="modal-content">
+      <div class="modal-header">
+        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
+        <h4 class="modal-title">Confirm</h4>
+      </div>
+      <div class="modal-body">
+        <p>Disable &quot;<%= agent.name %>&quot;?</p>
+      </div>
+      <div class="modal-footer">
+        <%= form_for(agent, as: :agent, url: agent_path(agent, return: returnTo), method: 'PUT') do |f| %>
+          <%= f.hidden_field :disabled, value: 'true' %>
+          <%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
+          <%= f.submit 'Yes', class: 'btn btn-primary' %>
+        <% end %>
+      </div>
+    </div>
+  </div>
+</div>
+<% end %>

+ 12 - 0
spec/controllers/agents_controller_spec.rb

@@ -251,6 +251,18 @@ describe AgentsController do
         response.should redirect_to(agents_path)
       end
     end
+
+    it "updates last_checked_event_id when drop_pending_events is given" do
+      sign_in users(:bob)
+      agent = agents(:bob_website_agent)
+      agent.disabled = true
+      agent.last_checked_event_id = nil
+      agent.save!
+      post :update, id: agents(:bob_website_agent).to_param, agent: { disabled: 'false' }, drop_pending_events: true
+      agent.reload
+      agent.disabled.should == false
+      agent.last_checked_event_id.should == Event.maximum(:id)
+    end
   end
 
   describe "PUT leave_scenario" do