mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 23:22:53 +00:00
Moving to github
This commit is contained in:
34
app/models/item.rb
Normal file
34
app/models/item.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class Item < ApplicationRecord
|
||||
# Associations
|
||||
belongs_to :user
|
||||
belongs_to :platform, optional: true
|
||||
|
||||
# Enums
|
||||
enum :item_type, {
|
||||
console: "console",
|
||||
controller: "controller",
|
||||
accessory: "accessory",
|
||||
other: "other"
|
||||
}, prefix: true
|
||||
|
||||
# Validations
|
||||
validates :name, presence: true
|
||||
validates :item_type, presence: true
|
||||
validates :date_added, presence: true
|
||||
|
||||
# Callbacks
|
||||
before_validation :set_date_added, on: :create
|
||||
|
||||
# Scopes
|
||||
scope :consoles, -> { where(item_type: "console") }
|
||||
scope :controllers, -> { where(item_type: "controller") }
|
||||
scope :accessories, -> { where(item_type: "accessory") }
|
||||
scope :by_platform, ->(platform_id) { where(platform_id: platform_id) }
|
||||
scope :recent, -> { order(date_added: :desc) }
|
||||
|
||||
private
|
||||
|
||||
def set_date_added
|
||||
self.date_added ||= Date.current
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user