Create actionmodel stock/stockentry and change form
This commit is contained in:
parent
b7c292275b
commit
0862b275fc
16 changed files with 149 additions and 35 deletions
3
app/assets/javascripts/stock.js.coffee
Normal file
3
app/assets/javascripts/stock.js.coffee
Normal 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/
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
3
app/assets/stylesheets/stock.css.scss
Normal file
3
app/assets/stylesheets/stock.css.scss
Normal 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/
|
|
@ -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
|
||||
|
|
20
app/controllers/stocks_controller.rb
Normal file
20
app/controllers/stocks_controller.rb
Normal 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
|
2
app/helpers/stock_helper.rb
Normal file
2
app/helpers/stock_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module StockHelper
|
||||
end
|
60
app/models/stock.rb
Normal file
60
app/models/stock.rb
Normal 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
|
|
@ -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">
|
||||
|
|
|
@ -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>
|
|
@ -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>
|
||||
|
|
7
app/views/stock/new.html.erb
Normal file
7
app/views/stock/new.html.erb
Normal 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>
|
20
app/views/stocks/new.html.erb
Normal file
20
app/views/stocks/new.html.erb
Normal 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>
|
|
@ -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
|
||||
|
|
7
test/controllers/stock_controller_test.rb
Normal file
7
test/controllers/stock_controller_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class StockControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
11
test/fixtures/stock_entries.yml
vendored
Normal file
11
test/fixtures/stock_entries.yml
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
7
test/models/stock_entry_test.rb
Normal file
7
test/models/stock_entry_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class StockEntryTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in a new issue