username_not_reserved_validator 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: fc2fdca3103a23947c32a6080cba6de41359f707
4
+ data.tar.gz: 80bcdff40701a0197619fa4721f3251591d912cf
5
+ SHA512:
6
+ metadata.gz: 784fd21e15ff61183eaf295f654e31eedb6acf2543f519ec249eb9221a3573c59e73f64b81ac4f47c972838b9468b899a0ca1044b69788846e5a32cd187edffc
7
+ data.tar.gz: 5041c47d94c0268c6329d9e4aab211500cd58bcbbeb0943260423cb0cbed16b784295d621f8d190583957adf9fadbb6cf662f79eb8846c700f6bfa4508753409
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: WKDKB5K2vIiPuFD1KApoVPJjT4LPkoPIT
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ vendor/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in username_not_reserved_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 SHIOYA, Hiromu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # UsernameNotReservedValidator
2
+
3
+ [![Build Status](https://travis-ci.org/kwappa/username_not_reserved_validator.png)](https://travis-ci.org/kwappa/username_not_reserved_validator)
4
+
5
+ [![Coverage Status](https://coveralls.io/repos/kwappa/username_not_reserved_validator/badge.png?branch=master)](https://coveralls.io/r/kwappa/username_not_reserved_validator?branch=master)
6
+
7
+ [![Code Climate](https://codeclimate.com/github/kwappa/username_not_reserved_validator.png)](https://codeclimate.com/github/kwappa/username_not_reserved_validator)
8
+
9
+ custom validator for ActiveModel.
10
+
11
+ validates that username is not included in the reserved name list.
12
+
13
+ e.g:
14
+
15
+ * index
16
+ * home
17
+ * top
18
+ * ...
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'username_not_reserved_validator'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install username_not_reserved_validator
35
+
36
+ ## Usage
37
+
38
+ ### validation settings
39
+
40
+ write validation setting on your ActiveModel
41
+
42
+
43
+ ```
44
+ class User < ActiveRecord::Base
45
+ validates(:name, username_not_reserved: true)
46
+ )
47
+ ```
48
+
49
+ ### options
50
+
51
+ * `additional_reserved_names` (Array of String / default: `[]`)
52
+ * specify additional reserved names
53
+ * `case_insencitve` (Boolean / default: `true`)
54
+ * if set to `false`, comparison is case insencitive
55
+ * `message` (Symbol / default: `:invalid`)
56
+ * specify key of error message
57
+
58
+ ## Referenced resources
59
+
60
+ * http://qiita.com/phimcall/items/4c559b70f70ea7f1953b
61
+ * http://bitarts.jp/blog/archives/004363.html
62
+ * https://github.com/balexand/email_validator
63
+
64
+ with tons of thanks :sushi:
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Bundler.setup
4
+ require 'rspec/core/rake_task'
5
+
6
+ task default: :spec
7
+
8
+ desc "run spec"
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_opts = ["-c", "-fd"]
11
+ end
@@ -0,0 +1,264 @@
1
+ module UsernameNotReservedValidator::ReservedNames
2
+ def self.list
3
+ RESERVED_NAMES
4
+ end
5
+
6
+ RESERVED_NAMES = %w[
7
+ index
8
+ home
9
+ top
10
+ help
11
+ about
12
+ security
13
+ contact
14
+ connect
15
+ support
16
+ faq
17
+ form
18
+ mail
19
+ update
20
+ mobile
21
+ phone
22
+ portal
23
+ tour
24
+ tutorial
25
+ navigation
26
+ navi
27
+ manual
28
+ doc
29
+ company
30
+ store
31
+ shop
32
+ topic
33
+ news
34
+ information
35
+ info
36
+ howto
37
+ pr
38
+ press
39
+ release
40
+ sitemap
41
+ plan
42
+ price
43
+ business
44
+ premium
45
+ member
46
+ term
47
+ privacy
48
+ rule
49
+ inquiry
50
+ legal
51
+ policy
52
+ icon
53
+ image
54
+ img
55
+ photo
56
+ css
57
+ stylesheet
58
+ style
59
+ script
60
+ src
61
+ js
62
+ javascript
63
+ dist
64
+ asset
65
+ source
66
+ static
67
+ file
68
+ flash
69
+ swf
70
+ xml
71
+ json
72
+ sag
73
+ cgi
74
+ account
75
+ user
76
+ item
77
+ entry
78
+ article
79
+ page
80
+ archive
81
+ tag
82
+ category
83
+ event
84
+ contest
85
+ word
86
+ product
87
+ project
88
+ download
89
+ video
90
+ blog
91
+ diary
92
+ site
93
+ popular
94
+ i
95
+ my
96
+ me
97
+ owner
98
+ profile
99
+ self
100
+ old
101
+ first
102
+ last
103
+ start
104
+ end
105
+ special
106
+ design
107
+ theme
108
+ purpose
109
+ book
110
+ read
111
+ organization
112
+ community
113
+ group
114
+ all
115
+ status
116
+ state
117
+ search
118
+ explore
119
+ share
120
+ feature
121
+ upload
122
+ rss
123
+ atom
124
+ widget
125
+ api
126
+ wiki
127
+ bookmark
128
+ captcha
129
+ comment
130
+ jump
131
+ ranking
132
+ setting
133
+ config
134
+ tool
135
+ connect
136
+ notify
137
+ recent
138
+ report
139
+ system
140
+ sys
141
+ message
142
+ msg
143
+ log
144
+ analysis
145
+ query
146
+ call
147
+ calendar
148
+ friend
149
+ graph
150
+ watch
151
+ cart
152
+ activity
153
+ password
154
+ auth
155
+ session
156
+ register
157
+ login
158
+ logout
159
+ signup
160
+ signin
161
+ signout
162
+ forgot
163
+ admin
164
+ root
165
+ secure
166
+ get
167
+ show
168
+ create
169
+ edit
170
+ update
171
+ post
172
+ destroy
173
+ delete
174
+ remove
175
+ reset
176
+ error
177
+ new
178
+ dashboard
179
+ recruit
180
+ join
181
+ offer
182
+ career
183
+ corp
184
+ cgi-bin
185
+ server-status
186
+ balancer-manager
187
+ ldap-status
188
+ server-info
189
+ svn
190
+ as
191
+ by
192
+ if
193
+ is
194
+ on
195
+ or
196
+ add
197
+ dir
198
+ off
199
+ out
200
+ put
201
+ case
202
+ else
203
+ find
204
+ then
205
+ when
206
+ count
207
+ order
208
+ select
209
+ switch
210
+ school
211
+ developer
212
+ dev
213
+ test
214
+ bug
215
+ code
216
+ guest
217
+ app
218
+ maintenance
219
+ roc
220
+ id
221
+ bot
222
+ game
223
+ forum
224
+ contribute
225
+ usage
226
+ feed
227
+ ad
228
+ service
229
+ official
230
+ language
231
+ repository
232
+ spec
233
+ license
234
+ asct
235
+ dictionary
236
+ dict
237
+ version
238
+ ver
239
+ gift
240
+ alpha
241
+ beta
242
+ tux
243
+ year
244
+ public
245
+ private
246
+ default
247
+ request
248
+ req
249
+ data
250
+ master
251
+ die
252
+ exit
253
+ eval
254
+ issue
255
+ thread
256
+ diagram
257
+ undef
258
+ nan
259
+ null
260
+ empty
261
+ 0
262
+ www
263
+ ]
264
+ end
@@ -0,0 +1,27 @@
1
+ class UsernameNotReservedValidator < ActiveModel::EachValidator
2
+ require 'username_not_reserved_validator/reserved_names'
3
+
4
+ @@default_options = { case_insencitive: true }
5
+
6
+ def self.default_options
7
+ @@default_options
8
+ end
9
+
10
+ def validate_each(record, attribute, value)
11
+ options = @@default_options.merge(self.options)
12
+ additional_reserved_names = options[:additional_reserved_names] || []
13
+ words = ReservedNames.list + additional_reserved_names
14
+ words += words.map(&:pluralize)
15
+
16
+ if options[:case_insencitive]
17
+ words.map!(&:downcase)
18
+ username = value.downcase
19
+ else
20
+ username = value
21
+ end
22
+
23
+ if words.include?(username)
24
+ record.errors[attribute] << (options[:message] || :invalid)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'active_model'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'username_not_reserved_validator'
9
+
10
+ class TestModel
11
+ include ActiveModel::Validations
12
+
13
+ def initialize(attributes = {})
14
+ @attributes = attributes
15
+ end
16
+
17
+ def read_attribute_for_validation(key)
18
+ @attributes[key]
19
+ end
20
+ end
21
+
22
+ class WithoutValidationUser < TestModel
23
+ end
24
+
25
+ class WithValidationUser < TestModel
26
+ validates :name, username_not_reserved: true
27
+ end
28
+
29
+ class WithCaseInsencitiveValidationUser < TestModel
30
+ validates :name, username_not_reserved: { case_insencitive: false }
31
+ end
32
+
33
+ class WithAdditionalReservedNamesValidationUser < TestModel
34
+ validates :name, username_not_reserved: { additional_reserved_names: %w[additional_reserved_username] }
35
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsernameNotReservedValidator do
4
+
5
+ let(:reserved_usernames) { UsernameNotReservedValidator::ReservedNames.list }
6
+ let(:valid_username) { 'valid_username' }
7
+ let(:invalid_username) { reserved_usernames.first(30).sample }
8
+ let(:invalid_camelized_username) { reserved_usernames.first(30).sample.camelize }
9
+ let(:invalid_pluralized_username) { reserved_usernames.first(30).sample.pluralize }
10
+ let(:invalid_additional_username) { 'additional_reserved_username' }
11
+
12
+
13
+ describe 'sample user name' do
14
+ context 'when valid' do
15
+ it 'does not included in reserved list' do
16
+ expect(reserved_usernames.map(&:downcase)).to_not include(valid_username)
17
+ expect(reserved_usernames.map(&:downcase).map(&:pluralize)).to_not include(valid_username)
18
+ end
19
+ end
20
+
21
+ context 'when invalid' do
22
+ it 'included in reserved list' do
23
+ expect(reserved_usernames).to include(invalid_username)
24
+ end
25
+
26
+ context 'when camelized invalid name' do
27
+ it 'included only downcase list' do
28
+ expect(reserved_usernames).to_not include(invalid_camelized_username)
29
+ expect(reserved_usernames.map(&:downcase)).to include(invalid_camelized_username.downcase)
30
+ end
31
+ end
32
+
33
+ context 'when pluarized invalid name' do
34
+ it 'included in pluralized list' do
35
+ expect(reserved_usernames).to include(invalid_pluralized_username.singularize)
36
+ end
37
+ end
38
+
39
+ context 'when additional invalid name' do
40
+ it 'does not included in reserved list' do
41
+ expect(reserved_usernames.map(&:downcase)).to_not include(invalid_additional_username)
42
+ expect(reserved_usernames.map(&:downcase).map(&:pluralize)).to_not include(invalid_additional_username)
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ shared_examples_for 'accepts username' do
49
+ it { expect(user).to be_valid }
50
+ end
51
+
52
+ shared_examples_for 'rejects username' do
53
+ it { expect(user).to_not be_valid }
54
+ end
55
+
56
+
57
+ describe 'model without validation' do
58
+ subject(:user) { WithoutValidationUser.new(name: username) }
59
+
60
+ context 'with valid username' do
61
+ let(:username) { valid_username }
62
+ include_examples 'accepts username'
63
+ end
64
+
65
+ context 'with invalid username' do
66
+ let(:username) { invalid_username }
67
+ include_examples 'accepts username'
68
+ end
69
+
70
+ context 'with invalid camelized username' do
71
+ let(:username) { invalid_camelized_username }
72
+ include_examples 'accepts username'
73
+ end
74
+
75
+ context 'with invalid pluralized username' do
76
+ let(:username) { invalid_pluralized_username }
77
+ include_examples 'accepts username'
78
+ end
79
+
80
+ context 'with additionals invalid username' do
81
+ let(:username) { invalid_additional_username }
82
+ include_examples 'accepts username'
83
+ end
84
+ end
85
+
86
+ describe 'model with validation' do
87
+ subject(:user) { WithValidationUser.new(name: username) }
88
+
89
+ context 'with valid username' do
90
+ let(:username) { valid_username }
91
+ include_examples 'accepts username'
92
+ end
93
+
94
+ context 'with invalid username' do
95
+ let(:username) { invalid_username }
96
+ include_examples 'rejects username'
97
+ end
98
+
99
+ context 'with invalid camelized username' do
100
+ let(:username) { invalid_camelized_username }
101
+ include_examples 'rejects username'
102
+ end
103
+
104
+ context 'with invalid pluralized username' do
105
+ let(:username) { invalid_pluralized_username }
106
+ include_examples 'rejects username'
107
+ end
108
+ end
109
+
110
+ describe 'model with case insencitive validation' do
111
+ subject(:user) { WithCaseInsencitiveValidationUser.new(name: username) }
112
+
113
+ context 'with valid username' do
114
+ let(:username) { valid_username }
115
+ include_examples 'accepts username'
116
+ end
117
+
118
+ context 'with invalid username' do
119
+ let(:username) { invalid_username }
120
+ include_examples 'rejects username'
121
+ end
122
+
123
+ context 'with invalid camelized username' do
124
+ let(:username) { invalid_camelized_username }
125
+ include_examples 'accepts username'
126
+ end
127
+
128
+ context 'with invalid pluralized username' do
129
+ let(:username) { invalid_pluralized_username }
130
+ include_examples 'rejects username'
131
+ end
132
+ end
133
+
134
+ describe 'model with validation and additional reserved username' do
135
+ subject(:user) { WithAdditionalReservedNamesValidationUser.new(name: username) }
136
+
137
+ context 'with additionals invalid username' do
138
+ let(:username) { invalid_additional_username }
139
+ include_examples 'rejects username'
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "username_not_reserved_validator"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["SHIOYA, Hiromu"]
9
+ spec.email = ["kwappa.856@gmail.com"]
10
+ spec.summary = "validates that username is not included in reserved list."
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/kwappa/username_not_reserved_validator"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activemodel"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: username_not_reserved_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SHIOYA, Hiromu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: validates that username is not included in reserved list.
70
+ email:
71
+ - kwappa.856@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".coveralls.yml"
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/username_not_reserved_validator.rb
84
+ - lib/username_not_reserved_validator/reserved_names.rb
85
+ - spec/spec_helper.rb
86
+ - spec/username_not_reserved_validator_spec.rb
87
+ - username_not_reserved_validator.gemspec
88
+ homepage: https://github.com/kwappa/username_not_reserved_validator
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: validates that username is not included in reserved list.
112
+ test_files:
113
+ - spec/spec_helper.rb
114
+ - spec/username_not_reserved_validator_spec.rb