Merge pull request #275 from knu/agent-clone

Allow cloning an existing agent.

Andrew Cantino 10 年 前
コミット
45f39d35c2

+ 3 - 3
app/assets/javascripts/application.js.coffee.erb

@@ -56,9 +56,6 @@ $(document).ready ->
56 56
   # JSON Editor
57 57
   window.jsonEditor = setupJsonEditor()
58 58
 
59
-  # Select2 Selects
60
-  $(".select2").select2(width: 'resolve')
61
-
62 59
   # Flash
63 60
   if $(".flash").length
64 61
     setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000)
@@ -155,6 +152,9 @@ $(document).ready ->
155 152
 
156 153
   $("#agent_type").change() if $("#agent_type").length
157 154
 
155
+  # Select2 Selects
156
+  $(".select2").select2(width: 'resolve')
157
+
158 158
   if $(".schedule-region")
159 159
     if $(".schedule-region").data("can-be-scheduled") == true
160 160
       showSchedule()

+ 7 - 1
app/controllers/agents_controller.rb

@@ -71,7 +71,13 @@ class AgentsController < ApplicationController
71 71
   end
72 72
 
73 73
   def new
74
-    @agent = current_user.agents.build
74
+    agents = current_user.agents
75
+
76
+    if id = params[:id]
77
+      @agent = agents.build_clone(agents.find(id))
78
+    else
79
+      @agent = agents.build
80
+    end
75 81
 
76 82
     respond_to do |format|
77 83
       format.html

+ 13 - 0
app/models/agent.rb

@@ -230,6 +230,19 @@ class Agent < ActiveRecord::Base
230 230
   # Class Methods
231 231
 
232 232
   class << self
233
+    def build_clone(original)
234
+      new(original.slice(:type, :options, :schedule, :source_ids, :keep_events_for, :propagate_immediately)) { |clone|
235
+        # Give it a unique name
236
+        2.upto(count) do |i|
237
+          name = '%s (%d)' % [original.name, i]
238
+          unless exists?(name: name)
239
+            clone.name = name
240
+            break
241
+          end
242
+        end
243
+      }
244
+    end
245
+
233 246
     def cannot_be_scheduled!
234 247
       @cannot_be_scheduled = true
235 248
     end

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

@@ -18,6 +18,7 @@
18 18
           <% end %>
19 19
           <li><%= link_to '<i class="icon-chevron-left"></i> Back'.html_safe, agents_path %></li>
20 20
           <li><%= link_to '<i class="icon-pencil"></i> Edit'.html_safe, edit_agent_path(@agent) %></li>
21
+          <li><%= link_to '<i class="icon-plus"></i> Clone'.html_safe, new_agent_path(id: @agent) %></li>
21 22
 
22 23
           <% if @agent.can_be_scheduled? || @agent.events.count > 0 %>
23 24
             <li class="dropdown">

+ 16 - 0
spec/controllers/agents_controller_spec.rb

@@ -46,6 +46,22 @@ describe AgentsController do
46 46
     end
47 47
   end
48 48
 
49
+  describe "GET new with :id" do
50
+    it "opens a clone of a given Agent" do
51
+      sign_in users(:bob)
52
+      get :new, :id => agents(:bob_website_agent).to_param
53
+      assigns(:agent).attributes.should eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
54
+    end
55
+
56
+    it "only allows the current user to clone his own Agent" do
57
+      sign_in users(:bob)
58
+
59
+      lambda {
60
+        get :new, :id => agents(:jane_website_agent).to_param
61
+      }.should raise_error(ActiveRecord::RecordNotFound)
62
+    end
63
+  end
64
+
49 65
   describe "GET edit" do
50 66
     it "only shows Agents for the current user" do
51 67
       sign_in users(:bob)

+ 45 - 0
spec/models/agent_spec.rb

@@ -514,6 +514,51 @@ describe Agent do
514 514
         end
515 515
       end
516 516
     end
517
+
518
+    describe "Agent.build_clone" do
519
+      before do
520
+        Event.delete_all
521
+        @sender = Agents::SomethingSource.new(
522
+          name: 'Agent (2)',
523
+          options: { foo: 'bar2' },
524
+          schedule: '5pm')
525
+        @sender.user = users(:bob)
526
+        @sender.save!
527
+        @sender.create_event :payload => {}
528
+        @sender.create_event :payload => {}
529
+        @sender.events.count.should == 2
530
+
531
+        @receiver = Agents::CannotBeScheduled.new(
532
+          name: 'Agent',
533
+          options: { foo: 'bar3' },
534
+          keep_events_for: 3,
535
+          propagate_immediately: true)
536
+        @receiver.user = users(:bob)
537
+        @receiver.sources << @sender
538
+        @receiver.memory[:test] = 1
539
+        @receiver.save!
540
+      end
541
+
542
+      it "should create a clone of a given agent for editing" do
543
+        sender_clone = users(:bob).agents.build_clone(@sender)
544
+
545
+        sender_clone.attributes.should == Agent.new.attributes.
546
+          update(@sender.slice(:user_id, :type,
547
+            :options, :schedule, :keep_events_for, :propagate_immediately)).
548
+          update('name' => 'Agent (2) (2)', 'options' => { 'foo' => 'bar2' })
549
+
550
+        sender_clone.source_ids.should == []
551
+
552
+        receiver_clone = users(:bob).agents.build_clone(@receiver)
553
+
554
+        receiver_clone.attributes.should == Agent.new.attributes.
555
+          update(@receiver.slice(:user_id, :type,
556
+            :options, :schedule, :keep_events_for, :propagate_immediately)).
557
+          update('name' => 'Agent (3)', 'options' => { 'foo' => 'bar3' })
558
+
559
+        receiver_clone.source_ids.should == [@sender.id]
560
+      end
561
+    end
517 562
   end
518 563
 
519 564
   describe ".trigger_web_request" do