-
-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix uniqueness matcher when scope is a *_type attr #592
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
# @private | ||
module Uniqueness | ||
end | ||
end | ||
end | ||
end | ||
|
||
require 'shoulda/matchers/active_model/uniqueness/model' | ||
require 'shoulda/matchers/active_model/uniqueness/namespace' | ||
require 'shoulda/matchers/active_model/uniqueness/test_model_creator' | ||
require 'shoulda/matchers/active_model/uniqueness/test_models' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
module Uniqueness | ||
# @private | ||
class Model | ||
def self.next_unique_copy_of(model_name, namespace) | ||
model = new(model_name, namespace) | ||
|
||
while model.already_exists? | ||
model = model.next | ||
end | ||
|
||
model | ||
end | ||
|
||
def initialize(name, namespace) | ||
@name = name | ||
@namespace = namespace | ||
end | ||
|
||
def already_exists? | ||
namespace.has?(name) | ||
end | ||
|
||
def next | ||
Model.new(name.next, namespace) | ||
end | ||
|
||
def symlink_to(parent) | ||
namespace.set(name, parent.dup) | ||
end | ||
|
||
def to_s | ||
[namespace, name].join('::') | ||
end | ||
|
||
protected | ||
|
||
attr_reader :name, :namespace | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
module Uniqueness | ||
# @private | ||
class Namespace | ||
def initialize(constant) | ||
@constant = constant | ||
end | ||
|
||
def has?(name) | ||
constant.const_defined?(name) | ||
end | ||
|
||
def set(name, value) | ||
constant.const_set(name, value) | ||
end | ||
|
||
def clear | ||
constant.constants.each do |child_constant| | ||
constant.__send__(:remove_const, child_constant) | ||
end | ||
end | ||
|
||
def to_s | ||
constant.to_s | ||
end | ||
|
||
protected | ||
|
||
attr_reader :constant | ||
end | ||
end | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
lib/shoulda/matchers/active_model/uniqueness/test_model_creator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'thread' | ||
|
||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
module Uniqueness | ||
# @private | ||
class TestModelCreator | ||
def self.create(model_name, namespace) | ||
Mutex.new.synchronize do | ||
new(model_name, namespace).create | ||
end | ||
end | ||
|
||
def initialize(model_name, namespace) | ||
@model_name = model_name | ||
@namespace = namespace | ||
end | ||
|
||
def create | ||
new_model.tap do |new_model| | ||
new_model.symlink_to(existing_model) | ||
end | ||
end | ||
|
||
protected | ||
|
||
attr_reader :model_name, :namespace | ||
|
||
private | ||
|
||
def model_name_without_namespace | ||
model_name.demodulize | ||
end | ||
|
||
def new_model | ||
@_new_model ||= Model.next_unique_copy_of( | ||
model_name_without_namespace, | ||
namespace | ||
) | ||
end | ||
|
||
def existing_model | ||
@_existing_model ||= model_name.constantize | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
lib/shoulda/matchers/active_model/uniqueness/test_models.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'thread' | ||
|
||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
module Uniqueness | ||
# @private | ||
module TestModels | ||
def self.create(model_name) | ||
TestModelCreator.create(model_name, root_namespace) | ||
end | ||
|
||
def self.remove_all | ||
root_namespace.clear | ||
end | ||
|
||
def self.root_namespace | ||
@_root_namespace ||= Namespace.new(self) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,10 +216,13 @@ def matches?(subject) | |
@original_subject = subject | ||
@subject = subject.class.new | ||
@expected_message ||= :taken | ||
|
||
set_scoped_attributes && | ||
validate_everything_except_duplicate_nils? && | ||
validate_after_scope_change? && | ||
allows_nil? | ||
ensure | ||
Uniqueness::TestModels.remove_all | ||
end | ||
|
||
private | ||
|
@@ -262,6 +265,7 @@ def create_record_in_database(options = {}) | |
instance.__send__("#{@attribute}=", value) | ||
ensure_secure_password_set(instance) | ||
instance.save(validate: false) | ||
@created_record = instance | ||
end | ||
end | ||
|
||
|
@@ -305,6 +309,12 @@ def create_record_without_nil | |
@existing_record = create_record_in_database | ||
end | ||
|
||
def model_class?(model_name) | ||
model_name.constantize.ancestors.include?(::ActiveRecord::Base) | ||
rescue NameError | ||
false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
end | ||
|
||
def validate_after_scope_change? | ||
if @options[:scopes].blank? | ||
true | ||
|
@@ -322,6 +332,8 @@ def validate_after_scope_change? | |
key == previous_value | ||
end | ||
available_values.keys.last | ||
elsif scope.to_s =~ /_type$/ && model_class?(previous_value) | ||
Uniqueness::TestModels.create(previous_value).to_s | ||
elsif previous_value.respond_to?(:next) | ||
previous_value.next | ||
elsif previous_value.respond_to?(:to_datetime) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Shoulda | ||
module Matchers | ||
# @private | ||
module Util | ||
def self.deconstantize(path) | ||
if defined?(ActiveSupport::Inflector) && | ||
ActiveSupport::Inflector.respond_to?(:deconstantize) | ||
ActiveSupport::Inflector.deconstantize(path) | ||
else | ||
path.to_s[0...(path.to_s.rindex('::') || 0)] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model_name.constantize < ::ActiveRecord::Base