Merge pull request #63 from ZeusWPI/active_model_for_stock_form

Active model for stock form
This commit is contained in:
benji 2015-09-01 23:39:48 +02:00
commit ea360dc028
15 changed files with 138 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,2 @@
module StockHelper
end

60
app/models/stock.rb Normal file
View file

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

View file

@ -37,7 +37,7 @@
<ul class="dropdown-menu" role="menu">
<li><%= link_to "List", products_path %></li>
<li><%= link_to "Add product" , new_product_path %></li>
<li><%= link_to "Add stock", stock_products_path %></li>
<li><%= link_to "Add stock", new_stock_path %></li>
</ul>
</li>
<li class="dropdown">

View file

@ -1,14 +0,0 @@
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_tag do %>
<% @products.each do |product| %>
<%= fields_for "products[]", product do |form| %>
<%= image_tag product.avatar %>
<%= form.number_field :stock_inc, value: 0, class: 'form-control' %>
<% end %>
<% end %>
<%= submit_tag "Insert stock", class: 'btn btn-primary' %>
<% end %>
</div>
</div>

View file

@ -3,6 +3,7 @@
<div class="col-md-8 col-md-offset-2">
<h1>Products</h1>
<%= render partial: 'flash' %>
<%= link_to "Add Stock", new_stock_path, class: "btn btn-default" %>
<table id="products-table" class="table table-striped">
<tr>

View file

@ -0,0 +1,7 @@
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for @stock do %>
<%= submit_tag "Insert stock", class: 'btn btn-primary' %>
<% end %>
</div>
</div>

View file

@ -0,0 +1,20 @@
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2>Add stock</h2>
<%= f_form_for @stock do |f| %>
<%= f.error_messages %>
<%= f.fields_for :stock_entries do |se_field| %>
<div class="row">
<div class="col-sm-3">
<%= image_tag se_field.object.product.avatar %>
</div>
<div class="col-sm-9">
<%= se_field.hidden_field :product_id %>
<%= se_field.number_field :count, skip_label: true %>
</div>
</div>
<% end %>
<%= f.submit "Insert stock", class: 'btn btn-primary' %>
<% end %>
</div>
</div>

View file

@ -25,12 +25,8 @@ Rails.application.routes.draw do
get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel'
end
resources :products do
collection do
get 'stock' => 'products#stock', as: 'stock'
post 'stock' => 'products#update_stock', as: 'update_stock'
end
end
resources :products
resources :stocks
get 'overview' => 'orders#overview', as: "orders"
end

View file

@ -0,0 +1,7 @@
require 'test_helper'
class StockControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require 'test_helper'
class StockEntryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end