Permalink
Browse files

First version

  • Loading branch information...
1 parent e723c78 commit cf1ed69aedd59544e7e2d7030ea97a8bed3db846 @martincik committed Dec 9, 2010
Showing with 256 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +20 −0 LICENSE
  3. 0 README
  4. +90 −0 README.textile
  5. +1 −0 lib/rack-restful_submit.rb
  6. +58 −0 lib/rack/rack-restful_submit.rb
  7. +18 −0 rack-restful_submit.gemspec
  8. +68 −0 spec/rack-restful_submit_spec.rb
View
@@ -0,0 +1 @@
+*.gem
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Ladislav Martincik
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
0 README
No changes.
View
@@ -0,0 +1,90 @@
+h1. Rack RESTful Submit
+
+Implements support of RESTful resources with Rails MVC when Javascript is off and bulk operations are required with multiple submit buttons.
+
+h2. Rack Builder
+
+<pre>
+require 'rack-restful_submit'
+use Rack::RestfulSubmit
+</pre>
+
+h2. Rails integration
+
+h3. Install GEM without Bundler
+
+Insert into config/environment.rb:
+<pre>
+ config.gem "rack-restful_submit"
+</pre>
+
+Run:
+<pre>
+ rake gems:install
+</pre>
+
+h3. Install GEM with Bundler
+
+Insert into Gemfile:
+<pre>
+ gem "rack-restful_submit"
+</pre>
+
+Run:
+<pre>
+ bundle install
+</pre>
+
+h3. Integration
+
+Insert into config/environment.rb:
+<pre>
+config.middleware.insert_after Rack::MethodOverride, Rack::RestfulSubmit
+</pre>
+
+h2. Usage
+
+In the example below you can see that using submit input with '__rewrite' name and 'multi_destroy' mapping allows
+us to convert normal POST request into RESTful one. Don't forget to also insert 2 hidden fields URL and METHOD as shown below.
+*NOTE:* I'm aware of that adding collection to RESTfull controller ins't very RESTfull but this is only example of this GEM. If you want fully RESTfull create new controller for the bulk operations.
+
+Example of industries/index.html.erb
+<pre>
+<h1>Listing industries</h1>
+
+<form method="post">
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Description</th>
+ </tr>
+
+<% @industries.each do |industry| %>
+ <tr>
+ <td><%=h check_box_tag 'industry_ids[]', industry.id %></td>
+ <td><%=h link_to industry.name, industry %></td>
+ <td><%=h industry.description %></td>
+ <td><%= link_to 'Edit', edit_industry_path(industry) %></td>
+ </tr>
+<% end %>
+</table>
+
+<input type="hidden" name="__map[multi_destroy][url]" value="<%= destroy_multiple_industries_url %>" />
+<input type="hidden" name="__map[multi_destroy][method]" value="DELETE" />
+<input type="submit" name="__rewrite[multi_destroy]" value="Destroy">
+
+</form>
+
+<br />
+
+<%= link_to 'New industry', new_industry_path %>
+</pre>
+
+Routes:
+<pre>
+
+</pre>
+
+h2. Contribute
+
+The Github way: Fork Me, Hack, Push, Send Pull Request
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), 'rack', 'rack-restful_submit')
@@ -0,0 +1,58 @@
+require 'rack'
+
+module Rack
+ class RestfulSubmit
+
+ REWRITE_KEYWORD = '__rewrite'.freeze
+ REWRITE_MAP = '__map'.freeze
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["REQUEST_METHOD"] == "POST"
+ req = Request.new(env)
+
+ if req.params[REWRITE_KEYWORD] && req.params[REWRITE_MAP]
+ action = req.params[REWRITE_KEYWORD].keys.first # Should be always just one.
+ mapping = req.params[REWRITE_MAP][action]
+
+ if mapping && mapping['url'] && mapping['method']
+ rewrite(env, mapping['url'], mapping['method'])
+ end
+ end
+ end
+
+ @app.call(env)
+ end
+
+ private
+
+ def rewrite(env, url, method)
+ rewrite_request(env, url)
+ rewrite_method(env, method)
+ end
+
+ def rewrite_request(env, uri)
+ env['REQUEST_URI'] = uri
+ if q_index = uri.index('?')
+ env['PATH_INFO'] = uri[0..q_index-1]
+ env['QUERYSTRING'] = uri[q_index+1..uri.size-1]
+ else
+ env['PATH_INFO'] = uri
+ env['QUERYSTRING'] = ''
+ end
+ end
+
+ def rewrite_method(env, method)
+ if HTTP_METHODS.include?(method)
+ env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
+ env["REQUEST_METHOD"] = method
+ end
+ end
+
+ end
+end
+
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = "rack-restful_submit"
+ s.version = "1.0.0"
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Ladislav Martincik"]
+ s.email = ["ladislav.martincik@gmail.com"]
+ s.homepage = "https://github.com/martincik/rack-restful_submit"
+ s.summary = %q(Allows RESTful routing without Javascript and multiple submit buttons)
+ s.description = %q(Allows RESTful routing without Javascript and multiple submit buttons)
+ s.required_rubygems_version = ">= 1.3.6"
+ s.files = `git ls-files`.split("\n")
+ s.require_paths = ["lib"]
+ s.add_dependency "rack", "~> 1"
+ s.add_development_dependency 'rspec', '~> 2'
+end
+
@@ -0,0 +1,68 @@
+lib = File.expand_path('../lib/', __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require 'rack-restful_submit'
+
+describe Rack::RestfulSubmit do
+ let(:in_env) { {} }
+ let(:out_env) { subject.call(in_env) }
+ let(:app) {lambda {|env| env}}
+ subject { Rack::RestfulSubmit.new(app)}
+
+ describe "a get request" do
+ it { app.should_receive(:call).with(in_env); out_env }
+ it { out_env.should == in_env }
+ end
+
+ describe "a post request" do
+ before do
+ in_env['REQUEST_METHOD'] = 'POST'
+ in_env['rack.input'] = in_env['form_input'] = ''
+ in_env['rack.request.form_input'] = ''
+ in_env["rack.request.form_hash"] = {}
+ end
+
+ Rack::RestfulSubmit::HTTP_METHODS.each do |method|
+ describe "with #{method} and valid mapping" do
+ before do
+ in_env["rack.request.form_hash"] = {
+ "__rewrite" => { method => "value is not important" },
+ "__map" => {
+ method => {
+ "method" => method,
+ "url" => "http://localhost:3000/#{method}"
+ }
+ }
+ }
+ end
+
+ it { out_env['REQUEST_METHOD'].should == method }
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/#{method}" }
+ end
+
+ describe "with #{method} and invalid mapping" do
+ before do
+ in_env['REQUEST_URI'] = "http://localhost:3000/"
+ in_env["rack.request.form_hash"] = {
+ "__rewrite" => { method => "value is not important" }
+ }
+ end
+
+ it { out_env['REQUEST_METHOD'].should == "POST" }
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/" }
+ end
+ end
+
+ describe "with invalid method" do
+ before do
+ in_env['REQUEST_URI'] = "http://localhost:3000/"
+ in_env["rack.request.form_hash"] = {
+ "__rewrite" => { "ROCKSTARS" => "value is not important" }
+ }
+ end
+
+ it { out_env['REQUEST_METHOD'].should == "POST" }
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/" }
+ end
+ end
+end

0 comments on commit cf1ed69

Please sign in to comment.