Remove hack for nested models

This commit is contained in:
benji 2015-10-28 22:07:48 +01:00
parent 895c523bb9
commit 839151405b
5 changed files with 6 additions and 17 deletions

View file

@ -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

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -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