Skip to content
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

Bug fix: uniqueness validation fails with scoped attributes of data type time #1190

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -820,7 +820,7 @@ def next_scalar_value_for(scope, previous_value)
elsif previous_value.respond_to?(:next)
previous_value.next
elsif previous_value.respond_to?(:to_datetime)
previous_value.to_datetime.next
previous_value.to_datetime.in(60).next
elsif boolean_value?(previous_value)
!previous_value
else
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/util.rb
Expand Up @@ -92,7 +92,7 @@ def self.dummy_value_for(column_type, array: false)
when :datetime, :timestamp
DateTime.new(2100, 1, 1)
when :time
Time.new(2100, 1, 1)
Time.new(2000, 1, 1)
when :uuid
SecureRandom.uuid
when :boolean
Expand Down
Expand Up @@ -829,6 +829,11 @@
column_type: :datetime
end

context 'when one of the scoped attributes is a time column (using Time)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :time
end

context 'when one of the scoped attributes is a datetime column (using Time)' do
include_context 'it supports scoped attributes of a certain type',
column_type: :datetime,
Expand Down Expand Up @@ -1538,32 +1543,7 @@ def attributes_with_values_for(model)
end

def dummy_value_for(attribute_type, array: false)
if array
[ dummy_scalar_value_for(attribute_type) ]
else
dummy_scalar_value_for(attribute_type)
end
end

def dummy_scalar_value_for(attribute_type)
case attribute_type
when :string, :text
'dummy value'
when :integer
1
when :date
Date.today
when :datetime
Date.today.to_datetime
when :time
Time.now
when :uuid
SecureRandom.uuid
when :boolean
true
else
raise ArgumentError, "Unknown type '#{attribute_type}'"
end
Shoulda::Matchers::Util.dummy_value_for(attribute_type, array: array)
end

def next_version_of(value, value_type)
Expand Down