Merge master

This commit is contained in:
benji 2015-08-25 16:05:13 +02:00
commit cc9e4ed6b3
11 changed files with 30 additions and 1 deletions

View file

@ -57,6 +57,6 @@ class ProductsController < ApplicationController
private
def product_params
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :deleted)
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories, :deleted)
end
end

View file

@ -0,0 +1,13 @@
module ProductsHelper
def kcal(calories)
calories.to_s + " kcal"
end
def kcal_tag(calories)
if calories
content_tag :small, kcal(calories)
else
'&nbsp;'.html_safe
end
end
end

View file

@ -13,6 +13,7 @@
# avatar_updated_at :datetime
# category :integer default("0")
# stock :integer default("0"), not null
# calories :integer default("0") // expressed in kcal
#
class Product < ActiveRecord::Base
@ -24,6 +25,7 @@ class Product < ActiveRecord::Base
validates :name, presence: true
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
validates_attachment :avatar,
presence: true,
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }

View file

@ -5,6 +5,9 @@
<%= image_tag product.avatar %>
</div>
<div class="caption">
<h6>
<%= kcal_tag product.calories %>
</h6>
<h4 class="text-nowrap">
<%= content_tag :span, product.name %>
<%= content_tag :small, euro(product.price) %>

View file

@ -7,6 +7,7 @@
<%= f.price_field :price %>
<%= f.collection_select :category, Product.categories.keys %>
<%= f.number_field :stock %>
<%= f.number_field :calories %>
<%= f.file_field :avatar %>
<%= f.submit %>

View file

@ -4,6 +4,7 @@
<%= image_tag product.avatar %>
</div>
<div class="caption">
<%= kcal_tag product.calories %>
<h4><%= product.name %></h4>
<h3><%= euro(product.price) %></h3>
<h6>(In stock: <%= product.stock %>)</h6>

View file

@ -5,6 +5,7 @@
<td><%= f.price_field :price, skip_label: true %></td>
<td><%= f.number_field :stock, skip_label: true %></td>
<td><%= f.check_box :deleted, skip_label: true %></td>
<td><%= f.number_field :calories, skip_label: true %></td>
<td><%= f.button "Update", class: "btn btn-primary" %></td>
<%= javascript_tag do %>
var id = "#edit_<%= dom_id(product) %>";

View file

@ -4,5 +4,6 @@
<td><%= euro(product.price) %></td>
<td><%= product.stock %></td>
<td><span class="glyphicon <%= product.deleted ? "glyphicon-check" : "glyphicon-unchecked" %>"></span></td>
<td><%= product.calories %></td>
<td><%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %></td>
</tr>

View file

@ -11,6 +11,7 @@
<th>Price</th>
<th>Stock</th>
<th>Deleted</th>
<th>Kilocalorieën</th>
<th></th>
</tr>
<%= render partial: 'products_list/product_row', collection: @products, as: :product %>

View file

@ -0,0 +1,5 @@
class AddCaloriesToProducts < ActiveRecord::Migration
def change
add_column :products, :calories, :int
end
end

View file

@ -41,6 +41,7 @@ ActiveRecord::Schema.define(version: 20150824142843) do
t.datetime "avatar_updated_at"
t.integer "category", default: 0
t.integer "stock", default: 0, null: false
t.integer "calories"
t.boolean "deleted", default: false
end