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 年之前
父节点
当前提交
7edbf92f01
共有 3 个文件被更改,包括 75 次插入3 次删除
  1. 7 1
      app/controllers/agents_controller.rb
  2. 56 2
      app/views/agents/_action_menu.html.erb
  3. 12 0
      spec/controllers/agents_controller_spec.rb

+ 7 - 1
app/controllers/agents_controller.rb

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

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

@@ -21,9 +21,13 @@
21 21
 
22 22
   <li>
23 23
     <% if agent.disabled? %>
24
-      <%= link_to '<i class="glyphicon glyphicon-play"></i> Enable agent'.html_safe, agent_path(agent, :agent => { :disabled => false }, :return => returnTo), :method => :put %>
24
+      <%= link_to '#', 'data-toggle' => 'modal', 'data-target' => "#confirm-enable-agent#{agent.id}" do %>
25
+        <i class="glyphicon glyphicon-play"></i> Enable agent
26
+      <% end %>
25 27
     <% else %>
26
-      <%= link_to '<i class="glyphicon glyphicon-pause"></i> Disable agent'.html_safe, agent_path(agent, :agent => { :disabled => true }, :return => returnTo), :method => :put %>
28
+      <%= link_to '#', 'data-toggle' => 'modal', 'data-target' => "#confirm-disable-agent#{agent.id}" do %>
29
+        <i class="glyphicon glyphicon-pause"></i> Disable agent
30
+      <% end %>
27 31
     <% end %>
28 32
   </li>
29 33
 
@@ -49,3 +53,53 @@
49 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" %>
50 54
   </li>
51 55
 </ul>
56
+
57
+<% if agent.disabled? %>
58
+<div id="confirm-enable-agent<%= agent.id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmEnableAgentLabel" aria-hidden="true">
59
+  <div class="modal-dialog modal-sm">
60
+    <div class="modal-content">
61
+      <div class="modal-header">
62
+        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
63
+        <h4 class="modal-title">Confirm</h4>
64
+      </div>
65
+      <div class="modal-body">
66
+        <p>Enable &quot;<%= agent.name %>&quot;?</p>
67
+      </div>
68
+      <div class="modal-footer">
69
+        <%= form_for(agent, as: :agent, url: agent_path(agent, return: returnTo), method: 'PUT') do |f| %>
70
+          <% if agent.can_receive_events? || true %>
71
+            <div class="form-group">
72
+              <%= check_box_tag check_box_id = "agent#{agent.id}_drop_pending_events", 'true', false, name: :drop_pending_events %>
73
+              <%= label_tag check_box_id, 'Drop pending events' %>
74
+            </div>
75
+          <% end %>
76
+          <%= f.hidden_field :disabled, value: 'false' %>
77
+          <%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
78
+          <%= f.submit 'Yes', class: 'btn btn-primary' %>
79
+        <% end %>
80
+      </div>
81
+    </div>
82
+  </div>
83
+</div>
84
+<% else %>
85
+<div id="confirm-disable-agent<%= agent.id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmDisableAgentLabel" aria-hidden="true">
86
+  <div class="modal-dialog modal-sm">
87
+    <div class="modal-content">
88
+      <div class="modal-header">
89
+        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
90
+        <h4 class="modal-title">Confirm</h4>
91
+      </div>
92
+      <div class="modal-body">
93
+        <p>Disable &quot;<%= agent.name %>&quot;?</p>
94
+      </div>
95
+      <div class="modal-footer">
96
+        <%= form_for(agent, as: :agent, url: agent_path(agent, return: returnTo), method: 'PUT') do |f| %>
97
+          <%= f.hidden_field :disabled, value: 'true' %>
98
+          <%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
99
+          <%= f.submit 'Yes', class: 'btn btn-primary' %>
100
+        <% end %>
101
+      </div>
102
+    </div>
103
+  </div>
104
+</div>
105
+<% end %>

+ 12 - 0
spec/controllers/agents_controller_spec.rb

@@ -251,6 +251,18 @@ describe AgentsController do
251 251
         response.should redirect_to(agents_path)
252 252
       end
253 253
     end
254
+
255
+    it "updates last_checked_event_id when drop_pending_events is given" do
256
+      sign_in users(:bob)
257
+      agent = agents(:bob_website_agent)
258
+      agent.disabled = true
259
+      agent.last_checked_event_id = nil
260
+      agent.save!
261
+      post :update, id: agents(:bob_website_agent).to_param, agent: { disabled: 'false' }, drop_pending_events: true
262
+      agent.reload
263
+      agent.disabled.should == false
264
+      agent.last_checked_event_id.should == Event.maximum(:id)
265
+    end
254 266
   end
255 267
 
256 268
   describe "PUT leave_scenario" do