fix price bug for products
This commit is contained in:
parent
8e386f413f
commit
4b05623a96
|
@ -27,7 +27,7 @@ class Order < ActiveRecord::Base
|
||||||
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
|
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
|
||||||
|
|
||||||
def price
|
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
|
end
|
||||||
|
|
||||||
def to_sentence
|
def to_sentence
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# name :string(255)
|
# name :string(255)
|
||||||
# price :integer
|
# price_cents :integer
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# avatar_file_name :string(255)
|
# avatar_file_name :string(255)
|
||||||
|
@ -22,17 +22,17 @@ class Product < ActiveRecord::Base
|
||||||
enum category: %w(food beverages other)
|
enum category: %w(food beverages other)
|
||||||
|
|
||||||
validates :name, presence: true
|
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 :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"] }
|
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||||
|
|
||||||
def price
|
def price
|
||||||
(read_attribute(:price) || 0) / 100.0
|
(price_cents || 0) / 100.0
|
||||||
end
|
end
|
||||||
|
|
||||||
def price=(value)
|
def price=(value)
|
||||||
if value.is_a? String then value.sub!(',', '.') end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
<%= render 'btn_inc' %>
|
<%= render 'btn_inc' %>
|
||||||
|
|
||||||
<%= f.fields_for :product do |product| %>
|
<%= 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 %>
|
<%= product.hidden_field :stock, class: :stock %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
5
db/migrate/20150204155216_change_price_to_price_cents.rb
Normal file
5
db/migrate/20150204155216_change_price_to_price_cents.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class ChangePriceToPriceCents < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
rename_column :products, :price, :price_cents
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "order_products", force: true do |t|
|
||||||
t.integer "order_id"
|
t.integer "order_id"
|
||||||
|
@ -31,7 +31,7 @@ ActiveRecord::Schema.define(version: 20150113155744) do
|
||||||
|
|
||||||
create_table "products", force: true do |t|
|
create_table "products", force: true do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.integer "price"
|
t.integer "price_cents"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
t.string "avatar_file_name"
|
t.string "avatar_file_name"
|
||||||
|
|
2
test/fixtures/products.yml
vendored
2
test/fixtures/products.yml
vendored
|
@ -4,7 +4,7 @@
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# name :string(255)
|
# name :string(255)
|
||||||
# price :integer
|
# price_cents :integer
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# avatar_file_name :string(255)
|
# avatar_file_name :string(255)
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# name :string(255)
|
# name :string(255)
|
||||||
# price :integer
|
# price_cents :integer
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
# avatar_file_name :string(255)
|
# avatar_file_name :string(255)
|
||||||
|
|
Loading…
Reference in a new issue