Add bet model

* has_many relations in all belonging models
* specs for relations in existing models
* association specs for bet model
This commit is contained in:
Thor77 2019-06-09 20:43:28 +02:00
parent 7e2567a8cf
commit f3431e8442
No known key found for this signature in database
GPG Key ID: 5051E71B46AA669A
9 changed files with 38 additions and 0 deletions

7
app/models/bet.rb Normal file
View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Bet < ApplicationRecord
belongs_to :user
belongs_to :match
belongs_to :team, optional: true
end

View File

@ -5,7 +5,9 @@ class Match < ApplicationRecord
belongs_to :stage, optional: true
belongs_to :group, optional: true
has_many :match_scores, dependent: :destroy
has_many :bets, dependent: :destroy
validates :match_scores, length: { maximum: 2 }

View File

@ -4,6 +4,7 @@ class Team < ApplicationRecord
belongs_to :tournament, optional: true
has_many :group_scores, dependent: :destroy
has_many :match_scores, dependent: :destroy
has_many :bets, dependent: :destroy
validates :name, presence: true

View File

@ -11,4 +11,5 @@ class User < ApplicationRecord
validates :username, presence: true, uniqueness: { case_sensitive: false }
has_many :tournaments, dependent: :destroy
has_many :bets, dependent: :destroy
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
class CreateBets < ActiveRecord::Migration[5.2]
def change
create_table :bets do |t|
t.references :user, index: true, null: false, foreign_key: { on_delete: :cascade }
t.references :match, index: true, null: false, foreign_key: { on_delete: :cascade }
t.references :team, index: true, null: true, foreign_key: { on_delete: :cascade }
t.timestamps
end
end
end

11
spec/models/bet_spec.rb Normal file
View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Bet, type: :model do
describe 'association' do
it { should belong_to :user }
it { should belong_to :match }
it { should belong_to(:team).optional }
end
end

View File

@ -5,6 +5,7 @@ require 'rails_helper'
RSpec.describe Match, type: :model do
context 'association' do
it { should have_many :match_scores }
it { should have_many :bets }
it { should belong_to(:stage).optional }
it { should belong_to(:group).optional }
end

View File

@ -11,5 +11,6 @@ RSpec.describe Team, type: :model do
it { should belong_to(:tournament).optional }
it { should have_many :group_scores }
it { should have_many :match_scores }
it { should have_many :bets }
end
end

View File

@ -5,6 +5,7 @@ require 'rails_helper'
RSpec.describe User, type: :model do
describe 'association' do
it { should have_many :tournaments }
it { should have_many :bets }
end
describe 'validation' do