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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66254eab2313acf844dac9e2dda16ea53da054c2
4
- data.tar.gz: d18f21b31b329036568d4a5b6c4e4ac4bb817995
3
+ metadata.gz: 0699f5b129f7ef7a53754ec4214fd04886ed43a7
4
+ data.tar.gz: f788579f9e6c37f105ce360600c1fa85ee28d7a5
5
5
  SHA512:
6
- metadata.gz: f9b8e03bd2d1d1b8a3654faecd7b532b027332f93a076734298cf5312d3c4c19ec2067edfc52bbe9deeee47e33415a2cc8fca061a2dbcede79c8dc370ba8ba85
7
- data.tar.gz: a7bdb465ac018b679af4789aa1a909cf043f2a891ee6bed8dbe91b4beea1b1c5ad30ad71630db058505755affc79feb6a55edd4cc80a907b97163b288ed1071f
6
+ metadata.gz: d7e7d582c3a318ba52a827f06e67ad4956b6c7b0e1081120309854a98310428ab3df5f61468d72e40c1e7eb1eb710c8c37eca61becaa28ddc95fdf1aea36380d
7
+ data.tar.gz: bb22687489ea60b15770652a25c6350c0d0a0bae2b60958335ce33a7dc30a2073a85988062b9d5210c161a7fd95c94002f26ce8f374ee99eb5906891e98f70c2
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /.idea
11
+ /.byebug_history
11
12
  /bin
12
13
  /spec/dummy/tmp
13
14
  /spec/dummy/db/*.sqlite3
data/.travis.yml CHANGED
@@ -1,4 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.2.3
4
- before_install: gem install bundler -v 1.11.2
4
+ before_install:
5
+ - gem install bundler -v 1.11.2
6
+ script:
7
+ - RAILS_ENV=test bundle exec rake db:create db:migrate --trace
8
+ - bundle exec rspec spec
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
@@ -14,6 +14,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
17
19
  require "bundler/gem_tasks"
18
20
  require "rspec/core/rake_task"
19
21
 
@@ -12,8 +12,14 @@ module SubmitOnce
12
12
 
13
13
  # TODO: force or no
14
14
  def check_form_token
15
- if session[TOKEN_KEY] == params[TOKEN_KEY]
16
- session[TOKEN_KEY] = nil
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
- (session[TOKEN_KEY] = Digest::SHA1.hexdigest((Time.now.to_i + rand(0xffffff)).to_s)[0..39])
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
@@ -1,3 +1,3 @@
1
1
  module SubmitOnce
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -2,7 +2,8 @@ module SubmitOnce
2
2
  module ViewHelper
3
3
 
4
4
  def form_token_tag
5
- hidden_field_tag(TOKEN_KEY, gen_form_token)
5
+ key, token = gen_form_token
6
+ "#{hidden_field_tag(TOKEN_KEY, key)}#{hidden_field_tag(TOKEN_VALUE, token)}"
6
7
  end
7
8
  end
8
9
  end
data/lib/submit_once.rb CHANGED
@@ -1,4 +1,5 @@
1
- TOKEN_KEY = :__form_token
1
+ TOKEN_KEY = '__ft_k'.freeze
2
+ TOKEN_VALUE = '__ft_v'.freeze
2
3
 
3
4
  require "submit_once/version"
4
5
  require 'submit_once/railtie' if defined?(Rails)
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.2
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-02-29 00:00:00.000000000 Z
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