Sha256: 8817558933ec4c98297ce5569bc846881ab5f518e6912a0a47fbc8afb3c561a8

Contents?: true

Size: 774 Bytes

Versions: 4

Compression:

Stored size: 774 Bytes

Contents

class Item < ActiveRecord::Base
  belongs_to :category

  # validations
  
  def before_validation
    self.title = "" if self.title.nil?
    self.amount = 0 if self.amount.blank?
  end

  validates_presence_of :date

  validates_presence_of :amount, :position
  validates_numericality_of :amount, :position

  def self.find_conflict(item)
    item.type.constantize.find_conflict(item)
  end

  def validate
    # validate category
    unless self.category && self.category.kind == self.type
      errors.add("category", "the category is not for #{self.type}")
    end

    # validate index of day
    if same = self.class.find_conflict(self)
      errors.add("position", "position #{position} conflicts with #{same.title} (#{same.amount}, #{same.date})")
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
yhara-moneyrail-0.0.2 app/models/item.rb
moneyrail-0.1.1 app/models/item.rb
moneyrail-0.1.0 app/models/item.rb
moneyrail-0.0.2 app/models/item.rb