Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support datetime columns in `validate_uniqueness_of.scoped_to`

  • Loading branch information...
commit cf848e6d78e716ed1e4b9b1e4e0e959bd790ea4a 1 parent 5da0671
@aclevy aclevy authored mxie committed
View
1  NEWS.md
@@ -1,5 +1,6 @@
# HEAD
+* Support datetime columns in `validate_uniqueness_of.scoped_to`
* Add `allow_nil` option to the `validate_uniqueness_of` matcher
# v 2.0.0
View
15 lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb
@@ -169,11 +169,14 @@ def validate_after_scope_change?
# Assume the scope is a foreign key if the field is nil
previous_value ||= correct_type_for_column(@subject.class.columns_hash[scope.to_s])
- next_value = if previous_value.respond_to?(:next)
- previous_value.next
- else
- previous_value.to_s.next
- end
+ next_value =
+ if previous_value.respond_to?(:next)
+ previous_value.next
+ elsif previous_value.respond_to?(:to_datetime)
+ previous_value.to_datetime.next
+ else
+ previous_value.to_s.next
+ end
@subject.send("#{scope}=", next_value)
@@ -194,6 +197,8 @@ def validate_after_scope_change?
def correct_type_for_column(column)
if column.type == :string
'0'
+ elsif column.type == :datetime
+ DateTime.now
else
0
end
View
53 spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb
@@ -112,21 +112,58 @@ def validating_uniqueness_with_existing_record(options = {})
should_not matcher.scoped_to(:fake)
end
- def create_existing_record
- @existing ||= Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2, :other => 3)
+ context 'when the scoped attribute is a date' do
+ it "accepts" do
+ validating_scoped_uniqueness([:scope1], :date, :scope1 => Date.today).
+ should matcher.scoped_to(:scope1)
+ end
+
+ context 'when too narrow of a scope is specified' do
+ it 'rejects' do
+ validating_scoped_uniqueness([:scope1, :scope2], :date, :scope1 => Date.today, :scope2 => Date.today).
+ should_not matcher.scoped_to(:scope1, :scope2, :other)
+ end
+ end
+ end
+
+ context 'when the scoped attribute is a datetime' do
+ it 'accepts' do
+ validating_scoped_uniqueness([:scope1], :datetime, :scope1 => DateTime.now).
+ should matcher.scoped_to(:scope1)
+ end
+
+ context 'with a nil value' do
+ it 'accepts' do
+ validating_scoped_uniqueness([:scope1], :datetime, :scope1 => nil).
+ should matcher.scoped_to(:scope1)
+ end
+ end
+
+ context 'when too narrow of a scope is specified' do
+ it 'rejects' do
+ validating_scoped_uniqueness([:scope1, :scope2], :datetime, :scope1 => DateTime.now, :scope2 => DateTime.now).
+ should_not matcher.scoped_to(:scope1, :scope2, :other)
+ end
+ end
+ end
+
+ def create_existing_record(attributes = {})
+ default_attributes = {:attr => 'value', :scope1 => 1, :scope2 => 2, :other => 3}
+ @existing ||= Example.create!(default_attributes.merge(attributes))
end
- def define_scoped_model(scope)
- define_model(:example, :attr => :string, :scope1 => :integer,
- :scope2 => :integer, :other => :integer) do
+ def define_scoped_model(scope, scope_attr_type = :integer)
+ define_model(:example, :attr => :string, :scope1 => scope_attr_type,
+ :scope2 => scope_attr_type, :other => :integer) do
attr_accessible :attr, :scope1, :scope2, :other
validates_uniqueness_of :attr, :scope => scope
end
end
- def validating_scoped_uniqueness(scope)
- model = define_scoped_model(scope).new
- create_existing_record
+ def validating_scoped_uniqueness(*args)
+ attributes = args.extract_options!
+ model = define_scoped_model(*args).new
+ create_existing_record(attributes)
model
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.