Skip to content

Commit

Permalink
Fixes thoughtbot#402; handles UUID separately instead of calling .next
Browse files Browse the repository at this point in the history
  • Loading branch information
damncabbage committed May 16, 2014
1 parent 1511ed9 commit d577305
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def create_record_in_database(options = {})

@subject.class.new.tap do |instance|
instance.__send__("#{@attribute}=", value)

other_non_nullable_columns.each do |non_nullable_column|
instance.__send__("#{non_nullable_column.name}=", correct_type_for_column(non_nullable_column))
instance.__send__("#{non_nullable_column.name}=", default_value_for_column(non_nullable_column))
end

if has_secure_password?
instance.password = 'password'
instance.password_confirmation = 'password'
Expand Down Expand Up @@ -167,13 +167,17 @@ def validate_after_scope_change?
else
all_records = @subject.class.all
@options[:scopes].all? do |scope|
previous_value = all_records.map(&scope).max
column_type = @subject.class.columns_hash[scope.to_s]

# 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])
previous_value = all_records.map(&scope).max
previous_value ||= default_value_for_column(column_type)


# Assume the scope is a foreign key if the field is nil
next_value =
if @subject.class.respond_to?(:defined_enums) && @subject.defined_enums[scope.to_s]
if column_type.respond_to?(:type) && column_type.type == :uuid
default_value_for_column(:uuid)
elsif @subject.class.respond_to?(:defined_enums) && @subject.defined_enums[scope.to_s]
available_values = @subject.defined_enums[scope.to_s].reject do |key, _|
key == previous_value
end
Expand Down Expand Up @@ -202,7 +206,7 @@ def validate_after_scope_change?
end
end

def correct_type_for_column(column)
def default_value_for_column(column)
if column.type == :string || column.type == :binary
'0'
elsif column.type == :datetime
Expand Down

0 comments on commit d577305

Please sign in to comment.