username 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +21 -0
- data/README.md +140 -0
- data/app/models/concerns/username/model.rb +58 -0
- data/lib/generators/templates/initializer.rb +15 -0
- data/lib/generators/username_generator.rb +15 -0
- data/lib/username/configuration.rb +29 -0
- data/lib/username/engine.rb +6 -0
- data/lib/username/railtie.rb +13 -0
- data/lib/username/version.rb +5 -0
- data/lib/username.rb +10 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d15b4e55705a2cd5238ad7e92a4cfee36886f9f602b21967c59ac9e5d7d3dd6
|
4
|
+
data.tar.gz: ee13b30fe24e8f37deb62bef82f5b8f35430a76bfb94d95de8482962d4faad4a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 771c98c923b738bbedb939f5beda10349d38e76aaecd3874c484b7fde7dae0e5862870d4cbea97060e8ca631ae483a1382d55b7c299ed8ad4f64c04b41ec9ef2
|
7
|
+
data.tar.gz: e6198551b71dc0adff9d0540e15f22a0c2b6b73a29ade87f5e36005f0078b33f734a91f4feb92afae0f86798f27d93eff91439f357cf3be1d295db81ff7036e7
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# Username
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/username.svg)](https://badge.fury.io/rb/username) <img src="https://travis-ci.org/jonhue/username.svg?branch=master" />
|
4
|
+
|
5
|
+
Usernames for ActiveRecord model.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Methods](#methods)
|
14
|
+
* [Configuration](#configuration)
|
15
|
+
* [To Do](#to-do)
|
16
|
+
* [Contributing](#contributing)
|
17
|
+
* [Contributors](#contributors)
|
18
|
+
* [Semantic versioning](#semantic-versioning)
|
19
|
+
* [License](#license)
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Username works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'username'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install username
|
38
|
+
|
39
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
gem 'username', github: 'jonhue/username'
|
43
|
+
```
|
44
|
+
|
45
|
+
Now run the generator:
|
46
|
+
|
47
|
+
$ rails g username
|
48
|
+
|
49
|
+
---
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
|
53
|
+
You can add usernames to an ActiveRecord model:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class User < ApplicationRecord
|
57
|
+
has_username
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
**Important:** Make sure to add a `username` string column to the corresponding database table.
|
62
|
+
|
63
|
+
Username adds a validator to prevent the use of invalid usernames.
|
64
|
+
|
65
|
+
### Methods
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# If a username is available for an ActiveRecord class
|
69
|
+
User.username_valid? 'test'
|
70
|
+
|
71
|
+
# If a username is available for an ActiveRecord model
|
72
|
+
User.first.username_available? 'test'
|
73
|
+
```
|
74
|
+
|
75
|
+
---
|
76
|
+
|
77
|
+
## Configuration
|
78
|
+
|
79
|
+
You can configure Username by passing a block to `configure`. This can be done in `config/initializers/username.rb`:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Username.configure do |config|
|
83
|
+
config.minlength = 1
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
* `minlength` Minimum length for usernames. Takes an integer. Defaults to `1`.
|
88
|
+
* `maxlength` Maximum length for usernames. Takes an integer. Defaults to `20`.
|
89
|
+
* `regex` Strings not matching this regular expression are invalid as usernames. Takes a regular expression. Defaults to `/\A[a-zA-Z0-9_\.]*\z/`.
|
90
|
+
* `global_uniqueness` Whether to validate usernames across all models or just the records model. Takes a boolean. Defaults to `true`.
|
91
|
+
|
92
|
+
---
|
93
|
+
|
94
|
+
## To Do
|
95
|
+
|
96
|
+
[Here](https://github.com/jonhue/username/projects/1) is the full list of current projects.
|
97
|
+
|
98
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/username/issues/new).
|
99
|
+
|
100
|
+
---
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
We hope that you will consider contributing to Username. Please read this short overview for some information about how to get started:
|
105
|
+
|
106
|
+
[Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
|
107
|
+
|
108
|
+
### Contributors
|
109
|
+
|
110
|
+
Give the people some :heart: who are working on this project. See them all at:
|
111
|
+
|
112
|
+
https://github.com/jonhue/username/graphs/contributors
|
113
|
+
|
114
|
+
### Semantic Versioning
|
115
|
+
|
116
|
+
Username follows Semantic Versioning 2.0 as defined at http://semver.org.
|
117
|
+
|
118
|
+
## License
|
119
|
+
|
120
|
+
MIT License
|
121
|
+
|
122
|
+
Copyright (c) 2018 Jonas Hübotter
|
123
|
+
|
124
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
125
|
+
of this software and associated documentation files (the "Software"), to deal
|
126
|
+
in the Software without restriction, including without limitation the rights
|
127
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
128
|
+
copies of the Software, and to permit persons to whom the Software is
|
129
|
+
furnished to do so, subject to the following conditions:
|
130
|
+
|
131
|
+
The above copyright notice and this permission notice shall be included in all
|
132
|
+
copies or substantial portions of the Software.
|
133
|
+
|
134
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
135
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
136
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
137
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
138
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
139
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
140
|
+
SOFTWARE.
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Username
|
2
|
+
module Model
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def has_username
|
8
|
+
Username.configuration&.models << self.name
|
9
|
+
validate :username_validation
|
10
|
+
end
|
11
|
+
|
12
|
+
def username_valid? username
|
13
|
+
unless username.nil?
|
14
|
+
valid = true
|
15
|
+
if Username.configuration.global_uniqueness
|
16
|
+
Username.configuration.models.each do |model|
|
17
|
+
valid = false if model.constantize.all.where(username: username).any?
|
18
|
+
end
|
19
|
+
else
|
20
|
+
valid = false if self.all.where(username: username).any?
|
21
|
+
end
|
22
|
+
valid = false if Username.configuration.forbidden.include?(username) || username.length < Username.configuration.minlength || username.length > Username.configuration.maxlength || username !~ Username.configuration.regex
|
23
|
+
valid
|
24
|
+
else
|
25
|
+
true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def username_available? username
|
31
|
+
unless username.nil?
|
32
|
+
valid = true
|
33
|
+
if Username.configuration.global_uniqueness
|
34
|
+
Username.configuration.models.each do |model|
|
35
|
+
if model == self.class.name
|
36
|
+
valid = false if model.constantize.all.where(username: username).where.not(id: id).any?
|
37
|
+
else
|
38
|
+
valid = false if model.constantize.all.where(username: username).any?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
valid = false if self.class.all.where(username: username).where.not(id: id).any?
|
43
|
+
end
|
44
|
+
valid = false if Username.configuration.forbidden.include?(username) || username.length < Username.configuration.minlength || username.length > Username.configuration.maxlength || username !~ Username.configuration.regex
|
45
|
+
valid
|
46
|
+
else
|
47
|
+
true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def username_validation
|
54
|
+
errors.add(:username, 'has already been taken') unless self.username_available? self.username
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Username.configure do |config|
|
2
|
+
|
3
|
+
# Minimum length for usernames
|
4
|
+
# config.minlength = 1
|
5
|
+
|
6
|
+
# Maximum length for usernames
|
7
|
+
# config.maxlength = 20
|
8
|
+
|
9
|
+
# Strings not matching this regular expression are invalid as usernames.
|
10
|
+
# config.regex = /\A[a-zA-Z0-9_\.]*\z/
|
11
|
+
|
12
|
+
# Whether to validate usernames across all models or just the records model.
|
13
|
+
# config.global_uniqueness = true
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class UsernameGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.join File.dirname(__FILE__), 'templates'
|
9
|
+
desc 'Install Username'
|
10
|
+
|
11
|
+
def create_initializer
|
12
|
+
template 'initializer.rb', 'config/initializers/username.rb'
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Username
|
2
|
+
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configure
|
8
|
+
self.configuration ||= Configuration.new
|
9
|
+
yield configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
class Configuration
|
13
|
+
|
14
|
+
attr_accessor :models
|
15
|
+
attr_accessor :minlength
|
16
|
+
attr_accessor :maxlength
|
17
|
+
attr_accessor :regex
|
18
|
+
attr_accessor :global_uniqueness
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@models = []
|
22
|
+
@minlength = 1
|
23
|
+
@maxlength = 20
|
24
|
+
@regex = /\A[a-zA-Z0-9_\.]*\z/
|
25
|
+
@global_uniqueness = true
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/username.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: username
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Hübotter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.52'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.52'
|
69
|
+
description: Usernames for ActiveRecord models.
|
70
|
+
email: me@jonhue.me
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- CHANGELOG.md
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
- app/models/concerns/username/model.rb
|
79
|
+
- lib/generators/templates/initializer.rb
|
80
|
+
- lib/generators/username_generator.rb
|
81
|
+
- lib/username.rb
|
82
|
+
- lib/username/configuration.rb
|
83
|
+
- lib/username/engine.rb
|
84
|
+
- lib/username/railtie.rb
|
85
|
+
- lib/username/version.rb
|
86
|
+
homepage: https://github.com/jonhue/username
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '2.3'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.7.4
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Usernames for ActiveRecord models
|
110
|
+
test_files: []
|