randomized_field 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +46 -0
- data/.rspec +3 -0
- data/.rubocop.yml +24 -0
- data/.travis.yml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +81 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -68
- data/Rakefile +4 -35
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/randomized_field.rb +18 -4
- data/lib/randomized_field/callback.rb +30 -0
- data/lib/randomized_field/generator.rb +23 -0
- data/lib/randomized_field/version.rb +1 -2
- data/randomized_field.gemspec +49 -0
- metadata +133 -144
- data/MIT-LICENSE +0 -20
- data/lib/randomized_field/base.rb +0 -21
- data/lib/randomized_field/defaults.rb +0 -8
- data/lib/randomized_field/randomizer.rb +0 -31
- data/lib/tasks/randomized_field_tasks.rake +0 -4
- data/test/dummy/Rakefile +0 -7
- data/test/dummy/app/assets/javascripts/application.js +0 -9
- data/test/dummy/app/assets/stylesheets/application.css +0 -7
- data/test/dummy/app/controllers/application_controller.rb +0 -3
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/default_model.rb +0 -3
- data/test/dummy/app/models/prefix_model.rb +0 -3
- data/test/dummy/app/models/single_character_model.rb +0 -3
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -45
- data/test/dummy/config/boot.rb +0 -10
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -30
- data/test/dummy/config/environments/production.rb +0 -60
- data/test/dummy/config/environments/test.rb +0 -39
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/inflections.rb +0 -10
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -7
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -5
- data/test/dummy/config/routes.rb +0 -58
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20120204224641_create_default_models.rb +0 -9
- data/test/dummy/db/migrate/20120204235832_create_prefix_models.rb +0 -9
- data/test/dummy/db/migrate/20120205000853_create_single_character_models.rb +0 -9
- data/test/dummy/db/schema.rb +0 -34
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +0 -269
- data/test/dummy/log/test.log +0 -1186
- data/test/dummy/public/404.html +0 -26
- data/test/dummy/public/422.html +0 -26
- data/test/dummy/public/500.html +0 -26
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +0 -6
- data/test/dummy/test/fixtures/default_models.yml +0 -7
- data/test/dummy/test/fixtures/prefix_models.yml +0 -7
- data/test/dummy/test/fixtures/simple_models.yml +0 -7
- data/test/dummy/test/fixtures/single_character_models.yml +0 -7
- data/test/dummy/test/unit/default_model_test.rb +0 -7
- data/test/dummy/test/unit/prefix_model_test.rb +0 -7
- data/test/dummy/test/unit/single_character_model_test.rb +0 -7
- data/test/randomized_field_test.rb +0 -69
- data/test/test_helper.rb +0 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9b672711694faa07ea4ae48df26a552238f5a48fa4f04571ee2167468d354055
|
4
|
+
data.tar.gz: 698f94b12b4948c6942cdae923dc3198929ea4aafb7ad20b56eab8ddda4485ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f50da6ab04ca124bf770d4181e539e6a9d0948f1c8b6b28504f6047db481e42fbae0d7c6d1605d5e2a7e88c23853e16afeb97451c5d161ffd190b54448e46c2a
|
7
|
+
data.tar.gz: ac0f57a008b5b8d8668917eae652cee28581530fd3be99e1f71c99c88ee1a63aa15361bf0a74bcfd39b65d8560a08763d0483643727c4d518caa129cf4e8b675
|
data/.gitignore
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# rspec failure tracking
|
14
|
+
.rspec_status
|
15
|
+
|
16
|
+
# Used by dotenv library to load environment variables.
|
17
|
+
# .env
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
vendor/Pods/
|
21
|
+
.dat*
|
22
|
+
.repl_history
|
23
|
+
build/
|
24
|
+
*.bridgesupport
|
25
|
+
build-iPhoneOS/
|
26
|
+
build-iPhoneSimulator/
|
27
|
+
|
28
|
+
## Documentation cache and generated files:
|
29
|
+
/.yardoc/
|
30
|
+
/_yardoc/
|
31
|
+
/doc/
|
32
|
+
/rdoc/
|
33
|
+
|
34
|
+
## Environment normalization:
|
35
|
+
/.bundle/
|
36
|
+
/vendor/bundle
|
37
|
+
/lib/bundler/man/
|
38
|
+
|
39
|
+
# for a library or gem, you might want to ignore these files since the code is
|
40
|
+
# intended to run in multiple environments; otherwise, check them in:
|
41
|
+
# Gemfile.lock
|
42
|
+
# .ruby-version
|
43
|
+
# .ruby-gemset
|
44
|
+
|
45
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
46
|
+
.rvmrc
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.5+
|
5
|
+
Exclude:
|
6
|
+
- ./*.gemspec
|
7
|
+
|
8
|
+
Metrics/BlockLength:
|
9
|
+
ExcludedMethods: ['context', 'describe']
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 100
|
13
|
+
|
14
|
+
RSpec/BeforeAfterAll:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/BlockDelimiters:
|
18
|
+
EnforcedStyle: braces_for_chaining
|
19
|
+
|
20
|
+
Style/FrozenStringLiteralComment:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/StringLiterals:
|
24
|
+
EnforcedStyle: double_quotes
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
randomized_field (0.1.0)
|
5
|
+
activerecord (>= 4.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.1)
|
11
|
+
activesupport (= 5.2.1)
|
12
|
+
activerecord (5.2.1)
|
13
|
+
activemodel (= 5.2.1)
|
14
|
+
activesupport (= 5.2.1)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.1)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
arel (9.0.0)
|
22
|
+
ast (2.4.0)
|
23
|
+
concurrent-ruby (1.0.5)
|
24
|
+
diff-lcs (1.3)
|
25
|
+
i18n (1.1.1)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
jaro_winkler (1.5.1)
|
28
|
+
minitest (5.11.3)
|
29
|
+
parallel (1.12.1)
|
30
|
+
parser (2.5.1.2)
|
31
|
+
ast (~> 2.4.0)
|
32
|
+
powerpack (0.1.2)
|
33
|
+
rainbow (3.0.0)
|
34
|
+
rake (10.5.0)
|
35
|
+
rspec (3.8.0)
|
36
|
+
rspec-core (~> 3.8.0)
|
37
|
+
rspec-expectations (~> 3.8.0)
|
38
|
+
rspec-mocks (~> 3.8.0)
|
39
|
+
rspec-core (3.8.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-expectations (3.8.2)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-mocks (3.8.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.8.0)
|
47
|
+
rspec-support (3.8.0)
|
48
|
+
rubocop (0.59.2)
|
49
|
+
jaro_winkler (~> 1.5.1)
|
50
|
+
parallel (~> 1.10)
|
51
|
+
parser (>= 2.5, != 2.5.1.1)
|
52
|
+
powerpack (~> 0.1)
|
53
|
+
rainbow (>= 2.2.2, < 4.0)
|
54
|
+
ruby-progressbar (~> 1.7)
|
55
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
56
|
+
rubocop-rspec (1.30.0)
|
57
|
+
rubocop (>= 0.58.0)
|
58
|
+
ruby-progressbar (1.10.0)
|
59
|
+
shoulda-matchers (3.1.2)
|
60
|
+
activesupport (>= 4.0.0)
|
61
|
+
sqlite3 (1.3.13)
|
62
|
+
thread_safe (0.3.6)
|
63
|
+
tzinfo (1.2.5)
|
64
|
+
thread_safe (~> 0.1)
|
65
|
+
unicode-display_width (1.4.0)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
bundler (~> 1.16)
|
72
|
+
rake (~> 10.0)
|
73
|
+
randomized_field!
|
74
|
+
rspec (~> 3.0)
|
75
|
+
rubocop (~> 0.59)
|
76
|
+
rubocop-rspec (~> 1.30)
|
77
|
+
shoulda-matchers (~> 3.1)
|
78
|
+
sqlite3
|
79
|
+
|
80
|
+
BUNDLED WITH
|
81
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Strive Talent, Inc. (dba Strive)
|
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
CHANGED
@@ -1,92 +1,57 @@
|
|
1
1
|
# RandomizedField
|
2
2
|
|
3
|
-
|
3
|
+
RandomizedField is a simple module for ActiveRecord that populates a field with a unique, randomly-generated string. This is useful for generating one-off tokens or creating a non-incrementing unique ID.
|
4
4
|
|
5
|
-
|
6
|
-
randomly-generated string. This is useful for generating one-off tokens or creating a
|
7
|
-
non-incrementing unique ID.
|
8
|
-
|
9
|
-
Oftentimes we want a randomized unique ID so that we don't reveal information about how
|
10
|
-
large our database is, or to create an unguessable (and perhaps temporary) token for
|
11
|
-
things like password resets, etc.
|
5
|
+
Oftentimes we want a randomized unique ID so that we don't reveal information about how large our database is or to create an unguessable (and perhaps temporary) token for things like password resets, etc.
|
12
6
|
|
13
7
|
You might use RandomizedField to generate:
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
## Features
|
9
|
+
- Order numbers, return authorization numbers, etc. for an e-commerce system
|
10
|
+
- Unique referral IDs or invitation tokens for each user of your site
|
11
|
+
- Tokens for password reset emails
|
12
|
+
- Temporary tokens embedded in an email that automatically log users in
|
21
13
|
|
22
|
-
|
23
|
-
You can control the length of the string, what characters are used to generate the string, and what
|
24
|
-
prefix to prepend to the string (if any).
|
14
|
+
## Installation and Usage
|
25
15
|
|
26
|
-
|
16
|
+
1. Add the `randomized_field` gem to your application's Gemfile:
|
27
17
|
|
28
|
-
|
29
|
-
|
30
|
-
|
18
|
+
```ruby
|
19
|
+
gem 'randomized_field'
|
20
|
+
```
|
21
|
+
2. Run `bundle` to install the gem
|
31
22
|
|
32
|
-
|
23
|
+
```console
|
24
|
+
$ bundle
|
25
|
+
```
|
33
26
|
|
34
|
-
|
27
|
+
Or install it directly with the `gem` command:
|
35
28
|
|
36
|
-
|
29
|
+
```console
|
30
|
+
$ gem install randomized_field
|
31
|
+
```
|
37
32
|
|
38
|
-
|
33
|
+
3. Include `RandomizedField` in the models you care about (or in a shared base class like `ApplicationRecord` if you want it available to every model):
|
39
34
|
|
40
|
-
|
41
|
-
|
35
|
+
```ruby
|
36
|
+
class Article < ApplicationRecord
|
37
|
+
include RandomizedField
|
42
38
|
|
43
|
-
|
39
|
+
randomized_field :slug, length: 10
|
40
|
+
end
|
41
|
+
```
|
44
42
|
|
45
|
-
|
46
|
-
class User < ActiveRecord::Base
|
47
|
-
randomized_field :random_id, :length => 6, :prefix => 'U'
|
48
|
-
end
|
43
|
+
**Note**: RandomizedField won't do anything unless you also call `randomized_field` with the name of the field you want to populate.
|
49
44
|
|
50
|
-
|
45
|
+
## Development
|
51
46
|
|
52
|
-
|
53
|
-
|
54
|
-
User.find_by_random_id(params[:random_id])
|
55
|
-
```
|
56
|
-
|
57
|
-
## Bugs
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
48
|
|
59
|
-
|
60
|
-
tracker](http://github.com/jfarmer/randomized_field/issues) for this project.
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
61
50
|
|
62
|
-
|
63
|
-
|
64
|
-
* **Version information for RandomizedField, Rails and Ruby.**
|
65
|
-
* Full stack trace and error message (if you have them).
|
66
|
-
* Any snippets of relevant model, view or controller code that shows how you
|
67
|
-
are using RandomizedField.
|
51
|
+
## Contributing
|
68
52
|
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/StriveTalent/randomized_field.
|
69
54
|
|
70
55
|
## License
|
71
56
|
|
72
|
-
|
73
|
-
|
74
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
75
|
-
a copy of this software and associated documentation files (the
|
76
|
-
"Software"), to deal in the Software without restriction, including
|
77
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
78
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
79
|
-
permit persons to whom the Software is furnished to do so, subject to
|
80
|
-
the following conditions:
|
81
|
-
|
82
|
-
The above copyright notice and this permission notice shall be
|
83
|
-
included in all copies or substantial portions of the Software.
|
84
|
-
|
85
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
86
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
87
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
88
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
89
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
90
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
91
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
92
|
-
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). See the `LICENSE.txt` file.
|
data/Rakefile
CHANGED
@@ -1,37 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
rescue LoadError
|
10
|
-
require 'rdoc/rdoc'
|
11
|
-
require 'rake/rdoctask'
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
14
3
|
|
15
|
-
|
16
|
-
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = 'RandomizedField'
|
18
|
-
rdoc.options << '--line-numbers'
|
19
|
-
rdoc.rdoc_files.include('README.rdoc')
|
20
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
-
end
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
22
5
|
|
23
|
-
|
24
|
-
|
25
|
-
Bundler::GemHelper.install_tasks
|
26
|
-
|
27
|
-
require 'rake/testtask'
|
28
|
-
|
29
|
-
Rake::TestTask.new(:test) do |t|
|
30
|
-
t.libs << 'lib'
|
31
|
-
t.libs << 'test'
|
32
|
-
t.pattern = 'test/**/*_test.rb'
|
33
|
-
t.verbose = false
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
task :default => :test
|
6
|
+
task default: :spec
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "randomized_field"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/randomized_field.rb
CHANGED
@@ -1,4 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
|
1
|
+
require "randomized_field/version"
|
2
|
+
require "randomized_field/callback"
|
3
|
+
require "randomized_field/generator"
|
4
|
+
|
5
|
+
require "active_support/concern"
|
6
|
+
|
7
|
+
# Top-level module that adds the randomized_field method to ActiveRecord
|
8
|
+
# objects. Include in individual models or ApplicationRecord.
|
9
|
+
module RandomizedField
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def randomized_field(field_name, **opts, &block)
|
14
|
+
before_create RandomizedField::Callback.new(field_name, **opts, &block)
|
15
|
+
validates field_name, uniqueness: true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module RandomizedField
|
2
|
+
# Implements the before_create ActiveRecord callback used to set the value
|
3
|
+
# of the randomized field.
|
4
|
+
class Callback
|
5
|
+
attr_reader :field_name, :generator
|
6
|
+
private :field_name, :generator
|
7
|
+
|
8
|
+
def initialize(field_name, **generator_opts, &block)
|
9
|
+
@field_name = field_name
|
10
|
+
@generator = Generator.new(generator_opts, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_create(record)
|
14
|
+
record[field_name] ||= new_field_value(record)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def new_field_value(record)
|
20
|
+
old_record = true
|
21
|
+
|
22
|
+
until old_record.nil?
|
23
|
+
new_value = generator.generate
|
24
|
+
old_record = record.class.find_by(field_name => new_value)
|
25
|
+
end
|
26
|
+
|
27
|
+
new_value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|