Skip to content

Commit

Permalink
No more throws a error when use validate_uniqueness_of with scope of
Browse files Browse the repository at this point in the history
boolean column
  • Loading branch information
maurogeorge committed Mar 31, 2015
1 parent 762cebd commit 7733001
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,14 @@ def validate_after_scope_change?
else
all_records = @subject.class.all
@options[:scopes].all? do |scope|
previous_value = all_records.map(&scope).compact.max
previous_value =
if is_a_boolean_scope?(all_records, scope)
value = all_records.map(&scope).last
@existing_record.destroy
value
else
all_records.map(&scope).compact.max
end

next_value =
if previous_value.blank?
Expand Down Expand Up @@ -447,6 +454,8 @@ def dummy_scalar_value_for(column)
DateTime.now
when :uuid
SecureRandom.uuid
when :boolean
true
else
'dummy value'
end
Expand Down Expand Up @@ -474,11 +483,21 @@ def next_scalar_value_for(scope, previous_value)
previous_value.next
elsif previous_value.respond_to?(:to_datetime)
previous_value.to_datetime.next
elsif is_a_boolean?(previous_value)
!previous_value
else
previous_value.to_s.next
end
end

def is_a_boolean_scope?(all_records, scope)
all_records.map(&scope).any?{ |s| is_a_boolean?(s) }
end

def is_a_boolean?(value)
value.in?([true, false])
end

def defined_as_enum?(scope)
@subject.class.respond_to?(:defined_enums) &&
@subject.defined_enums[scope.to_s]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@
column_type: :string
end

context 'when one of the scoped attributes is a boolean column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :boolean
end

context 'when one of the scoped attributes is an integer column' do
include_context 'it supports scoped attributes of a certain type',
column_type: :integer
Expand Down Expand Up @@ -752,6 +757,8 @@ def dummy_scalar_value_for(attribute_type)
Time.now
when :uuid
SecureRandom.uuid
when :boolean
true
else
raise ArgumentError, "Unknown type '#{attribute_type}'"
end
Expand All @@ -764,6 +771,8 @@ def next_version_of(value, value_type)
SecureRandom.uuid
elsif value.is_a?(Time)
value + 1
elsif value.in?([true, false])
!value
elsif value.respond_to?(:next)
value.next
end
Expand Down

0 comments on commit 7733001

Please sign in to comment.