Skip to content

Commit

Permalink
Fix bug: scoped attributes of data type time
Browse files Browse the repository at this point in the history
See issue #1114: validate_uniqueness_of with scoped_to fails when the scope is of data type time

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
  • Loading branch information
mikeduynguyen and mcmire committed Jul 12, 2020
1 parent 47ca467 commit 778b5f4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
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

0 comments on commit 778b5f4

Please sign in to comment.