Sha256: 38a102c53616583170ed9140d9dd65f43b39f677c7489b7171721d413de18666

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

class Player
  include Mongoid::Document
  include Mongoid::Timestamps
  include ActiveModel::ForbiddenAttributesProtection

  field :deleted_at, type: DateTime
  belongs_to :team, inverse_of: :players
  field :name, type: String
  field :position, type: String
  field :number, type: Integer
  field :retired, type: Boolean, default: false
  field :injured, type: Boolean, default: false
  field :born_on, type: Date
  field :notes, type: String
  field :suspended, type: Boolean, default: false

  validates_presence_of(:name)
  validates_numericality_of(:number, only_integer: true)
  validates_uniqueness_of(:number, scope: :team_id, message: 'There is already a player with that number on this team')

  validates_each :name do |record, _attr, value|
    record.errors.add(:base, 'Player is cheating') if value.to_s =~ /on steroids/
  end

  has_one :draft, dependent: :destroy
  has_many :comments, as: :commentable

  before_destroy :destroy_hook

  def destroy_hook; end

  def draft_id
    draft.try :id
  end

  def draft_id=(id)
    self.draft = Draft.where(_id: id).first
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_admin-0.6.4 spec/dummy_app/app/mongoid/player.rb
rails_admin-0.6.3 spec/dummy_app/app/mongoid/player.rb