randomized_field 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +46 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +24 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +5 -0
  7. data/Gemfile.lock +81 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +33 -68
  10. data/Rakefile +4 -35
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/randomized_field.rb +18 -4
  14. data/lib/randomized_field/callback.rb +30 -0
  15. data/lib/randomized_field/generator.rb +23 -0
  16. data/lib/randomized_field/version.rb +1 -2
  17. data/randomized_field.gemspec +49 -0
  18. metadata +133 -144
  19. data/MIT-LICENSE +0 -20
  20. data/lib/randomized_field/base.rb +0 -21
  21. data/lib/randomized_field/defaults.rb +0 -8
  22. data/lib/randomized_field/randomizer.rb +0 -31
  23. data/lib/tasks/randomized_field_tasks.rake +0 -4
  24. data/test/dummy/Rakefile +0 -7
  25. data/test/dummy/app/assets/javascripts/application.js +0 -9
  26. data/test/dummy/app/assets/stylesheets/application.css +0 -7
  27. data/test/dummy/app/controllers/application_controller.rb +0 -3
  28. data/test/dummy/app/helpers/application_helper.rb +0 -2
  29. data/test/dummy/app/models/default_model.rb +0 -3
  30. data/test/dummy/app/models/prefix_model.rb +0 -3
  31. data/test/dummy/app/models/single_character_model.rb +0 -3
  32. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  33. data/test/dummy/config.ru +0 -4
  34. data/test/dummy/config/application.rb +0 -45
  35. data/test/dummy/config/boot.rb +0 -10
  36. data/test/dummy/config/database.yml +0 -25
  37. data/test/dummy/config/environment.rb +0 -5
  38. data/test/dummy/config/environments/development.rb +0 -30
  39. data/test/dummy/config/environments/production.rb +0 -60
  40. data/test/dummy/config/environments/test.rb +0 -39
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  42. data/test/dummy/config/initializers/inflections.rb +0 -10
  43. data/test/dummy/config/initializers/mime_types.rb +0 -5
  44. data/test/dummy/config/initializers/secret_token.rb +0 -7
  45. data/test/dummy/config/initializers/session_store.rb +0 -8
  46. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  47. data/test/dummy/config/locales/en.yml +0 -5
  48. data/test/dummy/config/routes.rb +0 -58
  49. data/test/dummy/db/development.sqlite3 +0 -0
  50. data/test/dummy/db/migrate/20120204224641_create_default_models.rb +0 -9
  51. data/test/dummy/db/migrate/20120204235832_create_prefix_models.rb +0 -9
  52. data/test/dummy/db/migrate/20120205000853_create_single_character_models.rb +0 -9
  53. data/test/dummy/db/schema.rb +0 -34
  54. data/test/dummy/db/test.sqlite3 +0 -0
  55. data/test/dummy/log/development.log +0 -269
  56. data/test/dummy/log/test.log +0 -1186
  57. data/test/dummy/public/404.html +0 -26
  58. data/test/dummy/public/422.html +0 -26
  59. data/test/dummy/public/500.html +0 -26
  60. data/test/dummy/public/favicon.ico +0 -0
  61. data/test/dummy/script/rails +0 -6
  62. data/test/dummy/test/fixtures/default_models.yml +0 -7
  63. data/test/dummy/test/fixtures/prefix_models.yml +0 -7
  64. data/test/dummy/test/fixtures/simple_models.yml +0 -7
  65. data/test/dummy/test/fixtures/single_character_models.yml +0 -7
  66. data/test/dummy/test/unit/default_model_test.rb +0 -7
  67. data/test/dummy/test/unit/prefix_model_test.rb +0 -7
  68. data/test/dummy/test/unit/single_character_model_test.rb +0 -7
  69. data/test/randomized_field_test.rb +0 -69
  70. data/test/test_helper.rb +0 -10
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -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
@@ -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
- [![Build Status](https://secure.travis-ci.org/jfarmer/randomized_field.png)](http://travis-ci.org/jfarmer/randomized_field)
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
- RandomizedField is a simple plugin for ActiveRecord that populates a field with a unique,
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
- * Order numbers, return authorization numbers, etc. for an e-commerce system
16
- * Unique referral IDs or invitation tokens for each user of your site
17
- * Tokens for password reset emails
18
- * Temporary tokens embedded in an email that automatically log users in
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
- RandomizedField currently generates a random, fixed-length string with an optional prefix.
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
- Like I said: simple.
16
+ 1. Add the `randomized_field` gem to your application's Gemfile:
27
17
 
28
- ## Rails Quickstart
29
- ```ruby
30
- gem install randomized_field
18
+ ```ruby
19
+ gem 'randomized_field'
20
+ ```
21
+ 2. Run `bundle` to install the gem
31
22
 
32
- rails new my_app
23
+ ```console
24
+ $ bundle
25
+ ```
33
26
 
34
- cd my_app
27
+ Or install it directly with the `gem` command:
35
28
 
36
- gem "randomized_field"
29
+ ```console
30
+ $ gem install randomized_field
31
+ ```
37
32
 
38
- rails generate scaffold user name:string random_id:string
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
- # edit db/migrate/*_create_users.rb
41
- add_index :users, :random_id, :unique => true
35
+ ```ruby
36
+ class Article < ApplicationRecord
37
+ include RandomizedField
42
38
 
43
- rake db:migrate
39
+ randomized_field :slug, length: 10
40
+ end
41
+ ```
44
42
 
45
- # edit app/models/user.rb
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
- User.create! name: "Joe Schmoe"
45
+ ## Development
51
46
 
52
- rails server
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
- Please report them on the [Github issue
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
- If you have a bug to report, please include the following information:
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
- Copyright (c) 2012 Jesse Farmer, released under the MIT license
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
- #!/usr/bin/env rake
2
- begin
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
- RDoc::Task.new(:rdoc) do |rdoc|
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
@@ -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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,4 +1,18 @@
1
- require 'randomized_field/version'
2
- require 'randomized_field/defaults'
3
- require 'randomized_field/base'
4
- require 'randomized_field/randomizer'
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