submit_once 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +5 -1
- data/README.md +4 -4
- data/Rakefile +2 -0
- data/lib/submit_once/controller_helper.rb +22 -3
- data/lib/submit_once/version.rb +1 -1
- data/lib/submit_once/view_helper.rb +2 -1
- data/lib/submit_once.rb +2 -1
- data/submit_once.gemspec +2 -0
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0699f5b129f7ef7a53754ec4214fd04886ed43a7
|
4
|
+
data.tar.gz: f788579f9e6c37f105ce360600c1fa85ee28d7a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7e7d582c3a318ba52a827f06e67ad4956b6c7b0e1081120309854a98310428ab3df5f61468d72e40c1e7eb1eb710c8c37eca61becaa28ddc95fdf1aea36380d
|
7
|
+
data.tar.gz: bb22687489ea60b15770652a25c6350c0d0a0bae2b60958335ce33a7dc30a2073a85988062b9d5210c161a7fd95c94002f26ce8f374ee99eb5906891e98f70c2
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# SubmitOnce
|
1
|
+
# SubmitOnce [![Build Status](https://travis-ci.org/bastengao/submit_once.svg?branch=master)](https://travis-ci.org/bastengao/submit_once)
|
2
2
|
|
3
3
|
Prevent from submitting form twice for Rails.
|
4
4
|
|
@@ -34,8 +34,8 @@ posts_controller.rb
|
|
34
34
|
class PostsController < ApplicationController
|
35
35
|
# return to root_url when form submit again
|
36
36
|
before_action :check_form_token!, only: [:create]
|
37
|
-
|
38
|
-
# OR custom before action
|
37
|
+
|
38
|
+
# OR custom before action
|
39
39
|
# before_action :custom_check_form_token
|
40
40
|
|
41
41
|
def index
|
@@ -47,7 +47,7 @@ class PostsController < ApplicationController
|
|
47
47
|
def create
|
48
48
|
...
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
private
|
52
52
|
def custom_check_form_token
|
53
53
|
# using `check_form_token`
|
data/Rakefile
CHANGED
@@ -12,8 +12,14 @@ module SubmitOnce
|
|
12
12
|
|
13
13
|
# TODO: force or no
|
14
14
|
def check_form_token
|
15
|
-
|
16
|
-
|
15
|
+
# TODO: 顺便做超时 token 清理工作
|
16
|
+
clean_expired_token
|
17
|
+
|
18
|
+
return false if params[TOKEN_KEY].blank?
|
19
|
+
|
20
|
+
form_token_key = params[TOKEN_KEY]
|
21
|
+
if session[form_token_key] == params[TOKEN_VALUE]
|
22
|
+
session.delete(form_token_key)
|
17
23
|
true
|
18
24
|
else
|
19
25
|
false
|
@@ -28,8 +34,21 @@ module SubmitOnce
|
|
28
34
|
end
|
29
35
|
|
30
36
|
def gen_form_token
|
37
|
+
clean_expired_token
|
38
|
+
|
39
|
+
@__form_token_key ||= "#{TOKEN_KEY}#{Time.now.to_i}"
|
31
40
|
@__form_token ||=
|
32
|
-
|
41
|
+
(session[@__form_token_key] = Digest::SHA1.hexdigest((Time.now.to_i + rand(0xffffff)).to_s)[0..39])
|
42
|
+
[@__form_token_key, @__form_token]
|
43
|
+
end
|
44
|
+
|
45
|
+
def clean_expired_token
|
46
|
+
session.each do |key, value|
|
47
|
+
if key.start_with? TOKEN_KEY
|
48
|
+
timestamp = Time.zone.at key.sub(TOKEN_KEY, '').to_i
|
49
|
+
session.delete(key) if timestamp < 30.minutes.ago
|
50
|
+
end
|
51
|
+
end
|
33
52
|
end
|
34
53
|
end
|
35
54
|
end
|
data/lib/submit_once/version.rb
CHANGED
data/lib/submit_once.rb
CHANGED
data/submit_once.gemspec
CHANGED
@@ -20,9 +20,11 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
22
|
spec.add_dependency 'rails', '>= 3.2', '< 5'
|
23
|
+
spec.add_development_dependency "byebug"
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.11"
|
24
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
26
|
spec.add_development_dependency "rspec", "~> 3.0"
|
26
27
|
spec.add_development_dependency 'rspec-rails'
|
27
28
|
spec.add_development_dependency 'sqlite3'
|
29
|
+
spec.add_development_dependency 'timecop'
|
28
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: submit_once
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bastengao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: byebug
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: bundler
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,6 +114,20 @@ dependencies:
|
|
100
114
|
- - ">="
|
101
115
|
- !ruby/object:Gem::Version
|
102
116
|
version: '0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: timecop
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
103
131
|
description: Prevent from submitting form twice for rails.
|
104
132
|
email:
|
105
133
|
- bastengao@gmail.com
|