Class: InvoiceBar::Item
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- InvoiceBar::Item
- Defined in:
- app/models/invoice_bar/item.rb
Instance Method Summary collapse
-
#copy ⇒ Object
Copies the item and returns a new instance.
-
#human_amount ⇒ Object
Returns total amount in a human-readable way.
-
#human_price ⇒ Object
Returns price in a human-readable way.
-
#price ⇒ Object
Price in cents.
-
#price=(price) ⇒ Object
Writes price using FormattedMoney for converting the user input.
-
#total ⇒ Object
Calculates the total by multiplying price by number (of units).
- #update_amount ⇒ Object
Instance Method Details
#copy ⇒ Object
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_amount ⇒ Object
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_price ⇒ Object
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 |
#price ⇒ Object
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 |
#total ⇒ Object
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_amount ⇒ Object
33 34 35 |
# File 'app/models/invoice_bar/item.rb', line 33 def update_amount self.amount = total end |