commit
5d39abafd5
11 changed files with 31 additions and 2 deletions
|
@ -57,6 +57,6 @@ class ProductsController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def product_params
|
def product_params
|
||||||
params.require(:product).permit(:name, :price, :avatar, :category, :stock)
|
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
13
app/helpers/products_helper.rb
Normal file
13
app/helpers/products_helper.rb
Normal 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
|
||||||
|
' '.html_safe
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -13,6 +13,7 @@
|
||||||
# avatar_updated_at :datetime
|
# avatar_updated_at :datetime
|
||||||
# category :integer default("0")
|
# category :integer default("0")
|
||||||
# stock :integer default("0"), not null
|
# stock :integer default("0"), not null
|
||||||
|
# calories :integer default("0") // expressed in kcal
|
||||||
#
|
#
|
||||||
|
|
||||||
class Product < ActiveRecord::Base
|
class Product < ActiveRecord::Base
|
||||||
|
@ -24,6 +25,7 @@ class Product < ActiveRecord::Base
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
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 :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,
|
validates_attachment :avatar,
|
||||||
presence: true,
|
presence: true,
|
||||||
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
<%= image_tag product.avatar %>
|
<%= image_tag product.avatar %>
|
||||||
</div>
|
</div>
|
||||||
<div class="caption">
|
<div class="caption">
|
||||||
|
<h6>
|
||||||
|
<%= kcal_tag product.calories %>
|
||||||
|
</h6>
|
||||||
<h4 class="text-nowrap">
|
<h4 class="text-nowrap">
|
||||||
<%= content_tag :span, product.name %>
|
<%= content_tag :span, product.name %>
|
||||||
<%= content_tag :small, euro(product.price) %>
|
<%= content_tag :small, euro(product.price) %>
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
<%= f.price_field :price %>
|
<%= f.price_field :price %>
|
||||||
<%= f.collection_select :category, Product.categories.keys %>
|
<%= f.collection_select :category, Product.categories.keys %>
|
||||||
<%= f.number_field :stock %>
|
<%= f.number_field :stock %>
|
||||||
|
<%= f.number_field :calories %>
|
||||||
<%= f.file_field :avatar %>
|
<%= f.file_field :avatar %>
|
||||||
|
|
||||||
<%= f.submit %>
|
<%= f.submit %>
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<%= image_tag product.avatar %>
|
<%= image_tag product.avatar %>
|
||||||
</div>
|
</div>
|
||||||
<div class="caption">
|
<div class="caption">
|
||||||
|
<%= kcal_tag product.calories %>
|
||||||
<h4><%= product.name %></h4>
|
<h4><%= product.name %></h4>
|
||||||
<h3><%= euro(product.price) %></h3>
|
<h3><%= euro(product.price) %></h3>
|
||||||
<h6>(In stock: <%= product.stock %>)</h6>
|
<h6>(In stock: <%= product.stock %>)</h6>
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<td><%= f.text_field :name, skip_label: true %></td>
|
<td><%= f.text_field :name, skip_label: true %></td>
|
||||||
<td><%= f.price_field :price, skip_label: true %></td>
|
<td><%= f.price_field :price, skip_label: true %></td>
|
||||||
<td><%= f.number_field :stock, skip_label: true %></td>
|
<td><%= f.number_field :stock, skip_label: true %></td>
|
||||||
|
<td><%= f.number_field :calories, skip_label: true %></td>
|
||||||
<td><%= f.button "Update", class: "btn btn-primary" %></td>
|
<td><%= f.button "Update", class: "btn btn-primary" %></td>
|
||||||
<%= javascript_tag do %>
|
<%= javascript_tag do %>
|
||||||
var id = "#edit_<%= dom_id(product) %>";
|
var id = "#edit_<%= dom_id(product) %>";
|
||||||
|
|
|
@ -3,5 +3,6 @@
|
||||||
<td><%= product.name %></td>
|
<td><%= product.name %></td>
|
||||||
<td><%= euro(product.price) %></td>
|
<td><%= euro(product.price) %></td>
|
||||||
<td><%= product.stock %></td>
|
<td><%= product.stock %></td>
|
||||||
|
<td><%= product.calories %></td>
|
||||||
<td><%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %></td>
|
<td><%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Price</th>
|
<th>Price</th>
|
||||||
<th>Stock</th>
|
<th>Stock</th>
|
||||||
|
<th>Kilocalorieën</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
<%= render partial: 'products_list/product_row', collection: @products, as: :product %>
|
<%= render partial: 'products_list/product_row', collection: @products, as: :product %>
|
||||||
|
|
5
db/migrate/20150707162949_add_calories_to_products.rb
Normal file
5
db/migrate/20150707162949_add_calories_to_products.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddCaloriesToProducts < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :products, :calories, :int
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20150630183223) do
|
ActiveRecord::Schema.define(version: 20150707162949) do
|
||||||
|
|
||||||
create_table "order_items", force: :cascade do |t|
|
create_table "order_items", force: :cascade do |t|
|
||||||
t.integer "order_id"
|
t.integer "order_id"
|
||||||
|
@ -41,6 +41,7 @@ ActiveRecord::Schema.define(version: 20150630183223) do
|
||||||
t.datetime "avatar_updated_at"
|
t.datetime "avatar_updated_at"
|
||||||
t.integer "category", default: 0
|
t.integer "category", default: 0
|
||||||
t.integer "stock", default: 0, null: false
|
t.integer "stock", default: 0, null: false
|
||||||
|
t.integer "calories"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "users", force: :cascade do |t|
|
create_table "users", force: :cascade do |t|
|
||||||
|
|
Loading…
Reference in a new issue