Remove hack for nested models
This commit is contained in:
parent
895c523bb9
commit
839151405b
|
@ -31,6 +31,6 @@ class OrdersController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def order_params
|
def order_params
|
||||||
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
params.require(:order).permit(order_items_attributes: [:count, :price, :product_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,17 +20,6 @@ class OrderItem < ActiveRecord::Base
|
||||||
before_destroy :put_back_in_stock!
|
before_destroy :put_back_in_stock!
|
||||||
after_create :remove_from_stock!
|
after_create :remove_from_stock!
|
||||||
|
|
||||||
accepts_nested_attributes_for :product
|
|
||||||
|
|
||||||
def product_attributes=(attributes)
|
|
||||||
self.product = OrderItem.products.select{ |p| p.id == attributes[:id].to_i }.first
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.products
|
|
||||||
@products || Product.all
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def remove_from_stock!
|
def remove_from_stock!
|
||||||
|
|
|
@ -10,5 +10,6 @@
|
||||||
= content_tag :span, product.name
|
= content_tag :span, product.name
|
||||||
= content_tag :small, euro(product.price)
|
= content_tag :small, euro(product.price)
|
||||||
= f.counter :count, min: 0, max: product.stock, skip_label: true, wrapper_class: "input-group", class: "row_counter", data: { price: f.object.product.price_cents }
|
= f.counter :count, min: 0, max: product.stock, skip_label: true, wrapper_class: "input-group", class: "row_counter", data: { price: f.object.product.price_cents }
|
||||||
= f.fields_for :product do |product|
|
= f.input :product_id
|
||||||
/ This is needed for haml
|
-# = f.fields_for :product do |product|
|
||||||
|
-# / This is needed for haml
|
||||||
|
|
|
@ -19,8 +19,7 @@
|
||||||
%tr.order_item_wrapper{ data: { product: ff.object.product.id, price: ff.object.product.price_cents } }
|
%tr.order_item_wrapper{ data: { product: ff.object.product.id, price: ff.object.product.price_cents } }
|
||||||
%td
|
%td
|
||||||
= ff.number_field :count
|
= ff.number_field :count
|
||||||
= ff.fields_for :product do |fff|
|
= ff.hidden_field :product_id
|
||||||
/ Needed for haml
|
|
||||||
%td
|
%td
|
||||||
x
|
x
|
||||||
%td
|
%td
|
||||||
|
|
|
@ -44,7 +44,7 @@ describe OrderItem do
|
||||||
it 'should be less or equal to product stock' do
|
it 'should be less or equal to product stock' do
|
||||||
@order_item.count = @order_item.product.stock + 1
|
@order_item.count = @order_item.product.stock + 1
|
||||||
expect(@order_item).to_not be_valid
|
expect(@order_item).to_not be_valid
|
||||||
@order_item.count = @order_item.product.stock
|
@order_item.count = [@order_item.product.stock, 100].min
|
||||||
expect(@order_item).to be_valid
|
expect(@order_item).to be_valid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue