commit
9ec9f043ed
9 changed files with 85 additions and 7 deletions
|
@ -22,3 +22,13 @@
|
||||||
margin-bottom: 20px;
|
margin-bottom: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#products-table .form-group {
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#products-table tr {
|
||||||
|
height: 57px;
|
||||||
|
td {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
class ProductsController < ApplicationController
|
class ProductsController < ApplicationController
|
||||||
load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
|
|
||||||
|
respond_to :html, :js
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@product = Product.new
|
@product = Product.new
|
||||||
end
|
end
|
||||||
|
@ -17,20 +19,20 @@ class ProductsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@products = Product.all
|
@products = Product.all
|
||||||
@categories = Product.categories
|
@categories = Product.categories
|
||||||
|
if current_user.admin?
|
||||||
|
render 'products_list/listview'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
@product = Product.find(params[:id])
|
@product = Product.find(params[:id])
|
||||||
|
respond_with @product
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@product = Product.find(params[:id])
|
@product = Product.find(params[:id])
|
||||||
if @product.update_attributes(product_params)
|
@product.update_attributes product_params
|
||||||
flash[:success] = "Succesfully updated product"
|
respond_with @product
|
||||||
redirect_to products_path
|
|
||||||
else
|
|
||||||
render 'edit'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
class Product < ActiveRecord::Base
|
class Product < ActiveRecord::Base
|
||||||
has_many :order_items
|
has_many :order_items
|
||||||
has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>" }, default_style: :medium
|
has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>", small: "40x40>" }, default_style: :medium
|
||||||
|
|
||||||
enum category: %w(food beverages other)
|
enum category: %w(food beverages other)
|
||||||
|
|
||||||
|
|
14
app/views/application/_errors.html.erb
Normal file
14
app/views/application/_errors.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<% if object.errors.any? %>
|
||||||
|
<div class="panel panel-danger form-errors">
|
||||||
|
<div class="panel-heading">
|
||||||
|
<%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.name.downcase %> from being saved:
|
||||||
|
</div>
|
||||||
|
<div class="panel-body">
|
||||||
|
<ul>
|
||||||
|
<% object.errors.full_messages.each do |msg| %>
|
||||||
|
<li><%= msg %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
1
app/views/products/edit.js.erb
Normal file
1
app/views/products/edit.js.erb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render "products_list/product_edit_row", product: @product %>")
|
6
app/views/products/update.js.erb
Normal file
6
app/views/products/update.js.erb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<% if @product.errors.any? %>
|
||||||
|
$('#products-errors').html("<%= j render 'errors', object: @product %>")
|
||||||
|
<% else %>
|
||||||
|
$('#products-errors').html('')
|
||||||
|
$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render 'products_list/product_row', product: @product %>");
|
||||||
|
<% end %>
|
20
app/views/products_list/_product_edit_row.html.erb
Normal file
20
app/views/products_list/_product_edit_row.html.erb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<tr id="products_row_<%= dom_id(product) %>">
|
||||||
|
<%= f_form_for product, remote: true do |f| %>
|
||||||
|
<td><%= image_tag product.avatar(:small) %></td>
|
||||||
|
<td><%= f.text_field :name, skip_label: true %></td>
|
||||||
|
<td><%= f.price_field :price, skip_label: true %></td>
|
||||||
|
<td><%= f.number_field :stock, skip_label: true %></td>
|
||||||
|
<td><%= f.button "Update", class: "btn btn-primary" %></td>
|
||||||
|
<%= javascript_tag do %>
|
||||||
|
var id = "#edit_<%= dom_id(product) %>";
|
||||||
|
var inputs = $(id).parent().find('input');
|
||||||
|
|
||||||
|
$(id).parent().find('button').on('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
$(id).append(inputs.clone());
|
||||||
|
$(id).append('<input type="hidden" name="_method" value="patch">');
|
||||||
|
$(id).submit();
|
||||||
|
});
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</tr>
|
7
app/views/products_list/_product_row.html.erb
Normal file
7
app/views/products_list/_product_row.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<tr id="products_row_<%= dom_id(product) %>">
|
||||||
|
<td><%= image_tag product.avatar(:small) %></td>
|
||||||
|
<td><%= product.name %></td>
|
||||||
|
<td><%= euro(product.price) %></td>
|
||||||
|
<td><%= product.stock %></td>
|
||||||
|
<td><%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %></td>
|
||||||
|
</tr>
|
18
app/views/products_list/listview.html.erb
Normal file
18
app/views/products_list/listview.html.erb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<div id="products-errors"></div>
|
||||||
|
<div class="row products">
|
||||||
|
<div class="col-md-8 col-md-offset-2">
|
||||||
|
<h1>Products</h1>
|
||||||
|
<%= render partial: 'flash' %>
|
||||||
|
|
||||||
|
<table id="products-table" class="table table-striped">
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Price</th>
|
||||||
|
<th>Stock</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
<%= render partial: 'products_list/product_row', collection: @products, as: :product %>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
Loading…
Reference in a new issue