Class: InvoiceBar::Item

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/invoice_bar/item.rb

Instance Method Summary collapse

Instance Method Details

#copyObject

Copies the item and returns a new instance.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/invoice_bar/item.rb', line 20

def copy
  item = Item.new(
    name: name,
    number: number,
    price: price,
    unit: unit
  )

  item.update_amount

  item
end

#human_amountObject

Returns total amount in a human-readable way.



71
72
73
# File 'app/models/invoice_bar/item.rb', line 71

def human_amount
  FormattedMoney.amount(read_attribute(:amount))
end

#human_priceObject

Returns price in a human-readable way.



66
67
68
# File 'app/models/invoice_bar/item.rb', line 66

def human_price
  FormattedMoney.amount(read_attribute(:price))
end

#priceObject

Price in cents



61
62
63
# File 'app/models/invoice_bar/item.rb', line 61

def price
  read_attribute(:price)
end

#price=(price) ⇒ Object

Writes price using FormattedMoney for converting the user input.



50
51
52
53
54
55
56
57
58
# File 'app/models/invoice_bar/item.rb', line 50

def price=(price)
  begin
    cents = FormattedMoney.cents(price.gsub(' ', ''))
  rescue
    cents = price
  end

  write_attribute(:price, cents)
end

#totalObject

Calculates the total by multiplying price by number (of units).



38
39
40
41
42
43
44
45
46
47
# File 'app/models/invoice_bar/item.rb', line 38

def total()
  if price.blank? and number.blank?
    return 0
  end

  total = Integer(price)
  total = Integer(price) * Integer(number) unless number.nil?

  total
end

#update_amountObject



33
34
35
# File 'app/models/invoice_bar/item.rb', line 33

def update_amount
  self.amount = total
end