easier removal of agents from scenarios via the menu

Andrew Cantino 10 jaren geleden
bovenliggende
commit
b91faf62a0

+ 1 - 1
app/assets/stylesheets/application.css.scss.erb

@@ -152,7 +152,7 @@ span.not-applicable:after {
152 152
   top: 2px;
153 153
 }
154 154
 
155
-h2 .scenario {
155
+h2 .scenario, a span.label.scenario {
156 156
   position: relative;
157 157
   top: -2px;
158 158
 }

+ 12 - 1
app/controllers/agents_controller.rb

@@ -98,7 +98,7 @@ class AgentsController < ApplicationController
98 98
   end
99 99
 
100 100
   def diagram
101
-    @agents = current_user.agents.includes(:receivers)
101
+    @agents = ((params[:scenario_id].present? && current_user.scenarios.find(params[:scenario_id])) || current_user).agents.includes(:receivers)
102 102
   end
103 103
 
104 104
   def create
@@ -130,6 +130,17 @@ class AgentsController < ApplicationController
130 130
     end
131 131
   end
132 132
 
133
+  def leave_scenario
134
+    @agent = current_user.agents.find(params[:id])
135
+    @scenario = current_user.scenarios.find(params[:scenario_id])
136
+    @agent.scenarios.destroy(@scenario)
137
+
138
+    respond_to do |format|
139
+      format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
140
+      format.json { head :no_content }
141
+    end
142
+  end
143
+
133 144
   def destroy
134 145
     @agent = current_user.agents.find(params[:id])
135 146
     @agent.destroy

+ 1 - 1
app/helpers/agent_helper.rb

@@ -9,7 +9,7 @@ module AgentHelper
9 9
   def scenario_links(agent)
10 10
     agent.scenarios.map { |scenario|
11 11
       link_to(scenario.name, scenario, class: "label label-info")
12
-    }.to_sentence
12
+    }.join(" ").html_safe
13 13
   end
14 14
 
15 15
   def agent_show_class(agent)

+ 1 - 0
app/helpers/dot_helper.rb

@@ -35,6 +35,7 @@ module DotHelper
35 35
           dot << '%s;' % disabled_label(agent)
36 36
         end
37 37
         agent.receivers.each do |receiver|
38
+          next unless agents.include?(receiver)
38 39
           dot << "%s->%s;" % [disabled_label(agent), disabled_label(receiver)]
39 40
         end
40 41
       end

+ 10 - 0
app/views/agents/_action_menu.html.erb

@@ -27,6 +27,16 @@
27 27
     <% end %>
28 28
   </li>
29 29
 
30
+  <% if agent.scenarios.length > 0 %>
31
+    <li class="divider"></li>
32
+
33
+    <% agent.scenarios.each do |scenario| %>
34
+      <li>
35
+        <%= link_to "<span class='color-warning glyphicon glyphicon-remove-circle'></span> Remove from <span class='scenario label label-info'>#{h scenario.name}</span>".html_safe, leave_scenario_agent_path(agent, :scenario_id => scenario.to_param, :return => returnTo), method: :put, :tabindex => "-1" %>
36
+      </li>
37
+    <% end %>
38
+  <% end %>
39
+
30 40
   <li class="divider"></li>
31 41
 
32 42
   <% if agent.can_create_events? && agent.events.count > 0 %>

+ 1 - 0
app/views/scenarios/show.html.erb

@@ -11,6 +11,7 @@
11 11
 
12 12
       <div class="btn-group">
13 13
         <%= link_to '<span class="glyphicon glyphicon-chevron-left"></span> Back'.html_safe, scenarios_path, class: "btn btn-default" %>
14
+        <%= link_to '<span class="glyphicon glyphicon-random"></span> View Diagram'.html_safe, diagram_agents_path(:scenario_id => @scenario.to_param), class: "btn btn-default" %>
14 15
         <%= link_to '<span class="glyphicon glyphicon-edit"></span> Edit'.html_safe, edit_scenario_path(@scenario), class: "btn btn-default" %>
15 16
         <%= link_to '<span class="glyphicon glyphicon-share-alt"></span> Share'.html_safe, share_scenario_path(@scenario), class: "btn btn-default" %>
16 17
         <%= link_to '<span class="glyphicon glyphicon-trash"></span> Delete'.html_safe, scenario_path(@scenario), method: :delete, data: { confirm: "This will remove the '#{@scenario.name}' Scenerio from all Agents and delete it.  Are you sure?" }, class: "btn btn-default" %>

+ 1 - 0
config/routes.rb

@@ -3,6 +3,7 @@ Huginn::Application.routes.draw do
3 3
     member do
4 4
       post :run
5 5
       post :handle_details_post
6
+      put :leave_scenario
6 7
       delete :remove_events
7 8
     end
8 9
 

+ 22 - 0
spec/controllers/agents_controller_spec.rb

@@ -207,6 +207,12 @@ describe AgentsController do
207 207
       assigns(:agent).should have(1).errors_on(:sources)
208 208
     end
209 209
 
210
+    it "will not accept Scenarios owned by other users" do
211
+      sign_in users(:bob)
212
+      post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:scenario_ids => [scenarios(:jane_weather).id])
213
+      assigns(:agent).should have(1).errors_on(:scenarios)
214
+    end
215
+
210 216
     it "shows errors" do
211 217
       sign_in users(:bob)
212 218
       post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")
@@ -247,6 +253,22 @@ describe AgentsController do
247 253
     end
248 254
   end
249 255
 
256
+  describe "PUT leave_scenario" do
257
+    it "removes an Agent from the given Scenario for the current user" do
258
+      sign_in users(:bob)
259
+
260
+      agents(:bob_weather_agent).scenarios.should include(scenarios(:bob_weather))
261
+      put :leave_scenario, :id => agents(:bob_weather_agent).to_param, :scenario_id => scenarios(:bob_weather).to_param
262
+      agents(:bob_weather_agent).scenarios.should_not include(scenarios(:bob_weather))
263
+
264
+      Scenario.where(:id => scenarios(:bob_weather).id).should exist
265
+
266
+      lambda {
267
+        put :leave_scenario, :id => agents(:jane_weather_agent).to_param, :scenario_id => scenarios(:jane_weather).to_param
268
+      }.should raise_error(ActiveRecord::RecordNotFound)
269
+    end
270
+  end
271
+
250 272
   describe "DELETE destroy" do
251 273
     it "destroys only Agents owned by the current user" do
252 274
       sign_in users(:bob)