Remove hack for nested models
This commit is contained in:
parent
895c523bb9
commit
839151405b
5 changed files with 6 additions and 17 deletions
|
@ -31,6 +31,6 @@ class OrdersController < ApplicationController
|
|||
private
|
||||
|
||||
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
|
||||
|
|
|
@ -20,17 +20,6 @@ class OrderItem < ActiveRecord::Base
|
|||
before_destroy :put_back_in_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
|
||||
|
||||
def remove_from_stock!
|
||||
|
|
|
@ -10,5 +10,6 @@
|
|||
= content_tag :span, product.name
|
||||
= 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.fields_for :product do |product|
|
||||
/ This is needed for haml
|
||||
= f.input :product_id
|
||||
-# = 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 } }
|
||||
%td
|
||||
= ff.number_field :count
|
||||
= ff.fields_for :product do |fff|
|
||||
/ Needed for haml
|
||||
= ff.hidden_field :product_id
|
||||
%td
|
||||
x
|
||||
%td
|
||||
|
|
|
@ -44,7 +44,7 @@ describe OrderItem do
|
|||
it 'should be less or equal to product stock' do
|
||||
@order_item.count = @order_item.product.stock + 1
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue