rack-restful_submit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ladislav Martincik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,106 @@
1
+ h1. Rack RESTful Submit
2
+
3
+ Implements support of RESTful resources with Rails MVC when Javascript is off and bulk operations are required with multiple submit buttons.
4
+
5
+ h2. Rack Builder
6
+
7
+ <pre>
8
+ <code>
9
+ require 'rack-restful_submit'
10
+ use Rack::RestfulSubmit
11
+ </code>
12
+ </pre>
13
+
14
+ h2. Rails integration
15
+
16
+ h3. Install GEM without Bundler
17
+
18
+ Insert into config/environment.rb:
19
+ <pre>
20
+ <code>
21
+ config.gem "rack-restful_submit"
22
+ </code>
23
+ </pre>
24
+
25
+ Run:
26
+ <pre>
27
+ <code>
28
+ rake gems:install
29
+ </code>
30
+ </pre>
31
+
32
+ h3. Install GEM with Bundler
33
+
34
+ Insert into Gemfile:
35
+ <pre>
36
+ <code>
37
+ gem "rack-restful_submit"
38
+ </code>
39
+ </pre>
40
+
41
+ Run:
42
+ <pre>
43
+ <code>
44
+ bundle install
45
+ </code>
46
+ </pre>
47
+
48
+ h3. Integration
49
+
50
+ Insert into config/environment.rb:
51
+ <pre>
52
+ <code>
53
+ config.middleware.insert_after Rack::MethodOverride, Rack::RestfulSubmit
54
+ </code>
55
+ </pre>
56
+
57
+ h2. Usage
58
+
59
+ In the example below you can see that using submit input with '__rewrite' name and 'multi_destroy' mapping allows
60
+ us to convert normal POST request into RESTful one. Don't forget to also insert 2 hidden fields URL and METHOD as shown below.
61
+ *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.
62
+
63
+ Example of industries/index.html.erb
64
+ <pre>
65
+ <code>
66
+ <h1>Listing industries</h1>
67
+
68
+ <form method="post">
69
+ <table>
70
+ <tr>
71
+ <th>Name</th>
72
+ <th>Description</th>
73
+ </tr>
74
+
75
+ <% @industries.each do |industry| %>
76
+ <tr>
77
+ <td><%=h check_box_tag 'industry_ids[]', industry.id %></td>
78
+ <td><%=h link_to industry.name, industry %></td>
79
+ <td><%=h industry.description %></td>
80
+ <td><%= link_to 'Edit', edit_industry_path(industry) %></td>
81
+ </tr>
82
+ <% end %>
83
+ </table>
84
+
85
+ <input type="hidden" name="__map[multi_destroy][url]" value="<%= destroy_multiple_industries_url %>" />
86
+ <input type="hidden" name="__map[multi_destroy][method]" value="DELETE" />
87
+ <input type="submit" name="__rewrite[multi_destroy]" value="Destroy">
88
+
89
+ </form>
90
+
91
+ <br />
92
+
93
+ <%= link_to 'New industry', new_industry_path %>
94
+ </code>
95
+ </pre>
96
+
97
+ Routes:
98
+ <pre>
99
+ <code>
100
+ map.resources :industries, :collection => { :destroy_multiple => :delete }
101
+ </code>
102
+ </pre>
103
+
104
+ h2. Contribute
105
+
106
+ The Github way: Fork Me, Hack, Push, Send Pull Request
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rack', 'rack-restful_submit')
@@ -0,0 +1,58 @@
1
+ require 'rack'
2
+
3
+ module Rack
4
+ class RestfulSubmit
5
+
6
+ REWRITE_KEYWORD = '__rewrite'.freeze
7
+ REWRITE_MAP = '__map'.freeze
8
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ if env["REQUEST_METHOD"] == "POST"
16
+ req = Request.new(env)
17
+
18
+ if req.params[REWRITE_KEYWORD] && req.params[REWRITE_MAP]
19
+ action = req.params[REWRITE_KEYWORD].keys.first # Should be always just one.
20
+ mapping = req.params[REWRITE_MAP][action]
21
+
22
+ if mapping && mapping['url'] && mapping['method']
23
+ rewrite(env, mapping['url'], mapping['method'])
24
+ end
25
+ end
26
+ end
27
+
28
+ @app.call(env)
29
+ end
30
+
31
+ private
32
+
33
+ def rewrite(env, url, method)
34
+ rewrite_request(env, url)
35
+ rewrite_method(env, method)
36
+ end
37
+
38
+ def rewrite_request(env, uri)
39
+ env['REQUEST_URI'] = uri
40
+ if q_index = uri.index('?')
41
+ env['PATH_INFO'] = uri[0..q_index-1]
42
+ env['QUERYSTRING'] = uri[q_index+1..uri.size-1]
43
+ else
44
+ env['PATH_INFO'] = uri
45
+ env['QUERYSTRING'] = ''
46
+ end
47
+ end
48
+
49
+ def rewrite_method(env, method)
50
+ if HTTP_METHODS.include?(method)
51
+ env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
52
+ env["REQUEST_METHOD"] = method
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "rack-restful_submit"
5
+ s.version = "1.0.0"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Ladislav Martincik"]
8
+ s.email = ["ladislav.martincik@gmail.com"]
9
+ s.homepage = "https://github.com/martincik/rack-restful_submit"
10
+ s.summary = %q(Allows RESTful routing without Javascript and multiple submit buttons)
11
+ s.description = %q(Allows RESTful routing without Javascript and multiple submit buttons)
12
+ s.required_rubygems_version = ">= 1.3.6"
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ s.add_dependency "rack", "~> 1"
16
+ s.add_development_dependency 'rspec', '~> 2'
17
+ end
18
+
@@ -0,0 +1,68 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'rack-restful_submit'
5
+
6
+ describe Rack::RestfulSubmit do
7
+ let(:in_env) { {} }
8
+ let(:out_env) { subject.call(in_env) }
9
+ let(:app) {lambda {|env| env}}
10
+ subject { Rack::RestfulSubmit.new(app)}
11
+
12
+ describe "a get request" do
13
+ it { app.should_receive(:call).with(in_env); out_env }
14
+ it { out_env.should == in_env }
15
+ end
16
+
17
+ describe "a post request" do
18
+ before do
19
+ in_env['REQUEST_METHOD'] = 'POST'
20
+ in_env['rack.input'] = in_env['form_input'] = ''
21
+ in_env['rack.request.form_input'] = ''
22
+ in_env["rack.request.form_hash"] = {}
23
+ end
24
+
25
+ Rack::RestfulSubmit::HTTP_METHODS.each do |method|
26
+ describe "with #{method} and valid mapping" do
27
+ before do
28
+ in_env["rack.request.form_hash"] = {
29
+ "__rewrite" => { method => "value is not important" },
30
+ "__map" => {
31
+ method => {
32
+ "method" => method,
33
+ "url" => "http://localhost:3000/#{method}"
34
+ }
35
+ }
36
+ }
37
+ end
38
+
39
+ it { out_env['REQUEST_METHOD'].should == method }
40
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/#{method}" }
41
+ end
42
+
43
+ describe "with #{method} and invalid mapping" do
44
+ before do
45
+ in_env['REQUEST_URI'] = "http://localhost:3000/"
46
+ in_env["rack.request.form_hash"] = {
47
+ "__rewrite" => { method => "value is not important" }
48
+ }
49
+ end
50
+
51
+ it { out_env['REQUEST_METHOD'].should == "POST" }
52
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/" }
53
+ end
54
+ end
55
+
56
+ describe "with invalid method" do
57
+ before do
58
+ in_env['REQUEST_URI'] = "http://localhost:3000/"
59
+ in_env["rack.request.form_hash"] = {
60
+ "__rewrite" => { "ROCKSTARS" => "value is not important" }
61
+ }
62
+ end
63
+
64
+ it { out_env['REQUEST_METHOD'].should == "POST" }
65
+ it { out_env['REQUEST_URI'].should == "http://localhost:3000/" }
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-restful_submit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Ladislav Martincik
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-09 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ version: "1"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 2
46
+ version: "2"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Allows RESTful routing without Javascript and multiple submit buttons
50
+ email:
51
+ - ladislav.martincik@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - LICENSE
61
+ - README.textile
62
+ - lib/rack-restful_submit.rb
63
+ - lib/rack/rack-restful_submit.rb
64
+ - rack-restful_submit.gemspec
65
+ - spec/rack-restful_submit_spec.rb
66
+ has_rdoc: true
67
+ homepage: https://github.com/martincik/rack-restful_submit
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 23
90
+ segments:
91
+ - 1
92
+ - 3
93
+ - 6
94
+ version: 1.3.6
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Allows RESTful routing without Javascript and multiple submit buttons
102
+ test_files: []
103
+