-
-
Save laurocaetano/1df56e039838e7616c00 to your computer and use it in GitHub Desktop.
Test case for https://github.com/rails/rails/issues/12570
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
unless File.exists?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', '4.0.4' | |
gem 'sqlite3' | |
GEMFILE | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :sites, force: true do |t| | |
t.string :repo | |
t.string :env | |
t.integer "build", default: 0, null: false | |
end | |
end | |
class Site < ActiveRecord::Base | |
BUILD = { manual: 0, cap: 1, recap: 2 } | |
def build | |
BUILD.key(read_attribute(:build)) | |
end | |
def build=(b) | |
write_attribute(:build, BUILD[b]) | |
end | |
end | |
class TestBuildSymbol < MiniTest::Unit::TestCase | |
def test_find_or_create_by_with_build | |
# It will create the 3 records, because there isn't any record on the | |
# database. | |
# It works, because it will do: Site.create(attributes) which have access | |
# to Site#build= because it is an instace of Site. | |
Site.find_or_create_by(repo: 'bob', env: 'staging', build: :recap) | |
Site.find_or_create_by(repo: 'joe', env: 'staging', build: :manual) | |
Site.find_or_create_by(repo: 'jim', env: 'staging', build: :manual) | |
assert_equal 3, Site.count | |
# It will not return the record, since the Site#build will not be used, | |
# because it is not an instance of Site here. | |
assert_nil Site.find_by(repo: 'bob', env: 'staging', build: :recap) | |
# It will work correctly | |
assert Site.find_by(repo: 'bob', env: 'staging', build: 2).present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment