Skip to content

Commit a1cf4e1

Browse files
authored
Sets up initial gem to patch shoulda-matchers
Since we're taking advantage of storing UUIDs in MySQL as `binary`, the shoulda-matchers has an issue when the UUID ends in an `f`. This has lead to some flakey tests. [We made an effort to fix this upstream](thoughtbot/shoulda-matchers#1159), which doesn't appear to be gaining traction. This extracts the fix into a gem so we don't have to support a hard fork for this. Pivotal Story: https://www.pivotaltracker.com/story/show/164603806 Merges #1 LGTM given by: @bsimpson
1 parent 772ce75 commit a1cf4e1

17 files changed

Lines changed: 401 additions & 1 deletion

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/**/database.yml
8+
/spec/reports/
9+
/tmp/
10+
Gemfile.lock
11+
12+
# rspec failure tracking
13+
.rspec_status

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

CODE_OF_CONDUCT.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at vstoll@doximity.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [https://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: https://contributor-covenant.org
74+
[version]: https://contributor-covenant.org/version/1/4/

Gemfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
source "https://rubygems.org"
2+
3+
# Specify your gem's dependencies in shoulda-matchers-uuid.gemspec
4+
gemspec
5+
6+
gem "activemodel", "> 5.0"
7+
gem "activerecord", "> 4"
8+
gem "byebug"
9+
gem "mysql2"
10+
gem "mysql-binuuid-rails", require: false
11+
gem "rake", "~> 12.0"
12+
gem "rspec", "~> 3.0"

LICENSE.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 Doximity, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# shoulda-matchers-uuid
2-
The shoulda-matchers gem does not currently support MYSQL uuid columns. This extends it to allow validating uuids in models.
2+
3+
This is a patch to shoulda-matchers that allows the uniqueness validation matchers to work with MySQL UUIDs. The current shoulda-matchers gem incorrectly increments UUID values when they are set as `binary` types (when using the [mysql-binuuid-rails gem](https://github.com/nedap/mysql-binuuid-rails)).
4+
5+
We have [an existing open PR](https://github.com/thoughtbot/shoulda-matchers/pull/1159) that is likely not going to make it through.
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'shoulda-matchers-uuid'
13+
```
14+
15+
And then execute:
16+
17+
$ bundle install
18+
19+
Or install it yourself as:
20+
21+
$ gem install shoulda-matchers-uuid
22+
23+
## Usage
24+
25+
That's it! Use shoulda-matchers as usual to validate uniqueness of UUIDs.
26+
27+
## Development
28+
29+
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.
30+
31+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32+
33+
## Contributing
34+
35+
Bug reports and pull requests are welcome on GitHub at https://github.com/codenamev/shoulda-matchers-uuid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/codenamev/shoulda-matchers-uuid/blob/master/CODE_OF_CONDUCT.md).
36+
37+
38+
## Code of Conduct
39+
40+
Everyone interacting in the Shoulda::Matchers::Uuid project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/codenamev/shoulda-matchers-uuid/blob/master/CODE_OF_CONDUCT.md).

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "shoulda/matchers/uuid"
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__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
cp ./spec/support/database.yml.example ./spec/support/database.yml

lib/shoulda/matchers/uuid.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require "active_support/concern"
4+
require "shoulda/matchers"
5+
6+
module ShouldaMatchersMysqlUuidFix
7+
extend ActiveSupport::Concern
8+
9+
included do
10+
def next_scalar_value_for(scope, previous_value)
11+
if uuid?(scope)
12+
SecureRandom.uuid
13+
elsif defined_as_enum?(scope)
14+
available_values = available_enum_values_for(scope, previous_value)
15+
available_values.keys.last
16+
elsif polymorphic_type_attribute?(scope, previous_value)
17+
Uniqueness::TestModels.create(previous_value).to_s
18+
elsif previous_value.respond_to?(:next)
19+
previous_value.next
20+
elsif previous_value.respond_to?(:to_datetime)
21+
previous_value.to_datetime.next
22+
elsif boolean_value?(previous_value)
23+
!previous_value
24+
else
25+
previous_value.to_s.next
26+
end
27+
end
28+
29+
def uuid?(scope)
30+
[
31+
column_for(scope),
32+
attribute_type_for(scope)
33+
].compact.map(&:type).include? :uuid
34+
end
35+
36+
def attribute_type_for(scope)
37+
return unless model.respond_to?(:attribute_types)
38+
39+
model.attribute_types[scope.to_s]
40+
end
41+
end
42+
end
43+
44+
Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher.public_send(:include, ShouldaMatchersMysqlUuidFix)

0 commit comments

Comments
 (0)