submit_once 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85f1c862b474d561aa9f068ddabc0026584f6845
4
+ data.tar.gz: 8a5bf24515387032b101532da5e4ac85f01ba945
5
+ SHA512:
6
+ metadata.gz: 259325e8da4ae1cdffd80d59bab01e4fa6b03e7a5b76ce9f1fe89cf54e8571602fe447a68b3624c9062c9050be39bae22d5278893913e418b3e55cfa6339e389
7
+ data.tar.gz: 304b8c1466fe831110bd6c912cfcd413829694f78d989fb1bcb19efcde804068532d1e34a03777264f789429d192305501c265b14b620d14f9649f49dc131afb
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
11
+ /bin
12
+ /spec/dummy/tmp
13
+ /spec/dummy/db/*.sqlite3
14
+ /spec/dummy/log/*.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at bastengao@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in submit_once.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 bastengao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # SubmitOnce
2
+
3
+ Prevent from submitting form twice for Rails.
4
+
5
+ Scenario: when user wanna add a post, usually go to *new* page first, input some information and submit,
6
+ it will redirect to success page if these is no error. However after all if user click backward button on browser, it will render history page(the new page before), user could submit same form again.
7
+
8
+ `SubmitOnce` gem resolve this problem. Form could be submitted only browser fetch a new fresh one.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'submit_once'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ posts/new.html.erb
21
+ ```erb
22
+ <%= form_for @post do |f| %>
23
+ <!-- token tag here -->
24
+ <%= form_token_tag %>
25
+
26
+ <%= f.text_field :title %>
27
+ ...
28
+ <%= f.submit %>
29
+ <% end %>
30
+ ```
31
+
32
+ posts_controller.rb
33
+ ```ruby
34
+ class PostsController < ApplicationController
35
+ # return to root_url when form submit again
36
+ before_action :check_form_token!, only: [:create]
37
+
38
+ # OR custom before action
39
+ # before_action :custom_check_form_token
40
+
41
+ def index
42
+ end
43
+
44
+ def new
45
+ end
46
+
47
+ def create
48
+ ...
49
+ end
50
+
51
+ private
52
+ def custom_check_form_token
53
+ # using `check_form_token`
54
+ unless check_form_token
55
+ redirect_to posts_url, alert: 'Invalid form token'
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/submit_once. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DummyPlugin'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require "bundler/gem_tasks"
18
+ require "rspec/core/rake_task"
19
+
20
+ RSpec::Core::RakeTask.new(:spec)
21
+
22
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ require 'active_support/concern'
2
+
3
+ module SubmitOnce
4
+ module ControllerHelper
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ if respond_to?(:helper_method)
9
+ helper_method :gen_form_token
10
+ end
11
+ end
12
+
13
+ # TODO: force or no
14
+ def check_form_token
15
+ if session[TOKEN_KEY] == params[TOKEN_KEY]
16
+ session[TOKEN_KEY] = nil
17
+ true
18
+ else
19
+ false
20
+ end
21
+ end
22
+
23
+ def check_form_token!
24
+ unless check_form_token
25
+ # TODO: path, and i18n
26
+ redirect_to '/', notice: "Couldn't repeat submit form"
27
+ end
28
+ end
29
+
30
+ def gen_form_token
31
+ @__form_token ||=
32
+ (session[TOKEN_KEY] = Digest::SHA1.hexdigest((Time.now.to_i + rand(0xffffff)).to_s)[0..39])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails'
2
+ require 'submit_once/controller_helper'
3
+ require 'submit_once/View_helper'
4
+
5
+ module SubmitOnce
6
+ class Railtie < Rails::Railtie
7
+ initializer "submit_once.controller_helper" do
8
+ ActiveSupport.on_load :action_controller do
9
+ include ControllerHelper
10
+ end
11
+
12
+ ActiveSupport.on_load :action_view do
13
+ puts 'load action view'
14
+ include ViewHelper
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SubmitOnce
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ module SubmitOnce
2
+ module ViewHelper
3
+
4
+ def form_token_tag
5
+ hidden_field_tag(TOKEN_KEY, gen_form_token)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ TOKEN_KEY = :__form_token
2
+
3
+ require "submit_once/version"
4
+ require 'submit_once/railtie' if defined?(Rails)
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'submit_once/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "submit_once"
8
+ spec.version = SubmitOnce::VERSION
9
+ spec.authors = ["bastengao"]
10
+ spec.email = ["bastengao@gmail.com"]
11
+
12
+ spec.summary = %q{Prevent from submitting form twice for rails.}
13
+ spec.description = %q{Prevent from submitting form twice for rails.}
14
+ spec.homepage = "https://github.com/bastengao/submit_once"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(bin|test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'rails', '>= 3.2', '< 5'
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency 'rspec-rails'
27
+ spec.add_development_dependency 'sqlite3'
28
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: submit_once
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bastengao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.11'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: Prevent from submitting form twice for rails.
104
+ email:
105
+ - bastengao@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".travis.yml"
113
+ - CODE_OF_CONDUCT.md
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - lib/submit_once.rb
119
+ - lib/submit_once/controller_helper.rb
120
+ - lib/submit_once/railtie.rb
121
+ - lib/submit_once/version.rb
122
+ - lib/submit_once/view_helper.rb
123
+ - submit_once.gemspec
124
+ homepage: https://github.com/bastengao/submit_once
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Prevent from submitting form twice for rails.
148
+ test_files: []