Created
March 17, 2014 20:06
-
-
Save arthurnn/9607176 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
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
puts "ActiveRecord: #{ActiveRecord::VERSION::STRING}" | |
case ARGV[0] | |
when 'pg' | |
require 'pg' | |
ActiveRecord::Base.establish_connection(adapter: "postgresql", host: "localhost", user: "postgres", database: "test") | |
when 'mysql' | |
require 'mysql2' | |
ActiveRecord::Base.establish_connection(adapter: "mysql2", host: "localhost", database: "test") | |
else | |
require 'sqlite3' | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
end | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
class Round < ActiveRecord::Base | |
connection.create_table table_name, force: true do |t| | |
end | |
has_many :cards_rounds | |
has_many :cards, through: :cards_rounds | |
accepts_nested_attributes_for :cards_rounds | |
validate :round_validations | |
private | |
def round_validations | |
if cards.empty? ### THIS IS CHANGED! | |
# unless cards.map(&:id).uniq.size == 3 | |
errors.add(:round, "Must have 3 unique cards") | |
end | |
unless cards.map(&:quality).uniq.size == 1 | |
errors.add(:round, "Cards must be of the same rarity") | |
end | |
end | |
end | |
class CardsRound < ActiveRecord::Base | |
connection.create_table table_name, force: true do |t| | |
t.column :card_id, :integer | |
t.column :round_id, :integer | |
end | |
belongs_to :card | |
belongs_to :round | |
end | |
class Card < ActiveRecord::Base | |
connection.create_table table_name, force: true do |t| | |
t.string :value | |
t.string :quality | |
end | |
has_many :cards_rounds | |
has_many :rounds, through: :cards_rounds | |
end | |
class BugTest < MiniTest::Unit::TestCase | |
def test_cards | |
cards = 3.times.map { |i| Card.new(value: "foo #{i}", quality: "good") } | |
round = Round.new(cards: cards) | |
round.save! | |
assert_equal 3, Round.last.cards.count | |
assert_equal 3, Round.last.cards_rounds.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment