hash_to_hidden_fields 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/hash_to_hidden_fields.gemspec +26 -0
- data/lib/action_view/helpers/hash_to_hidden_fields.rb +20 -0
- data/lib/hash_to_hidden_fields/version.rb +3 -0
- data/lib/hash_to_hidden_fields.rb +6 -0
- data/spec/hash_to_hidden_fields_spec.rb +59 -0
- data/spec/spec_helper.rb +3 -0
- metadata +121 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.2
|
5
|
+
- 1.9.3
|
6
|
+
- jruby-18mode # JRuby in 1.8 mode
|
7
|
+
- jruby-19mode # JRuby in 1.9 mode
|
8
|
+
- rbx-18mode
|
9
|
+
# - rbx-19mode # currently in active development, may or may not work for your project
|
10
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
11
|
+
script: bundle exec rspec spec
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# HashToHiddenFields [![Build Status](https://secure.travis-ci.org/brianhempel/hash_to_hidden_fields.png)](http://travis-ci.org/brianhempel/hash_to_hidden_fields)
|
2
|
+
|
3
|
+
Sometimes in your Rails app you want a user to begin an action, log in, then complete the action. You smash their params into the session, let the user log in, then pull their params out of the session when the login is successful. END SESSION STORE ABUSE! A better way is to have the login form resubmit the params.
|
4
|
+
|
5
|
+
Add this to your Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem "hash_to_hidden_fields"
|
9
|
+
```
|
10
|
+
|
11
|
+
Filter the params you want to save and turn them into hidden fields in your form:
|
12
|
+
|
13
|
+
```erb
|
14
|
+
<%= hash_to_hidden_fields(@params_to_save) %>
|
15
|
+
```
|
16
|
+
|
17
|
+
When the form is submitted, all the old params will come through! Nested arrays and nested hashes are all preserved.
|
18
|
+
|
19
|
+
## Help make it better!
|
20
|
+
|
21
|
+
Find a problem? [Open an issue](https://github.com/brianhempel/hash_to_hidden_fields/issues). Or, even better, code it yourself and send a pull request:
|
22
|
+
|
23
|
+
# fork it on github, then clone:
|
24
|
+
git clone git@github.com:your_username/hash_to_hidden_fields.git
|
25
|
+
bundle install
|
26
|
+
rspec
|
27
|
+
# hack away
|
28
|
+
git push
|
29
|
+
# then make a pull request
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
Public domain; no restrictions.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hash_to_hidden_fields/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hash_to_hidden_fields"
|
7
|
+
s.version = HashToHiddenFields::VERSION
|
8
|
+
s.authors = ["Brian Hempel"]
|
9
|
+
s.email = ["plasticchicken@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/brianhempel/hash_to_hidden_fields"
|
11
|
+
s.summary = %q{In goes a hash, out comes hidden fields. Great for preserving Rails parameters without abusing the session.}
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.rubyforge_project = "hash_to_hidden_fields"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
|
24
|
+
s.add_runtime_dependency "rack", "~> 1.2"
|
25
|
+
s.add_runtime_dependency "actionpack", "~> 3.0"
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'action_view/helpers'
|
3
|
+
|
4
|
+
module ActionView
|
5
|
+
module Helpers
|
6
|
+
module HashToHiddenFields
|
7
|
+
def hash_to_hidden_fields(hash)
|
8
|
+
query_string = Rack::Utils.build_nested_query(hash)
|
9
|
+
pairs = query_string.split(Rack::Utils::DEFAULT_SEP)
|
10
|
+
|
11
|
+
tags = pairs.map do |pair|
|
12
|
+
key, value = pair.split('=', 2).map { |str| Rack::Utils.unescape(str) }
|
13
|
+
hidden_field_tag(key, value)
|
14
|
+
end
|
15
|
+
|
16
|
+
tags.join("\n").html_safe
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
test_hash = {
|
5
|
+
"utf8" => "✓",
|
6
|
+
"_method" => "put",
|
7
|
+
"something" => {
|
8
|
+
"nested1" => "3",
|
9
|
+
"nested2" => "4"
|
10
|
+
},
|
11
|
+
"array" => [
|
12
|
+
"1", "2", "3", "abc"
|
13
|
+
],
|
14
|
+
"array of hashes" => [
|
15
|
+
{ "key 1" => "val 1" },
|
16
|
+
{ "ooh, nes<><><ting!" => ["x", "z", "y"]}
|
17
|
+
]
|
18
|
+
}
|
19
|
+
|
20
|
+
# Ruby 1.8.7 doesn't have an ordered hash, hence this mess
|
21
|
+
# want to make sure arrays come back in the correct order
|
22
|
+
expected_html_blocks = [
|
23
|
+
'<input id="utf8" name="utf8" type="hidden" value="✓" />',
|
24
|
+
'<input id="_method" name="_method" type="hidden" value="put" />',
|
25
|
+
'<input id="something_nested1" name="something[nested1]" type="hidden" value="3" />',
|
26
|
+
'<input id="something_nested2" name="something[nested2]" type="hidden" value="4" />',
|
27
|
+
<<-HTML,
|
28
|
+
<input id="array_" name="array[]" type="hidden" value="1" />
|
29
|
+
<input id="array_" name="array[]" type="hidden" value="2" />
|
30
|
+
<input id="array_" name="array[]" type="hidden" value="3" />
|
31
|
+
<input id="array_" name="array[]" type="hidden" value="abc" />
|
32
|
+
HTML
|
33
|
+
'<input id="array_of_hashes__key_1" name="array of hashes[][key 1]" type="hidden" value="val 1" />',
|
34
|
+
<<-HTML,
|
35
|
+
<input id="array_of_hashes__ooh__nes_____ting__" name="array of hashes[][ooh, nes<><><ting!][]" type="hidden" value="x" />
|
36
|
+
<input id="array_of_hashes__ooh__nes_____ting__" name="array of hashes[][ooh, nes<><><ting!][]" type="hidden" value="z" />
|
37
|
+
<input id="array_of_hashes__ooh__nes_____ting__" name="array of hashes[][ooh, nes<><><ting!][]" type="hidden" value="y" />
|
38
|
+
HTML
|
39
|
+
]
|
40
|
+
expected_html_blocks.map!(&:chomp)
|
41
|
+
|
42
|
+
describe ActionView::Base do
|
43
|
+
describe "#hash_to_hidden_fields" do
|
44
|
+
before :each do
|
45
|
+
@hash_to_hidden_fields = ActionView::Base.new.hash_to_hidden_fields(test_hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "produces the correct tags" do
|
49
|
+
# Ruby 1.8.7 doesn't have an ordered hash, hence this mess
|
50
|
+
expected_html_blocks.each do |tags|
|
51
|
+
@hash_to_hidden_fields.should include(tags)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "is the right size" do
|
56
|
+
@hash_to_hidden_fields.size.should == expected_html_blocks.join("\n").size
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_to_hidden_fields
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brian Hempel
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-28 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rack
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 11
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 2
|
46
|
+
version: "1.2"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: actionpack
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 7
|
58
|
+
segments:
|
59
|
+
- 3
|
60
|
+
- 0
|
61
|
+
version: "3.0"
|
62
|
+
type: :runtime
|
63
|
+
version_requirements: *id003
|
64
|
+
description: In goes a hash, out comes hidden fields. Great for preserving Rails parameters without abusing the session.
|
65
|
+
email:
|
66
|
+
- plasticchicken@gmail.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files: []
|
72
|
+
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- .rspec
|
76
|
+
- .travis.yml
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- hash_to_hidden_fields.gemspec
|
81
|
+
- lib/action_view/helpers/hash_to_hidden_fields.rb
|
82
|
+
- lib/hash_to_hidden_fields.rb
|
83
|
+
- lib/hash_to_hidden_fields/version.rb
|
84
|
+
- spec/hash_to_hidden_fields_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
homepage: https://github.com/brianhempel/hash_to_hidden_fields
|
87
|
+
licenses: []
|
88
|
+
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
112
|
+
requirements: []
|
113
|
+
|
114
|
+
rubyforge_project: hash_to_hidden_fields
|
115
|
+
rubygems_version: 1.8.17
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: In goes a hash, out comes hidden fields. Great for preserving Rails parameters without abusing the session.
|
119
|
+
test_files:
|
120
|
+
- spec/hash_to_hidden_fields_spec.rb
|
121
|
+
- spec/spec_helper.rb
|