rack-restful_submit 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ Rack RESTful Submit
2
+ ===================
3
+
4
+ Implements support of RESTful resources with Rails MVC when Javascript is off and bulk operations are required with multiple submit buttons.
5
+
6
+ Rack Builder
7
+ ------------
8
+
9
+ require 'rack-restful_submit'
10
+ use Rack::RestfulSubmit
11
+
12
+ Rails integration
13
+ -----------------
14
+
15
+ ### Install GEM without Bundler
16
+
17
+ Insert into config/environment.rb:
18
+
19
+ config.gem "rack-restful_submit"
20
+
21
+ Run:
22
+
23
+ rake gems:install
24
+
25
+ ### Install GEM with Bundler
26
+
27
+ Insert into Gemfile:
28
+
29
+ gem "rack-restful_submit"
30
+
31
+ Run:
32
+
33
+ bundle install
34
+
35
+ ### Integration
36
+
37
+ We need to swap our RestfulSubmit middleware with MethodOverride which we don't need anymore.
38
+
39
+ Insert into config/environment.rb:
40
+
41
+
42
+ config.middleware.swap Rack::MethodOverride, 'Rack::RestfulSubmit'
43
+
44
+
45
+ Usage
46
+ -----
47
+
48
+ In the example below you can see that using submit input with `__rewrite` name and `multi_destroy` mapping allows
49
+ us to convert normal POST request into RESTful one. Don't forget to also insert 2 hidden fields URL and METHOD as shown below.
50
+ __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.
51
+
52
+ Example of industries/index.html.erb
53
+
54
+ <h1>Listing industries</h1>
55
+
56
+ <form method="post">
57
+ <table>
58
+ <tr>
59
+ <th>Name</th>
60
+ <th>Description</th>
61
+ </tr>
62
+
63
+ <% @industries.each do |industry| %>
64
+ <tr>
65
+ <td><%=h check_box_tag 'industry_ids[]', industry.id %></td>
66
+ <td><%=h link_to industry.name, industry %></td>
67
+ <td><%=h industry.description %></td>
68
+ <td><%= link_to 'Edit', edit_industry_path(industry) %></td>
69
+ </tr>
70
+ <% end %>
71
+ </table>
72
+
73
+ <input type="hidden" name="__map[multi_destroy][url]" value="<%= destroy_multiple_industries_url %>" />
74
+ <input type="hidden" name="__map[multi_destroy][method]" value="DELETE" />
75
+ <input type="submit" name="__rewrite[multi_destroy]" value="Destroy">
76
+
77
+ </form>
78
+
79
+ <br />
80
+
81
+ <%= link_to 'New industry', new_industry_path %>
82
+
83
+ Routes:
84
+
85
+ map.resources :industries, :collection => { :destroy_multiple => :delete }
86
+
87
+ Contribute
88
+ ----------
89
+
90
+ The Github way: Fork Me, Hack, Push, Send Pull Request
91
+
92
+ History
93
+ -------
94
+
95
+ 1.1.0 - Version with MethodOverride support inside
96
+ 1.0.0 - First working version
@@ -1,11 +1,8 @@
1
- require 'rack'
2
-
3
1
  module Rack
4
- class RestfulSubmit
2
+ class RestfulSubmit < MethodOverride
5
3
 
6
4
  REWRITE_KEYWORD = '__rewrite'.freeze
7
5
  REWRITE_MAP = '__map'.freeze
8
- HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
9
6
 
10
7
  def initialize(app)
11
8
  @app = app
@@ -21,7 +18,11 @@ module Rack
21
18
 
22
19
  if mapping && mapping['url'] && mapping['method']
23
20
  rewrite(env, mapping['url'], mapping['method'])
21
+ else
22
+ super(env)
24
23
  end
24
+ else
25
+ super(env)
25
26
  end
26
27
  end
27
28
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "rack-restful_submit"
5
- s.version = "1.0.0"
5
+ s.version = "1.1.0"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Ladislav Martincik"]
8
8
  s.email = ["ladislav.martincik@gmail.com"]
@@ -1,13 +1,14 @@
1
1
  lib = File.expand_path('../lib/', __FILE__)
2
2
  $:.unshift lib unless $:.include?(lib)
3
3
 
4
+ require 'rack'
4
5
  require 'rack-restful_submit'
5
6
 
6
7
  describe Rack::RestfulSubmit do
7
8
  let(:in_env) { {} }
8
9
  let(:out_env) { subject.call(in_env) }
9
10
  let(:app) {lambda {|env| env}}
10
- subject { Rack::RestfulSubmit.new(app)}
11
+ subject { Rack::RestfulSubmit.new(app) }
11
12
 
12
13
  describe "a get request" do
13
14
  it { app.should_receive(:call).with(in_env); out_env }
@@ -64,5 +65,48 @@ describe Rack::RestfulSubmit do
64
65
  it { out_env['REQUEST_METHOD'].should == "POST" }
65
66
  it { out_env['REQUEST_URI'].should == "http://localhost:3000/" }
66
67
  end
68
+
69
+ describe "with methodoverride support" do
70
+ Rack::RestfulSubmit::HTTP_METHODS.each do |method|
71
+ describe "with #{method} no __rewrite args" do
72
+ before do
73
+ in_env["rack.request.form_hash"] = { "_method" => method }
74
+ end
75
+
76
+ it { out_env['REQUEST_METHOD'].should == method }
77
+ end
78
+
79
+ describe "with #{method} and valid __rewrite args" do
80
+ before do
81
+ in_env["rack.request.form_hash"] = {
82
+ "_method" => 'put',
83
+ "__rewrite" => { method => "value is not important" },
84
+ "__map" => {
85
+ method => {
86
+ "method" => method,
87
+ "url" => "http://localhost:3000/#{method}"
88
+ }
89
+ }
90
+ }
91
+ end
92
+
93
+ it { out_env['REQUEST_METHOD'].should == method }
94
+ end
95
+
96
+ describe "with #{method} and not valid __rewrite args" do
97
+ before do
98
+ in_env["rack.request.form_hash"] = {
99
+ "_method" => method,
100
+ "__rewrite" => { method => "value is not important" },
101
+ "__map" => {}
102
+ }
103
+ end
104
+
105
+ it { out_env['REQUEST_METHOD'].should == method }
106
+ end
107
+ end
108
+ end
109
+
67
110
  end
111
+
68
112
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-restful_submit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ladislav Martincik
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-09 00:00:00 +01:00
18
+ date: 2010-12-13 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -58,7 +58,7 @@ extra_rdoc_files: []
58
58
  files:
59
59
  - .gitignore
60
60
  - LICENSE
61
- - README.textile
61
+ - README.markdown
62
62
  - lib/rack-restful_submit.rb
63
63
  - lib/rack/rack-restful_submit.rb
64
64
  - rack-restful_submit.gemspec
@@ -1,106 +0,0 @@
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