From: Ladislav Martincik <lmartinc(a)redhat.com>
---
src/app/helpers/application_helper.rb | 18 +++++++++++++
src/config/environment.rb | 3 ++
src/spec/helpers/application_helper_spec.rb | 37 +++++++++++++++++++++++++++
3 files changed, 58 insertions(+), 0 deletions(-)
create mode 100644 src/spec/helpers/application_helper_spec.rb
diff --git a/src/app/helpers/application_helper.rb
b/src/app/helpers/application_helper.rb
index e48851c..456a02e 100644
--- a/src/app/helpers/application_helper.rb
+++ b/src/app/helpers/application_helper.rb
@@ -172,4 +172,22 @@ module ApplicationHelper
end
end
end
+
+ # Integration of rack-restful_submit convention to be able to call
+ # RESTful resources defined by Rails conventions without Javascript.
+ #
+ # Method generates 3 tags:
+ # * 2 hidden field tags to define URL and METHOD to be forwared to in RESTful env.
+ # * 1 submit field to trigger the form submission defining the action
+ #
+ # Example:
+ # <tt>restful_submit_tag('Delete', 'destroy',
instance_path(@instance), 'DELETE')</tt>
+ #
+ # Learn more:
https://github.com/martincik/rack-restful_submit
+ def restful_submit_tag(value, action, url, method, options = {})
+ hidden_field_tag("__map[#{action}][url]", url) <<
+ hidden_field_tag("__map[#{action}][method]", method.upcase) <<
+ submit_tag(value, {:name =>
"__rewrite[#{action}]"}.reverse_merge!(options))
+ end
+
end
diff --git a/src/config/environment.rb b/src/config/environment.rb
index 7d8ba2c..07c0ea0 100644
--- a/src/config/environment.rb
+++ b/src/config/environment.rb
@@ -52,6 +52,9 @@ Rails::Initializer.run do |config|
config.gem "simple-navigation"
config.gem "typhoeus"
config.gem "rb-inotify"
+ config.gem 'rack-restful_submit'
+
+ config.middleware.insert_after Rack::MethodOverride, 'Rack::RestfulSubmit'
config.active_record.observers = :instance_observer, :task_observer
# Only load the plugins named here, in the order given. By default, all plugins
diff --git a/src/spec/helpers/application_helper_spec.rb
b/src/spec/helpers/application_helper_spec.rb
new file mode 100644
index 0000000..fe5c956
--- /dev/null
+++ b/src/spec/helpers/application_helper_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe ApplicationHelper do
+ include ApplicationHelper
+
+ context "restful_submit_tag() helper" do
+
+ it "contains mapping as hidden fields" do
+ html = restful_submit_tag('Delete', 'destroy',
'destroy_url', 'DELETE')
+ doc = Nokogiri::HTML(html)
+ nodes = (doc/"input[@type='hidden']")
+
+ nodes.each do |node|
+ ['__map[destroy][url]',
'__map[destroy][method]'].include?(node['name']).should be_true
+ end
+ end
+
+ it "with no options" do
+ html = restful_submit_tag('Delete', 'destroy',
'destroy_url', 'DELETE')
+ doc = Nokogiri::HTML(html)
+ node = (doc/"input[@type='submit']")
+
+ node.first['name'].should == '__rewrite[destroy]'
+ end
+
+ it "with options" do
+ html = restful_submit_tag('Delete', 'destroy',
'destroy_url', 'DELETE', :id => 'delete', :name =>
'test')
+ doc = Nokogiri::HTML(html)
+ node = (doc/"input[@type='submit']")
+
+ node.first['name'].should == '__rewrite[destroy]'
+ node.first['id'].should == 'delete'
+ end
+
+ end
+
+end
--
1.7.3.2