enable disable all agents within a scenario (#1506)

* tentative work on enable disable all agents within a scenario

* more work with pr

* more work

* better names and save a line of code

* coffeescript to change modal text and hidden disabled value

* redoing coffeescript class to save some code

* initing the function

* updated coffeescript

* change text from specific modal, made names more specific

* updates disabled test and rubocop bug

* improved test and refactored method per rubocop warnings

* switched from 0 to false

* switched from 0 to false and 1 to true

* fixed null bug in postgresql database

* fixed ruby boolean bug

* moved instances into let lazy load and use proper agent fixture

* putting in a different fix

* using existing membership and agent and another version of test

* switch back to 2 not 3 after rm an agent in yml

* made test passed locally

* forgot to remove debuging pp

* small spacing bug

* trying to get rid of newline in file

* typo not agent but scenario

* minor changes to syntax and naming

* rm potential failure and changed update_all statement

* fixing new line issue

* removed unnecessary if else statement from method
This commit is contained in:
Judy Ngai 2016-06-14 01:36:22 -04:00 committed by Andrew Cantino
parent 8d71615972
commit f57a3af521
6 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,15 @@
class @ScenarioShowPage
constructor:() ->
@changeModalText()
changeModalText: () ->
$('#disable-all').click ->
$('#enable-disable-agents .modal-body').text 'Would you like to disable all agents?'
$('#scenario-disabled-value').val 'true'
$('#enable-all').click ->
$('#enable-disable-agents .modal-body').text 'Would you like to enable all agents?'
$('#scenario-disabled-value').val 'false'
$ ->
Utils.registerPage(ScenarioShowPage, forPathsMatching: /^scenarios/)

View file

@ -95,6 +95,16 @@ class ScenariosController < ApplicationController
end
end
def enable_or_disable_all_agents
@scenario = current_user.scenarios.find(params[:id])
@scenario.agents.update_all(disabled: params[:scenario][:disabled] == 'true')
respond_to do |format|
format.html { redirect_to @scenario, notice: 'The agents in this scenario have been successfully updated.' }
format.json { head :no_content }
end
end
def destroy
@scenario = current_user.scenarios.find(params[:id])
@scenario.destroy_with_mode(params[:mode])

View file

@ -0,0 +1,19 @@
<div id="enable-disable-agents" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="enableAgentLabel" 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">
</div>
<div class="modal-footer">
<%= form_for(scenario, as: :scenario, url: enable_or_disable_all_agents_scenario_path(scenario), method: 'PUT') do |f| %>
<%= f.hidden_field :disabled, value: '', id: "scenario-disabled-value" %>
<%= f.button 'No', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
<%= f.submit 'Yes', class: 'btn btn-primary' %>
<% end %>
</div>
</div>
</div>
</div>

View file

@ -24,8 +24,11 @@
<% end %>
<%= link_to icon_tag('glyphicon-share-alt') + ' Share', share_scenario_path(@scenario), class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-trash') + ' Delete', '#', data: { toggle: 'modal', target: "#confirm-scenario-deletion-#{@scenario.id}"}, class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-play') + 'Enable all Agents', '#', data: { toggle: 'modal', target: "#enable-disable-agents"}, class: "btn btn-default", id: "enable-all" %>
<%= link_to icon_tag('glyphicon-pause') + 'Disable all Agents', '#', data: { toggle: 'modal', target: "#enable-disable-agents"}, class: "btn btn-default", id: "disable-all" %>
</div>
</div>
</div>
</div>
<%= render 'scenarios/confirm_deletion_modal', scenario: @scenario %>
<%= render 'scenarios/enable_agents_modal', scenario: @scenario %>

View file

@ -50,6 +50,7 @@ Huginn::Application.routes.draw do
member do
get :share
get :export
put :enable_or_disable_all_agents
end
resource :diagram, :only => [:show]

View file

@ -140,6 +140,15 @@ describe ScenariosController do
end
end
describe 'PUT enable_or_disable_all_agents' do
it 'updates disabled on all agents in a scenario for the current user' do
@params = {"scenario"=>{"disabled"=>"true"}, "commit"=>"Yes", "id"=> scenarios(:bob_weather).id}
put :enable_or_disable_all_agents, @params
expect(agents(:bob_rain_notifier_agent).disabled).to eq(true)
expect(response).to redirect_to(scenario_path(scenarios(:bob_weather)))
end
end
describe "DELETE destroy" do
it "destroys only Scenarios owned by the current user" do
expect {