You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2010/11/25 12:44:34 UTC
[PATCH core] Removed Javascript helper for calling DELETE and POST requests since Sinatra can handle this itself using method overide. (thx. Marios)
From: Michal Fojtik <mf...@redhat.com>
---
.../lib/deltacloud/helpers/application_helper.rb | 12 +++++++++
server/public/javascripts/application.js | 26 --------------------
server/public/stylesheets/compiled/application.css | 2 +
server/views/buckets/index.html.haml | 2 +-
server/views/buckets/show.html.haml | 2 +-
server/views/instances/index.html.haml | 2 +-
server/views/instances/show.html.haml | 2 +-
server/views/keys/index.html.haml | 2 +-
server/views/load_balancers/index.html.haml | 2 +-
server/views/load_balancers/show.html.haml | 2 +-
10 files changed, 21 insertions(+), 33 deletions(-)
diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index 00e8bc9..f538714 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -130,4 +130,16 @@ module ApplicationHelper
"<pem><![CDATA[#{text.strip}]]></pem>"
end
+ def link_to_action(action, url, method)
+ return link_to(action, url) if method.eql? :get
+ capture_haml do
+ haml_tag :form, :method => :post, :action => url, :class => :link do
+ haml_tag :input, :type => :hidden, :name => '_method', :value => method
+ haml_tag :button, :type => :submit do
+ haml_concat action
+ end
+ end
+ end
+ end
+
end
diff --git a/server/public/javascripts/application.js b/server/public/javascripts/application.js
index 509768b..5ee0f7f 100644
--- a/server/public/javascripts/application.js
+++ b/server/public/javascripts/application.js
@@ -3,32 +3,6 @@
$(document).ready(function() {
- $("a.delete").click(function(e) {
- var original_url = $(this).attr('href')
- $.ajax({
- url : original_url,
- type : 'DELETE',
- cache : false,
- success: function(data) {
- window.location = original_url.replace(/\/([\w_-]+)$/i, '')
- }
- })
- return false;
- })
-
- $("a.post").click(function(e) {
- var original_url = $(this).attr('href')
- $.ajax({
- url : original_url,
- type : 'POST',
- dataType : 'xml',
- success: function(data) {
- window.location = original_url.replace(/\/([\w_-]+)$/i, '')
- }
- })
- return false;
- })
-
if ($('select#list_instances').length) {
$('select#list_instances').html("<option>Loading instances...</option>");
$.getJSON("/api/instances?state=RUNNING&format=json",
diff --git a/server/public/stylesheets/compiled/application.css b/server/public/stylesheets/compiled/application.css
index 969f712..569007b 100644
--- a/server/public/stylesheets/compiled/application.css
+++ b/server/public/stylesheets/compiled/application.css
@@ -611,3 +611,5 @@ input[type='radio'] {
table.docs { border : 1px solid #ccc }
table.docs td { border : 1px solid #ccc }
table.docs table td { border : none }
+
+td form.link { display : inline }
diff --git a/server/views/buckets/index.html.haml b/server/views/buckets/index.html.haml
index 781f5c7..279d06d 100644
--- a/server/views/buckets/index.html.haml
+++ b/server/views/buckets/index.html.haml
@@ -30,4 +30,4 @@
-bucket.blob_list.each do |blob|
= blob
%td
- =link_to 'Delete', destroy_bucket_url(bucket.name), :class => 'delete'
+ =link_to_action 'Delete', destroy_bucket_url(bucket.name), :delete
diff --git a/server/views/buckets/show.html.haml b/server/views/buckets/show.html.haml
index d704867..dd85670 100644
--- a/server/views/buckets/show.html.haml
+++ b/server/views/buckets/show.html.haml
@@ -16,4 +16,4 @@
= link_to blob, bucket_url(@bucket.name) + '/' + blob
%dt delete bucket (must be empty)
%dd
- =link_to 'Delete', destroy_bucket_url(@bucket.name), :class => 'delete'
+ =link_to_action 'Delete', destroy_bucket_url(@bucket.name), :delete
diff --git a/server/views/instances/index.html.haml b/server/views/instances/index.html.haml
index 679b9b0..2bd4607 100644
--- a/server/views/instances/index.html.haml
+++ b/server/views/instances/index.html.haml
@@ -27,4 +27,4 @@
= instance.state
%td
-instance.actions.each do |action|
- =link_to action, self.send(:"#{action}_instance_url", instance.id), :class => instance_action_method(action)
+ =link_to_action action, self.send(:"#{action}_instance_url", instance.id), instance_action_method(action)
diff --git a/server/views/instances/show.html.haml b/server/views/instances/show.html.haml
index f27dcb7..0a6e115 100644
--- a/server/views/instances/show.html.haml
+++ b/server/views/instances/show.html.haml
@@ -40,4 +40,4 @@
%dt
%dd
-@instance.actions.each do |action|
- =link_to action, self.send(:"#{action}_instance_url", @instance.id), :class => instance_action_method(action)
+ =link_to_action action, self.send(:"#{action}_instance_url", @instance.id), instance_action_method(action)
diff --git a/server/views/keys/index.html.haml b/server/views/keys/index.html.haml
index 02f6b0c..6f246a4 100644
--- a/server/views/keys/index.html.haml
+++ b/server/views/keys/index.html.haml
@@ -18,7 +18,7 @@
= "#{key.username} - #{key.password}"
%td
- if driver.respond_to?(:destroy_key)
- =link_to 'Destroy', destroy_key_url(key.id), :class => 'delete'
+ =link_to_action 'Destroy', destroy_key_url(key.id), :delete
%tfoot
- if driver.respond_to?(:create_key)
%tr
diff --git a/server/views/load_balancers/index.html.haml b/server/views/load_balancers/index.html.haml
index 65b109c..9bd8fab 100644
--- a/server/views/load_balancers/index.html.haml
+++ b/server/views/load_balancers/index.html.haml
@@ -25,7 +25,7 @@
- balancer.listeners.each do |listener|
="#{listener.protocol}[#{listener.load_balancer_port}]<br/>"
%td
- =link_to 'Destroy', destroy_load_balancer_url(balancer.id), :class => 'delete'
+ =link_to_action 'Destroy', destroy_load_balancer_url(balancer.id), :delete
%tfoot
%tr
%td{:colspan => 6, :style => "text-align:right;"}
diff --git a/server/views/load_balancers/show.html.haml b/server/views/load_balancers/show.html.haml
index 7b6f40b..26e1766 100644
--- a/server/views/load_balancers/show.html.haml
+++ b/server/views/load_balancers/show.html.haml
@@ -25,7 +25,7 @@
- @load_balancer.instances.each do |inst|
%dd
=inst.id
- %a{:class => :post, :href => unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id)} Delete
+ =link_to_action, 'Delete', unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id), :post
%form{:action => url_for("/api/load_balancers/#{@load_balancer.id}/register"), :method => :post}
%p
--
1.7.3.2
Re: [PATCH core] Removed Javascript helper for calling DELETE and POST requests since Sinatra can handle this itself using method overide. (thx. Marios)
Posted by Ladislav Martincik <lm...@redhat.com>.
On Nov 25, 2010, at 12:44 PM, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
>
> ---
> .../lib/deltacloud/helpers/application_helper.rb | 12 +++++++++
> server/public/javascripts/application.js | 26 --------------------
> server/public/stylesheets/compiled/application.css | 2 +
> server/views/buckets/index.html.haml | 2 +-
> server/views/buckets/show.html.haml | 2 +-
> server/views/instances/index.html.haml | 2 +-
> server/views/instances/show.html.haml | 2 +-
> server/views/keys/index.html.haml | 2 +-
> server/views/load_balancers/index.html.haml | 2 +-
> server/views/load_balancers/show.html.haml | 2 +-
> 10 files changed, 21 insertions(+), 33 deletions(-)
>
> diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
> index 00e8bc9..f538714 100644
> --- a/server/lib/deltacloud/helpers/application_helper.rb
> +++ b/server/lib/deltacloud/helpers/application_helper.rb
> @@ -130,4 +130,16 @@ module ApplicationHelper
> "<pem><![CDATA[#{text.strip}]]></pem>"
> end
>
> + def link_to_action(action, url, method)
> + return link_to(action, url) if method.eql? :get
> + capture_haml do
> + haml_tag :form, :method => :post, :action => url, :class => :link do
> + haml_tag :input, :type => :hidden, :name => '_method', :value => method
> + haml_tag :button, :type => :submit do
> + haml_concat action
> + end
> + end
> + end
> + end
> +
> end
> diff --git a/server/public/javascripts/application.js b/server/public/javascripts/application.js
> index 509768b..5ee0f7f 100644
> --- a/server/public/javascripts/application.js
> +++ b/server/public/javascripts/application.js
> @@ -3,32 +3,6 @@
>
> $(document).ready(function() {
>
> - $("a.delete").click(function(e) {
> - var original_url = $(this).attr('href')
> - $.ajax({
> - url : original_url,
> - type : 'DELETE',
> - cache : false,
> - success: function(data) {
> - window.location = original_url.replace(/\/([\w_-]+)$/i, '')
> - }
> - })
> - return false;
> - })
> -
> - $("a.post").click(function(e) {
> - var original_url = $(this).attr('href')
> - $.ajax({
> - url : original_url,
> - type : 'POST',
> - dataType : 'xml',
> - success: function(data) {
> - window.location = original_url.replace(/\/([\w_-]+)$/i, '')
> - }
> - })
> - return false;
> - })
> -
> if ($('select#list_instances').length) {
> $('select#list_instances').html("<option>Loading instances...</option>");
> $.getJSON("/api/instances?state=RUNNING&format=json",
> diff --git a/server/public/stylesheets/compiled/application.css b/server/public/stylesheets/compiled/application.css
> index 969f712..569007b 100644
> --- a/server/public/stylesheets/compiled/application.css
> +++ b/server/public/stylesheets/compiled/application.css
> @@ -611,3 +611,5 @@ input[type='radio'] {
> table.docs { border : 1px solid #ccc }
> table.docs td { border : 1px solid #ccc }
> table.docs table td { border : none }
> +
> +td form.link { display : inline }
> diff --git a/server/views/buckets/index.html.haml b/server/views/buckets/index.html.haml
> index 781f5c7..279d06d 100644
> --- a/server/views/buckets/index.html.haml
> +++ b/server/views/buckets/index.html.haml
> @@ -30,4 +30,4 @@
> -bucket.blob_list.each do |blob|
> = blob
> %td
> - =link_to 'Delete', destroy_bucket_url(bucket.name), :class => 'delete'
> + =link_to_action 'Delete', destroy_bucket_url(bucket.name), :delete
> diff --git a/server/views/buckets/show.html.haml b/server/views/buckets/show.html.haml
> index d704867..dd85670 100644
> --- a/server/views/buckets/show.html.haml
> +++ b/server/views/buckets/show.html.haml
> @@ -16,4 +16,4 @@
> = link_to blob, bucket_url(@bucket.name) + '/' + blob
> %dt delete bucket (must be empty)
> %dd
> - =link_to 'Delete', destroy_bucket_url(@bucket.name), :class => 'delete'
> + =link_to_action 'Delete', destroy_bucket_url(@bucket.name), :delete
> diff --git a/server/views/instances/index.html.haml b/server/views/instances/index.html.haml
> index 679b9b0..2bd4607 100644
> --- a/server/views/instances/index.html.haml
> +++ b/server/views/instances/index.html.haml
> @@ -27,4 +27,4 @@
> = instance.state
> %td
> -instance.actions.each do |action|
> - =link_to action, self.send(:"#{action}_instance_url", instance.id), :class => instance_action_method(action)
> + =link_to_action action, self.send(:"#{action}_instance_url", instance.id), instance_action_method(action)
> diff --git a/server/views/instances/show.html.haml b/server/views/instances/show.html.haml
> index f27dcb7..0a6e115 100644
> --- a/server/views/instances/show.html.haml
> +++ b/server/views/instances/show.html.haml
> @@ -40,4 +40,4 @@
> %dt
> %dd
> -@instance.actions.each do |action|
> - =link_to action, self.send(:"#{action}_instance_url", @instance.id), :class => instance_action_method(action)
> + =link_to_action action, self.send(:"#{action}_instance_url", @instance.id), instance_action_method(action)
> diff --git a/server/views/keys/index.html.haml b/server/views/keys/index.html.haml
> index 02f6b0c..6f246a4 100644
> --- a/server/views/keys/index.html.haml
> +++ b/server/views/keys/index.html.haml
> @@ -18,7 +18,7 @@
> = "#{key.username} - #{key.password}"
> %td
> - if driver.respond_to?(:destroy_key)
> - =link_to 'Destroy', destroy_key_url(key.id), :class => 'delete'
> + =link_to_action 'Destroy', destroy_key_url(key.id), :delete
> %tfoot
> - if driver.respond_to?(:create_key)
> %tr
> diff --git a/server/views/load_balancers/index.html.haml b/server/views/load_balancers/index.html.haml
> index 65b109c..9bd8fab 100644
> --- a/server/views/load_balancers/index.html.haml
> +++ b/server/views/load_balancers/index.html.haml
> @@ -25,7 +25,7 @@
> - balancer.listeners.each do |listener|
> ="#{listener.protocol}[#{listener.load_balancer_port}]<br/>"
> %td
> - =link_to 'Destroy', destroy_load_balancer_url(balancer.id), :class => 'delete'
> + =link_to_action 'Destroy', destroy_load_balancer_url(balancer.id), :delete
> %tfoot
> %tr
> %td{:colspan => 6, :style => "text-align:right;"}
> diff --git a/server/views/load_balancers/show.html.haml b/server/views/load_balancers/show.html.haml
> index 7b6f40b..26e1766 100644
> --- a/server/views/load_balancers/show.html.haml
> +++ b/server/views/load_balancers/show.html.haml
> @@ -25,7 +25,7 @@
> - @load_balancer.instances.each do |inst|
> %dd
> =inst.id
> - %a{:class => :post, :href => unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id)} Delete
> + =link_to_action, 'Delete', unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id), :post
>
> %form{:action => url_for("/api/load_balancers/#{@load_balancer.id}/register"), :method => :post}
> %p
> --
> 1.7.3.2
>
ACK