Forráskód Böngészése

Merge pull request #825 from cantino/dry_run_from_anywhere

Add "Dry Run" to the action menu
Akinori MUSHA 9 éve
szülő
commit
c4f67d7f68

+ 26 - 0
app/assets/javascripts/components/utils.js.coffee

@@ -33,3 +33,29 @@ class @Utils
       onHide?()
     body?(modal.querySelector('.modal-body'))
     $(modal).modal('show')
+
+  @handleDryRunButton: (button, data = $(button.form).serialize()) ->
+    $(button).prop('disabled', true)
+    $('body').css(cursor: 'progress')
+    $.ajax type: 'POST', url: $(button).data('action-url'), dataType: 'json', data: data
+      .always =>
+        $('body').css(cursor: 'auto')
+      .done (json) =>
+        Utils.showDynamicModal """
+          <h5>Log</h5>
+          <pre class="agent-dry-run-log"></pre>
+          <h5>Events</h5>
+          <pre class="agent-dry-run-events"></pre>
+          <h5>Memory</h5>
+          <pre class="agent-dry-run-memory"></pre>
+          """,
+          body: (body) ->
+            $(body).
+              find('.agent-dry-run-log').text(json.log).end().
+              find('.agent-dry-run-events').text(json.events).end().
+              find('.agent-dry-run-memory').text(json.memory)
+          title: 'Dry Run Results',
+          onHide: -> $(button).prop('disabled', false)
+      .fail (xhr, status, error) ->
+        alert('Error: ' + error)
+        $(button).prop('disabled', false)

+ 1 - 25
app/assets/javascripts/pages/agent-edit-page.js.coffee

@@ -142,31 +142,7 @@ class @AgentEditPage
 
   invokeDryRun: (e) ->
     e.preventDefault()
-    button = this
-    $(button).prop('disabled', true)
-    $('body').css(cursor: 'progress')
-    $.ajax type: 'POST', url: $(button).data('action-url'), dataType: 'json', data: $(button.form).serialize()
-      .always =>
-        $("body").css(cursor: 'auto')
-      .done (json) =>
-        Utils.showDynamicModal """
-          <h5>Log</h5>
-          <pre class="agent-dry-run-log"></pre>
-          <h5>Events</h5>
-          <pre class="agent-dry-run-events"></pre>
-          <h5>Memory</h5>
-          <pre class="agent-dry-run-memory"></pre>
-          """,
-          body: (body) ->
-            $(body).
-              find('.agent-dry-run-log').text(json.log).end().
-              find('.agent-dry-run-events').text(json.events).end().
-              find('.agent-dry-run-memory').text(json.memory)
-          title: 'Dry Run Results',
-          onHide: -> $(button).prop('disabled', false)
-      .fail (xhr, status, error) ->
-        alert('Error: ' + error)
-        $(button).prop('disabled', false)
+    Utils.handleDryRunButton(this)
 
 $ ->
   Utils.registerPage(AgentEditPage, forPathsMatching: /^agents/)

+ 6 - 3
app/controllers/agents_controller.rb

@@ -35,15 +35,18 @@ class AgentsController < ApplicationController
   end
 
   def dry_run
-    attrs = params[:agent]
+    attrs = params[:agent] || {}
     if agent = current_user.agents.find_by(id: params[:id])
       # PUT /agents/:id/dry_run
-      type = agent.type
+      if attrs.present?
+        type = agent.type
+        agent = Agent.build_for_type(type, current_user, attrs)
+      end
     else
       # POST /agents/dry_run
       type = attrs.delete(:type)
+      agent = Agent.build_for_type(type, current_user, attrs)
     end
-    agent = Agent.build_for_type(type, current_user, attrs)
     agent.name ||= '(Untitled)'
 
     if agent.valid?

+ 6 - 0
app/views/agents/_action_menu.html.erb

@@ -5,6 +5,12 @@
     </li>
   <% end %>
 
+  <% if agent.can_dry_run? %>
+    <li>
+      <%= link_to icon_tag('glyphicon-refresh') + ' Dry Run', '#', 'data-action-url' => dry_run_agent_path(agent), tabindex: "-1", onclick: "Utils.handleDryRunButton(this, '_method=PUT')" %>
+    </li>
+  <% end %>
+
   <li>
     <%= link_to icon_tag('glyphicon-eye-open') + ' Show'.html_safe, agent_path(agent) %>
   </li>