Skip to content

Commit

Permalink
There is a button to add a new Agent directly in a Scenario; deleting…
Browse files Browse the repository at this point in the history
… an Agent from its show page no longer 404s; there is a button to destroy all delayed_jobs
  • Loading branch information
cantino committed Aug 15, 2015
1 parent 16e9504 commit ce84f15
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
7 changes: 5 additions & 2 deletions app/controllers/agents_controller.rb
Expand Up @@ -148,6 +148,9 @@ def new
else
@agent = agents.build
end

@agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])

initialize_presenter

respond_to do |format|
Expand Down Expand Up @@ -237,14 +240,14 @@ def redirect_back(message, options = {})
if @agent && !@agent.destroyed?
path = agent_path(@agent)
end
when /\A#{Regexp::escape scenarios_path}\/\d+\Z/, agents_path
when /\A#{Regexp::escape scenarios_path}\/\d+\z/, agents_path
path = ret
end

if path
redirect_to path, notice: message
else
super agents_path, notice: message
redirect_to agents_path, notice: message
end
end

Expand Down
9 changes: 9 additions & 0 deletions app/controllers/jobs_controller.rb
Expand Up @@ -48,6 +48,15 @@ def destroy_failed
end
end

def destroy_all
Delayed::Job.delete_all

respond_to do |format|
format.html { redirect_to jobs_path, notice: "All jobs removed." }
format.json { render json: '', status: :ok }
end
end

private

def running?
Expand Down
4 changes: 4 additions & 0 deletions app/views/jobs/index.html.erb
Expand Up @@ -79,6 +79,10 @@
<%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
<span class="glyphicon glyphicon-trash"></span> Remove failed jobs
<% end %>
<%= link_to destroy_all_jobs_path, class: "btn btn-default", method: :delete, data: { confirm: "Are you sure you want to delete ALL pending jobs for all Huginn users?" } do %>
<span class="glyphicon glyphicon-remove"></span> Remove all jobs
<% end %>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/views/scenarios/show.html.erb
Expand Up @@ -16,10 +16,11 @@

<div class="btn-group">
<%= link_to icon_tag('glyphicon-chevron-left') + ' Back', scenarios_path, class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-plus') + ' New Agent', new_agent_path(scenario_id: @scenario.id), class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-random') + ' View Diagram', scenario_diagram_path(@scenario), class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-edit') + ' Edit', edit_scenario_path(@scenario), class: "btn btn-default" %>
<% if @scenario.source_url.present? %>
<%= link_to icon_tag('glyphicon-plus') + ' Update', new_scenario_imports_path(url: @scenario.source_url), class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-refresh') + ' Update', new_scenario_imports_path(url: @scenario.source_url), class: "btn btn-default" %>
<% end %>
<%= link_to icon_tag('glyphicon-share-alt') + ' Share', share_scenario_path(@scenario), class: "btn btn-default" %>
<%= link_to icon_tag('glyphicon-trash') + ' Delete', 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" %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -62,6 +62,7 @@
end
collection do
delete :destroy_failed
delete :destroy_all
end
end

Expand Down
36 changes: 26 additions & 10 deletions spec/controllers/agents_controller_spec.rb
Expand Up @@ -87,19 +87,35 @@ def valid_attributes(options = {})
end
end

describe "GET new with :id" do
it "opens a clone of a given Agent" do
sign_in users(:bob)
get :new, :id => agents(:bob_website_agent).to_param
expect(assigns(:agent).attributes).to eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
describe "GET new" do
describe "with :id" do
it "opens a clone of a given Agent" do
sign_in users(:bob)
get :new, :id => agents(:bob_website_agent).to_param
expect(assigns(:agent).attributes).to eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
end

it "only allows the current user to clone his own Agent" do
sign_in users(:bob)

expect {
get :new, :id => agents(:jane_website_agent).to_param
}.to raise_error(ActiveRecord::RecordNotFound)
end
end

it "only allows the current user to clone his own Agent" do
sign_in users(:bob)
describe "with a scenario_id" do
it 'populates the assigned agent with the scenario' do
sign_in users(:bob)
get :new, :scenario_id => scenarios(:bob_weather).id
expect(assigns(:agent).scenario_ids).to eq([scenarios(:bob_weather).id])
end

expect {
get :new, :id => agents(:jane_website_agent).to_param
}.to raise_error(ActiveRecord::RecordNotFound)
it "does not see other user's scenarios" do
sign_in users(:bob)
get :new, :scenario_id => scenarios(:jane_weather).id
expect(assigns(:agent).scenario_ids).to eq([])
end
end
end

Expand Down
16 changes: 13 additions & 3 deletions spec/controllers/jobs_controller_spec.rb
@@ -1,7 +1,6 @@
require 'spec_helper'

describe JobsController do

describe "GET index" do
before do
async_handler_yaml =
Expand Down Expand Up @@ -75,8 +74,19 @@
end

it "just destroy failed jobs" do
expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
expect { delete :destroy_failed }.to change(Delayed::Job, :count).by(-1)
end
end

describe "DELETE destroy_all" do
before do
@failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
@running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
sign_in users(:jane)
end

it "destroys all jobs" do
expect { delete :destroy_all }.to change(Delayed::Job, :count).by(-2)
end
end
end

0 comments on commit ce84f15

Please sign in to comment.