Class: SimpleItem

Inherits:
Item
  • Object
show all
Defined in:
app/models/simple_item.rb

Direct Known Subclasses

Expense, Income

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Item

#before_validation

Class Method Details

.find_conflict(item) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'app/models/simple_item.rb', line 10

def self.find_conflict(item)
  Item.all(:conditions => {
    :type => item.type,
    :date => item.date, 
    :account_id => item.,
    :category_id => item.category_id,
    :position => item.position,
  }).reject{|x| x.id == item.id}.first
end

Instance Method Details

#find_positionObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/simple_item.rb', line 37

def find_position
  klass = self.class
  items = klass.all(conditions: {
    date: self.date,
    account_id: self.,
    category_id: self.category_id
  })

  n = 0
  loop do
    if items.none?{|item| item.position == n}
      $stderr.puts "[find_position] #{n}"
      return n
    end
    n += 1
  end
end

#validateObject



20
21
22
23
24
25
26
27
# File 'app/models/simple_item.rb', line 20

def validate
  super

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