resty_prefill 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +52 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +21 -0
- data/LICENSE.txt +20 -0
- data/README.md +109 -0
- data/Rakefile +28 -0
- data/lib/resty_prefill.rb +42 -0
- data/resty_prefill.gemspec +28 -0
- metadata +104 -0
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
+
#
|
19
|
+
# * Create a file at ~/.gitignore
|
20
|
+
# * Include files you want ignored
|
21
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
+
#
|
23
|
+
# After doing this, these files will be ignored in all your git projects,
|
24
|
+
# saving you from having to 'pollute' every project you touch with them
|
25
|
+
#
|
26
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
+
#
|
28
|
+
# For MacOS:
|
29
|
+
#
|
30
|
+
#.DS_Store
|
31
|
+
|
32
|
+
# For TextMate
|
33
|
+
#*.tmproj
|
34
|
+
#tmtags
|
35
|
+
|
36
|
+
# For emacs:
|
37
|
+
#*~
|
38
|
+
#\#*
|
39
|
+
#.\#*
|
40
|
+
|
41
|
+
# For vim:
|
42
|
+
.*.swp
|
43
|
+
.*.swo
|
44
|
+
|
45
|
+
# For redcar:
|
46
|
+
#.redcar
|
47
|
+
|
48
|
+
# For rubinius:
|
49
|
+
#*.rbc
|
50
|
+
|
51
|
+
tmp
|
52
|
+
README.html
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# gem 'rails', '>= 3.0.0'
|
7
|
+
gem 'rake'
|
8
|
+
# gem 'activemodel', '= 3.0.11'
|
9
|
+
# gem 'activesupport', '= 3.0.11'
|
10
|
+
|
11
|
+
# Add dependencies to develop your gem here.
|
12
|
+
# Include everything needed to run rake, tests, features, etc.
|
13
|
+
group :development do
|
14
|
+
gem "rspec", "~> 2.4.0"
|
15
|
+
gem "bundler"
|
16
|
+
end
|
17
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
rake (10.0.3)
|
6
|
+
rspec (2.4.0)
|
7
|
+
rspec-core (~> 2.4.0)
|
8
|
+
rspec-expectations (~> 2.4.0)
|
9
|
+
rspec-mocks (~> 2.4.0)
|
10
|
+
rspec-core (2.4.0)
|
11
|
+
rspec-expectations (2.4.0)
|
12
|
+
diff-lcs (~> 1.1.2)
|
13
|
+
rspec-mocks (2.4.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
bundler
|
20
|
+
rake
|
21
|
+
rspec (~> 2.4.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Paul A. Jungwirth
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
resty\_prefill
|
2
|
+
==============
|
3
|
+
|
4
|
+
The resty\_prefill gem fixes a problem with Rails' REST behavior when handling form-submit errors. I've written more about this problem [here](http://www.illuminatedcomputing.com/posts/2011/07/restless-doubts/), but basically it's this:
|
5
|
+
|
6
|
+
A typical Rails controller has these methods:
|
7
|
+
|
8
|
+
class WidgetsController < ApplicationController
|
9
|
+
|
10
|
+
def new
|
11
|
+
@widget = Widget.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
@widget = Widget.new(params[:widget])
|
16
|
+
if @widget.save
|
17
|
+
flash[:success] = 'Widget created.'
|
18
|
+
redirect_to widgets_path
|
19
|
+
else
|
20
|
+
render 'new'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def edit
|
25
|
+
@widget = Widget.find(params[:id])
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
@widget = Widget.find(params[:id])
|
30
|
+
if @widget.update_attributes(params[:widget])
|
31
|
+
flash[:success] = 'Widget updated.'
|
32
|
+
redirect_to widgets_path
|
33
|
+
else
|
34
|
+
render 'edit'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# ...
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
The problem is how this approach handles errors. To create a Widget, the user starts at `/widgets/new`, but after getting errors he winds up at just `/widgets`. That URL is non-GETtable, non-bookmarkable, and non-sharable. If you do Ctrl-L then Enter, you'll get a routing error. If you click "Like" or "Share", people following your URL get a routing error. Ideally form errors should send the user back to `/widgets/new`. Similarly with `/widgets/2/edit` vs. `/widgets/2`.
|
43
|
+
|
44
|
+
So resty\_prefill makes it easy to preserve RESTful URLs even with form errors. Just `include RestyPrefill` in your ApplicationController, then write your code like this:
|
45
|
+
|
46
|
+
class WidgetsController < ApplicationController
|
47
|
+
|
48
|
+
def new
|
49
|
+
@widget = Widget.new
|
50
|
+
prefill @widget
|
51
|
+
end
|
52
|
+
|
53
|
+
def create
|
54
|
+
@widget = Widget.new(params[:widget])
|
55
|
+
if @widget.save
|
56
|
+
flash[:success] = 'Widget created.'
|
57
|
+
redirect_to widgets_path
|
58
|
+
else
|
59
|
+
ready_prefill @widget, params[:widget]
|
60
|
+
redirect_to new_widget_path
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def edit
|
65
|
+
@widget = Widget.find(params[:id])
|
66
|
+
prefill @widget
|
67
|
+
end
|
68
|
+
|
69
|
+
def update
|
70
|
+
@widget = Widget.find(params[:id])
|
71
|
+
if @widget.update_attributes(params[:widget])
|
72
|
+
flash[:success] = 'Widget updated.'
|
73
|
+
redirect_to widgets_path
|
74
|
+
else
|
75
|
+
ready_prefill @widget, params[:widget]
|
76
|
+
redirect_to edit_widget_path(@widget)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# ...
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
The `prefill` method populates `@widget.errors` so your form displays the right messages and highlights the right fields. The `ready_prefill` method stores information in the session to make this possible.
|
85
|
+
|
86
|
+
|
87
|
+
Contributing to resty\_prefill
|
88
|
+
------------------------------
|
89
|
+
|
90
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
91
|
+
* Check out the issue tracker to make sure someone hasn't already requested and/or contributed it.
|
92
|
+
* Fork the project.
|
93
|
+
* Start a feature/bugfix branch.
|
94
|
+
* Commit and push until you are happy with your contribution.
|
95
|
+
* Make be sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
96
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, that is fine, but please isolate that change to its own commit so I can cherry-pick around it.
|
97
|
+
|
98
|
+
Commands for building/releasing/installing:
|
99
|
+
|
100
|
+
* `rake build`
|
101
|
+
* `rake install`
|
102
|
+
* `rake release`
|
103
|
+
|
104
|
+
Copyright
|
105
|
+
---------
|
106
|
+
|
107
|
+
Copyright (c) 2013 Illuminated Computing Inc.
|
108
|
+
See LICENSE.txt for further details.
|
109
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
require 'rspec/core'
|
15
|
+
require 'rspec/core/rake_task'
|
16
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
17
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
18
|
+
end
|
19
|
+
desc 'Default: run specs'
|
20
|
+
task :default => :spec
|
21
|
+
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
|
26
|
+
task :readme => [] do |task|
|
27
|
+
`markdown README.md >README.html`
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module RestyPrefill
|
2
|
+
|
3
|
+
VERSION = '1.0.0'
|
4
|
+
|
5
|
+
def redirect_and_prefill_for(obj)
|
6
|
+
(session[:redirect_and_prefill] ||= {})[obj.class.to_s] ||= {
|
7
|
+
attrs: {},
|
8
|
+
errors: []
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def clear_redirect_and_prefill_for(obj)
|
13
|
+
h = redirect_and_prefill_for(obj)
|
14
|
+
h[:attrs] = {}
|
15
|
+
h[:errors] = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def prefill(obj)
|
19
|
+
attrs = redirect_and_prefill_for(obj)[:attrs]
|
20
|
+
attrs.each do |k,v|
|
21
|
+
obj[:k] = v
|
22
|
+
end
|
23
|
+
|
24
|
+
errs = redirect_and_prefill_for(obj)[:errors]
|
25
|
+
errs.each do |attr, msg|
|
26
|
+
obj.errors.add(attr, msg)
|
27
|
+
end
|
28
|
+
ensure
|
29
|
+
clear_redirect_and_prefill_for(obj)
|
30
|
+
end
|
31
|
+
|
32
|
+
def ready_prefill(obj, attrs)
|
33
|
+
redirect_and_prefill_for(obj)[:attrs] = attrs
|
34
|
+
errs = []
|
35
|
+
obj.errors.each do |attr, msg|
|
36
|
+
errs << [attr, msg]
|
37
|
+
end
|
38
|
+
redirect_and_prefill_for(obj)[:errors] = errs
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.push File.dirname(__FILE__) + '/lib'
|
2
|
+
require 'resty_prefill'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "resty_prefill"
|
6
|
+
s.version = RestyPrefill::VERSION
|
7
|
+
s.date = "2013-03-10"
|
8
|
+
|
9
|
+
s.summary = "Used to preserve RESTful URLs in Rails despite form submit errors."
|
10
|
+
s.description = "Used to preserve RESTful URLs in Rails despite form submit errors."
|
11
|
+
|
12
|
+
s.authors = ["Paul A. Jungwirth"]
|
13
|
+
s.homepage = "http://github.com/pjungwir/resty_prefill"
|
14
|
+
s.email = "pj@illuminatedcomputing.com"
|
15
|
+
|
16
|
+
s.licenses = ["MIT"]
|
17
|
+
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
s.executables = []
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,fixtures}/*`.split("\n")
|
22
|
+
|
23
|
+
s.add_runtime_dependency 'rails', '>= 3.0.0'
|
24
|
+
s.add_development_dependency 'rspec', '~> 2.4.0'
|
25
|
+
s.add_development_dependency 'bundler', '>= 0'
|
26
|
+
|
27
|
+
end
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resty_prefill
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul A. Jungwirth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.4.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.4.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Used to preserve RESTful URLs in Rails despite form submit errors.
|
63
|
+
email: pj@illuminatedcomputing.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- .gitignore
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/resty_prefill.rb
|
75
|
+
- resty_prefill.gemspec
|
76
|
+
homepage: http://github.com/pjungwir/resty_prefill
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
hash: -618312344268099606
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.24
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Used to preserve RESTful URLs in Rails despite form submit errors.
|
104
|
+
test_files: []
|