Created
January 24, 2016 18:25
-
-
Save nathansamson/d06bd374a6e8ede168ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'arel', github: 'rails/arel' | |
gem 'rack', github: 'rack/rack' | |
gem 'sprockets', github: 'rails/sprockets' | |
gem 'sprockets-rails', github: 'rails/sprockets-rails' | |
gem 'sass-rails', github: 'rails/sass-rails' | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :properties, force: true do |t| | |
t.decimal :number # Replace this with t.integer and it works | |
end | |
end | |
class Property < ActiveRecord::Base | |
attr_accessor :number_should_be_validated_as_integer | |
validates :number, numericality: { | |
# Using a proc here is not needed to expose the problem, but is used as an idea why I can not | |
# use integer as storage. | |
only_integer: ->(item) { item.number_should_be_validated_as_integer } | |
}#, if: ->() { number_changed? } # Enabling this line works around the problem but feels like a hack | |
end | |
class BugTest < Minitest::Test | |
def test_numericality_validation | |
property = Property.new | |
property.number_should_be_validated_as_integer = true | |
property.number = '150' | |
property.save! # Works like a charm | |
property.reload | |
# Nothing changed, same validations run, on the same input, but suddenly this starts to fail. | |
property.number_should_be_validated_as_integer = true | |
assert property.save, "Should save. errors: #{property.errors.messages}" # Does not work | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment