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:
parent
7e2567a8cf
commit
f3431e8442
|
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Bet < ApplicationRecord
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :match
|
||||||
|
belongs_to :team, optional: true
|
||||||
|
end
|
||||||
|
|
@ -5,7 +5,9 @@ class Match < ApplicationRecord
|
||||||
|
|
||||||
belongs_to :stage, optional: true
|
belongs_to :stage, optional: true
|
||||||
belongs_to :group, optional: true
|
belongs_to :group, optional: true
|
||||||
|
|
||||||
has_many :match_scores, dependent: :destroy
|
has_many :match_scores, dependent: :destroy
|
||||||
|
has_many :bets, dependent: :destroy
|
||||||
|
|
||||||
validates :match_scores, length: { maximum: 2 }
|
validates :match_scores, length: { maximum: 2 }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ class Team < ApplicationRecord
|
||||||
belongs_to :tournament, optional: true
|
belongs_to :tournament, optional: true
|
||||||
has_many :group_scores, dependent: :destroy
|
has_many :group_scores, dependent: :destroy
|
||||||
has_many :match_scores, dependent: :destroy
|
has_many :match_scores, dependent: :destroy
|
||||||
|
has_many :bets, dependent: :destroy
|
||||||
|
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,5 @@ class User < ApplicationRecord
|
||||||
validates :username, presence: true, uniqueness: { case_sensitive: false }
|
validates :username, presence: true, uniqueness: { case_sensitive: false }
|
||||||
|
|
||||||
has_many :tournaments, dependent: :destroy
|
has_many :tournaments, dependent: :destroy
|
||||||
|
has_many :bets, dependent: :destroy
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
@ -5,6 +5,7 @@ require 'rails_helper'
|
||||||
RSpec.describe Match, type: :model do
|
RSpec.describe Match, type: :model do
|
||||||
context 'association' do
|
context 'association' do
|
||||||
it { should have_many :match_scores }
|
it { should have_many :match_scores }
|
||||||
|
it { should have_many :bets }
|
||||||
it { should belong_to(:stage).optional }
|
it { should belong_to(:stage).optional }
|
||||||
it { should belong_to(:group).optional }
|
it { should belong_to(:group).optional }
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,5 +11,6 @@ RSpec.describe Team, type: :model do
|
||||||
it { should belong_to(:tournament).optional }
|
it { should belong_to(:tournament).optional }
|
||||||
it { should have_many :group_scores }
|
it { should have_many :group_scores }
|
||||||
it { should have_many :match_scores }
|
it { should have_many :match_scores }
|
||||||
|
it { should have_many :bets }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ require 'rails_helper'
|
||||||
RSpec.describe User, type: :model do
|
RSpec.describe User, type: :model do
|
||||||
describe 'association' do
|
describe 'association' do
|
||||||
it { should have_many :tournaments }
|
it { should have_many :tournaments }
|
||||||
|
it { should have_many :bets }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'validation' do
|
describe 'validation' do
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue