Merge pull request #46 from ZeusWPI/products_list

Products list
This commit is contained in:
benji 2015-04-03 23:56:48 +02:00
commit 9ec9f043ed
9 changed files with 85 additions and 7 deletions

View file

@ -22,3 +22,13 @@
margin-bottom: 20px;
}
#products-table .form-group {
margin-bottom: 0;
}
#products-table tr {
height: 57px;
td {
vertical-align: middle;
}
}

View file

@ -1,6 +1,8 @@
class ProductsController < ApplicationController
load_and_authorize_resource
respond_to :html, :js
def new
@product = Product.new
end
@ -17,20 +19,20 @@ class ProductsController < ApplicationController
def index
@products = Product.all
@categories = Product.categories
if current_user.admin?
render 'products_list/listview'
end
end
def edit
@product = Product.find(params[:id])
respond_with @product
end
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
flash[:success] = "Succesfully updated product"
redirect_to products_path
else
render 'edit'
end
@product.update_attributes product_params
respond_with @product
end
def destroy

View file

@ -17,7 +17,7 @@
class Product < ActiveRecord::Base
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)

View 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 %>

View file

@ -0,0 +1 @@
$("#products_row_<%= dom_id(@product) %>").replaceWith("<%= j render "products_list/product_edit_row", product: @product %>")

View 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 %>

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

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

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