Merge pull request #974 from cantino/minor_improvements

Various small improvements

Andrew Cantino 9 years ago
parent
commit
65f9d387ca

+ 6 - 1
app/controllers/agents_controller.rb

@@ -148,6 +148,9 @@ class AgentsController < ApplicationController
148 148
     else
149 149
       @agent = agents.build
150 150
     end
151
+
152
+    @agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])
153
+
151 154
     initialize_presenter
152 155
 
153 156
     respond_to do |format|
@@ -236,8 +239,10 @@ class AgentsController < ApplicationController
236 239
     when "show"
237 240
       if @agent && !@agent.destroyed?
238 241
         path = agent_path(@agent)
242
+      else
243
+        path = agents_path
239 244
       end
240
-    when /\A#{Regexp::escape scenarios_path}\/\d+\Z/, agents_path
245
+    when /\A#{Regexp::escape scenarios_path}\/\d+\z/, agents_path
241 246
       path = ret
242 247
     end
243 248
 

+ 9 - 0
app/controllers/jobs_controller.rb

@@ -48,6 +48,15 @@ class JobsController < ApplicationController
48 48
     end
49 49
   end
50 50
 
51
+  def destroy_all
52
+    Delayed::Job.where(locked_at: nil).delete_all
53
+
54
+    respond_to do |format|
55
+      format.html { redirect_to jobs_path, notice: "All jobs removed." }
56
+      format.json { render json: '', status: :ok }
57
+    end
58
+  end
59
+
51 60
   private
52 61
 
53 62
   def running?

+ 4 - 0
app/views/jobs/index.html.erb

@@ -79,6 +79,10 @@
79 79
         <%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
80 80
           <span class="glyphicon glyphicon-trash"></span> Remove failed jobs
81 81
         <% end %>
82
+
83
+        <%= 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 %>
84
+          <span class="glyphicon glyphicon-remove"></span> Remove all jobs
85
+        <% end %>
82 86
       </div>
83 87
     </div>
84 88
   </div>

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

@@ -16,10 +16,11 @@
16 16
 
17 17
       <div class="btn-group">
18 18
         <%= link_to icon_tag('glyphicon-chevron-left') + ' Back', scenarios_path, class: "btn btn-default" %>
19
+        <%= link_to icon_tag('glyphicon-plus') + ' New Agent', new_agent_path(scenario_id: @scenario.id), class: "btn btn-default" %>
19 20
         <%= link_to icon_tag('glyphicon-random') + ' View Diagram', scenario_diagram_path(@scenario), class: "btn btn-default" %>
20 21
         <%= link_to icon_tag('glyphicon-edit') + ' Edit', edit_scenario_path(@scenario), class: "btn btn-default" %>
21 22
         <% if @scenario.source_url.present? %>
22
-          <%= link_to icon_tag('glyphicon-plus') + ' Update', new_scenario_imports_path(url: @scenario.source_url), class: "btn btn-default" %>
23
+          <%= link_to icon_tag('glyphicon-refresh') + ' Update', new_scenario_imports_path(url: @scenario.source_url), class: "btn btn-default" %>
23 24
         <% end %>
24 25
         <%= link_to icon_tag('glyphicon-share-alt') + ' Share', share_scenario_path(@scenario), class: "btn btn-default" %>
25 26
         <%= 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" %>

+ 1 - 0
config/routes.rb

@@ -62,6 +62,7 @@ Huginn::Application.routes.draw do
62 62
     end
63 63
     collection do
64 64
       delete :destroy_failed
65
+      delete :destroy_all
65 66
     end
66 67
   end
67 68
 

+ 26 - 10
spec/controllers/agents_controller_spec.rb

@@ -87,19 +87,35 @@ describe AgentsController do
87 87
     end
88 88
   end
89 89
 
90
-  describe "GET new with :id" do
91
-    it "opens a clone of a given Agent" do
92
-      sign_in users(:bob)
93
-      get :new, :id => agents(:bob_website_agent).to_param
94
-      expect(assigns(:agent).attributes).to eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
90
+  describe "GET new" do
91
+    describe "with :id" do
92
+      it "opens a clone of a given Agent" do
93
+        sign_in users(:bob)
94
+        get :new, :id => agents(:bob_website_agent).to_param
95
+        expect(assigns(:agent).attributes).to eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
96
+      end
97
+
98
+      it "only allows the current user to clone his own Agent" do
99
+        sign_in users(:bob)
100
+
101
+        expect {
102
+          get :new, :id => agents(:jane_website_agent).to_param
103
+        }.to raise_error(ActiveRecord::RecordNotFound)
104
+      end
95 105
     end
96 106
 
97
-    it "only allows the current user to clone his own Agent" do
98
-      sign_in users(:bob)
107
+    describe "with a scenario_id" do
108
+      it 'populates the assigned agent with the scenario' do
109
+        sign_in users(:bob)
110
+        get :new, :scenario_id => scenarios(:bob_weather).id
111
+        expect(assigns(:agent).scenario_ids).to eq([scenarios(:bob_weather).id])
112
+      end
99 113
 
100
-      expect {
101
-        get :new, :id => agents(:jane_website_agent).to_param
102
-      }.to raise_error(ActiveRecord::RecordNotFound)
114
+      it "does not see other user's scenarios" do
115
+        sign_in users(:bob)
116
+        get :new, :scenario_id => scenarios(:jane_weather).id
117
+        expect(assigns(:agent).scenario_ids).to eq([])
118
+      end
103 119
     end
104 120
   end
105 121
 

+ 16 - 3
spec/controllers/jobs_controller_spec.rb

@@ -1,7 +1,6 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe JobsController do
4
-
5 4
   describe "GET index" do
6 5
     before do
7 6
       async_handler_yaml =
@@ -71,12 +70,26 @@ describe JobsController do
71 70
     before do
72 71
       @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
73 72
       @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
73
+      @pending = Delayed::Job.create
74 74
       sign_in users(:jane)
75 75
     end
76 76
 
77 77
     it "just destroy failed jobs" do
78
-      expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
79
-      expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
78
+      expect { delete :destroy_failed }.to change(Delayed::Job, :count).by(-1)
79
+    end
80
+  end
81
+
82
+  describe "DELETE destroy_all" do
83
+    before do
84
+      @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
85
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
86
+      @pending = Delayed::Job.create
87
+      sign_in users(:jane)
88
+    end
89
+
90
+    it "destroys all jobs" do
91
+      expect { delete :destroy_all }.to change(Delayed::Job, :count).by(-2)
92
+      expect(Delayed::Job.find(@running.id)).to be
80 93
     end
81 94
   end
82 95
 end