Jelajahi Sumber

Add a "Clone" button in agents#show.

- Make agents#new take an optional :id for creating a clone of an
  existing agent of the user.

- Delay the call for $(".select2").select2() so filled-in values of
  sources can take effect.
Akinori MUSHA 11 tahun lalu
induk
melakukan
47fb2894a1

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

@@ -56,9 +56,6 @@ $(document).ready ->
   # JSON Editor
   window.jsonEditor = setupJsonEditor()
 
-  # Select2 Selects
-  $(".select2").select2(width: 'resolve')
-
   # Flash
   if $(".flash").length
     setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000)
@@ -155,6 +152,9 @@ $(document).ready ->
 
   $("#agent_type").change() if $("#agent_type").length
 
+  # Select2 Selects
+  $(".select2").select2(width: 'resolve')
+
   if $(".schedule-region")
     if $(".schedule-region").data("can-be-scheduled") == true
       showSchedule()

+ 7 - 1
app/controllers/agents_controller.rb

@@ -71,7 +71,13 @@ class AgentsController < ApplicationController
   end
 
   def new
-    @agent = current_user.agents.build
+    agents = current_user.agents
+
+    if id = params[:id]
+      @agent = agents.build_clone(agents.find(id))
+    else
+      @agent = agents.build
+    end
 
     respond_to do |format|
       format.html

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

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

+ 16 - 0
spec/controllers/agents_controller_spec.rb

@@ -46,6 +46,22 @@ describe AgentsController do
     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
+      assigns(:agent).attributes.should 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)
+
+      lambda {
+        get :new, :id => agents(:jane_website_agent).to_param
+      }.should raise_error(ActiveRecord::RecordNotFound)
+    end
+  end
+
   describe "GET edit" do
     it "only shows Agents for the current user" do
       sign_in users(:bob)