How to get the full price for a gem

I use a money stone and I wanted to know how I can get prices such as 23.45, 10.00, 0.32 to show in one field like a dollar .cents? I want the field of my cents to become just the price (money or dollars and cents).

Here is my code:

My money gem model is composed:

class Price < ActiveRecord::Base
    attr_accessible :name, :date, :cents, :currency

    belongs_to :user

    validates :name,       :presence => true,
                           :length   => { :maximum => 50 }

    validates :currency,   :presence => true
    validates :cents,      :presence => true
    validates :date,       :presence => true

    default_scope :order => 'prices.created_at DESC'

    composed_of :price,
        :class_name => "Money",
        :mapping => [%w(cents cents), %w(currency currency_as_string)],
        :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
        :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
end

Shape (I cut parts for short):

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :date %><br />
    <%= f.date_select :date %>
  </div>
  <div class="field">
    <%= f.label :cents %><br />
    <%= f.text_field :cents %>
  </div>
  <><div class="field">
    <%= f.label :currency %><br />
    <%= f.select(:currency,major_currencies(Money::Currency::TABLE), 
    {:include_blank => 'Select a Currency'}) %>
  </div>

My migration or pricing table:

class CreatePrices < ActiveRecord::Migration
  def self.up
    create_table :prices do |t|
      t.integer :user_id
      t.string :name
      t.date :date
      t.integer :cents, :default => 0
      t.string :currency

It seems like I'm missing a column or something like that. Maybe it's even cents? Not sure, so I need your help.

Thanks for taking the time.

+3
source share
4 answers

Ok, I could figure it out, thanks everyone!

Here is my new code:

: : -

create_table :prices do |t|
  t.integer :user_id
  t.string :name
  t.date :date
  t.integer :price, :default => 0
  t.string :currency

:

  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>

new created_of:

composed_of :price,
    :class_name  => "Money",
    :mapping     => [%w(price cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter   => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't conver #{value.class} to Money") }

, _ , , - . , .

0

, , , , , -, :

https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/helpers/action_view_extension.rb

:

  • humanize_money

, @my_money_object.format .... bam, , 2.

:

@my_money_object = 20.to_money
@my_money_object.format

= > "$ 20.00"

. , " , , $45,23". , , , , , , . , , humanize_money, ( ), . , .

+4

,

DB :

 t.integer :cents, :default => 0

:

 t.decimal :cents, :default => 0
+1

. https://github.com/RubyMoney/money, :

money = Money.new(1000, "USD")
money.cents     #=> 1000
money.dollars   #=> 10.0

, -, - . :

def value
  cents / 100.0
end

( , ). , , 12.34, 1234 . , , 12.34, , ( /) before_save - . 100, .

EDIT: .

def with_zeros(price)
  array = price.to_s.split('.')
  array[1] = array[1].ljust(2,'0')
  array.join('.')
end
+1

All Articles