diff --git a/app/models/order.rb b/app/models/order.rb index 11d129f..72d9416 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -27,7 +27,7 @@ class Order < ActiveRecord::Base accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 } def price - self.order_products.map{ |op| op.count * op.product.price }.sum + self.order_products.map{ |op| op.count * op.product.price_cents }.sum end def to_sentence diff --git a/app/models/product.rb b/app/models/product.rb index 9dd33d6..ae090c0 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -4,7 +4,7 @@ # # id :integer not null, primary key # name :string(255) -# price :integer +# price_cents :integer # created_at :datetime # updated_at :datetime # avatar_file_name :string(255) @@ -22,17 +22,17 @@ class Product < ActiveRecord::Base enum category: %w(food beverages other) validates :name, presence: true - validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] } def price - (read_attribute(:price) || 0) / 100.0 + (price_cents || 0) / 100.0 end def price=(value) if value.is_a? String then value.sub!(',', '.') end - write_attribute(:price, (value.to_f * 100).to_int) + self.price_cents = (value.to_f * 100).to_int end end diff --git a/app/views/order_products/_order_product.html.erb b/app/views/order_products/_order_product.html.erb index 8fe1135..fa3da1f 100644 --- a/app/views/order_products/_order_product.html.erb +++ b/app/views/order_products/_order_product.html.erb @@ -11,7 +11,7 @@ <%= render 'btn_inc' %> <%= f.fields_for :product do |product| %> - <%= product.hidden_field :price, class: :price %> + <%= product.hidden_field :price_cents, class: :price %> <%= product.hidden_field :stock, class: :stock %> <% end %> diff --git a/db/migrate/20150204155216_change_price_to_price_cents.rb b/db/migrate/20150204155216_change_price_to_price_cents.rb new file mode 100644 index 0000000..b03312e --- /dev/null +++ b/db/migrate/20150204155216_change_price_to_price_cents.rb @@ -0,0 +1,5 @@ +class ChangePriceToPriceCents < ActiveRecord::Migration + def change + rename_column :products, :price, :price_cents + end +end diff --git a/db/schema.rb b/db/schema.rb index 2cd70b9..666c04c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150113155744) do +ActiveRecord::Schema.define(version: 20150204155216) do create_table "order_products", force: true do |t| t.integer "order_id" @@ -31,7 +31,7 @@ ActiveRecord::Schema.define(version: 20150113155744) do create_table "products", force: true do |t| t.string "name" - t.integer "price" + t.integer "price_cents" t.datetime "created_at" t.datetime "updated_at" t.string "avatar_file_name" diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 0947a20..5e907c6 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -4,7 +4,7 @@ # # id :integer not null, primary key # name :string(255) -# price :integer +# price_cents :integer # created_at :datetime # updated_at :datetime # avatar_file_name :string(255) diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 0e4c013..05f5eaa 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -4,7 +4,7 @@ # # id :integer not null, primary key # name :string(255) -# price :integer +# price_cents :integer # created_at :datetime # updated_at :datetime # avatar_file_name :string(255)