From 0862b275fca46677fe73330de8245ca349705703 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 1 Sep 2015 23:13:20 +0200 Subject: [PATCH 1/2] Create actionmodel stock/stockentry and change form --- app/assets/javascripts/stock.js.coffee | 3 ++ app/assets/stylesheets/products.css.scss | 6 ++- app/assets/stylesheets/stock.css.scss | 3 ++ app/controllers/products_controller.rb | 13 ----- app/controllers/stocks_controller.rb | 20 ++++++++ app/helpers/stock_helper.rb | 2 + app/models/stock.rb | 60 +++++++++++++++++++++++ app/views/layouts/_header.html.erb | 2 +- app/views/products/stock.html.erb | 14 ------ app/views/products_list/listview.html.erb | 1 + app/views/stock/new.html.erb | 7 +++ app/views/stocks/new.html.erb | 20 ++++++++ config/routes.rb | 8 +-- test/controllers/stock_controller_test.rb | 7 +++ test/fixtures/stock_entries.yml | 11 +++++ test/models/stock_entry_test.rb | 7 +++ 16 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 app/assets/javascripts/stock.js.coffee create mode 100644 app/assets/stylesheets/stock.css.scss create mode 100644 app/controllers/stocks_controller.rb create mode 100644 app/helpers/stock_helper.rb create mode 100644 app/models/stock.rb delete mode 100644 app/views/products/stock.html.erb create mode 100644 app/views/stock/new.html.erb create mode 100644 app/views/stocks/new.html.erb create mode 100644 test/controllers/stock_controller_test.rb create mode 100644 test/fixtures/stock_entries.yml create mode 100644 test/models/stock_entry_test.rb diff --git a/app/assets/javascripts/stock.js.coffee b/app/assets/javascripts/stock.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/stock.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/products.css.scss b/app/assets/stylesheets/products.css.scss index 7834f4e..0feb590 100644 --- a/app/assets/stylesheets/products.css.scss +++ b/app/assets/stylesheets/products.css.scss @@ -22,6 +22,10 @@ margin-bottom: 20px; } +#products-table { + margin-top: 20px; +} + #products-table .form-group { margin-bottom: 0; } @@ -35,4 +39,4 @@ div.out-of-stock { border-color:red; -} \ No newline at end of file +} diff --git a/app/assets/stylesheets/stock.css.scss b/app/assets/stylesheets/stock.css.scss new file mode 100644 index 0000000..f5d1f77 --- /dev/null +++ b/app/assets/stylesheets/stock.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the stock controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 4117eb2..3975675 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -35,19 +35,6 @@ class ProductsController < ApplicationController respond_with @product end - def stock - @products = Product.all - end - - def update_stock - @products = Product.all - @products.each do |product| - stock_inc = params[:products][product.id.to_s][:stock_inc].to_i - product.increment!(:stock, stock_inc) if stock_inc > 0 - end - redirect_to products_path - end - private def product_params diff --git a/app/controllers/stocks_controller.rb b/app/controllers/stocks_controller.rb new file mode 100644 index 0000000..9fc8b1c --- /dev/null +++ b/app/controllers/stocks_controller.rb @@ -0,0 +1,20 @@ +class StocksController < ApplicationController + load_and_authorize_resource + + def new + @stock = Stock.new + Product.all.each do |p| + @stock.stock_entries << Stock::StockEntry.new(product: p) + end + end + + def create + @stock = Stock.new(params[:stock]) + if @stock.update + flash[:success] = "Stock updated!" + redirect_to products_path + else + render 'new' + end + end +end diff --git a/app/helpers/stock_helper.rb b/app/helpers/stock_helper.rb new file mode 100644 index 0000000..db18c80 --- /dev/null +++ b/app/helpers/stock_helper.rb @@ -0,0 +1,2 @@ +module StockHelper +end diff --git a/app/models/stock.rb b/app/models/stock.rb new file mode 100644 index 0000000..25f8ad5 --- /dev/null +++ b/app/models/stock.rb @@ -0,0 +1,60 @@ +class Stock + include ActiveModel::Model + include ActiveModel::Validations + + def stock_entries + @stockentries ||= [] + end + + def stock_entries_attributes=(attributes) + attributes.each do |i, se_attr| + stock_entries.push(StockEntry.new(se_attr)) + end + end + + validate :validate_stock_entries + def validate_stock_entries + stock_entries.each do |se| + unless se.valid? + se.errors.each do |_, e| + errors[se.product.name] = "count " + e + end + end + end + end + + def update + return false unless valid? + + stock_entries.each do |se| + se.product.increment!(:stock, se.count.to_i) if se.count.to_i > 0 + end + end + + class StockEntry + include ActiveModel::Model + include ActiveModel::Validations + + validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :product, presence: true + + def initialize(attributes={}) + super + @count ||= 0 + end + + attr_accessor :product, :count + + def self.products + @products ||= Product.all.to_a + end + + def product_id + @product.id + end + + def product_id=(id) + @product = StockEntry.products.select { |p| p.id == id.to_i }.first + end + end +end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index bae5490..abf1143 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -37,7 +37,7 @@