diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 961ea70..747d3f1 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -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 diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 0000000..87d5bd7 --- /dev/null +++ b/app/helpers/products_helper.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb index a1e867a..64d4bf4 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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"] } diff --git a/app/views/order_items/_order_item.html.erb b/app/views/order_items/_order_item.html.erb index f5fd312..3f0fa03 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -5,6 +5,9 @@ <%= image_tag product.avatar %>
+
+ <%= kcal_tag product.calories %> +

<%= content_tag :span, product.name %> <%= content_tag :small, euro(product.price) %> diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb index 04911cd..be746cd 100644 --- a/app/views/products/_form.html.erb +++ b/app/views/products/_form.html.erb @@ -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 %> diff --git a/app/views/products/_product.html.erb b/app/views/products/_product.html.erb index 235c1a1..095e73a 100644 --- a/app/views/products/_product.html.erb +++ b/app/views/products/_product.html.erb @@ -4,6 +4,7 @@ <%= image_tag product.avatar %>

+ <%= kcal_tag product.calories %>

<%= product.name %>

<%= euro(product.price) %>

(In stock: <%= product.stock %>)
diff --git a/app/views/products_list/_product_edit_row.html.erb b/app/views/products_list/_product_edit_row.html.erb index 89267c9..e043de8 100644 --- a/app/views/products_list/_product_edit_row.html.erb +++ b/app/views/products_list/_product_edit_row.html.erb @@ -5,6 +5,7 @@ <%= f.price_field :price, skip_label: true %> <%= f.number_field :stock, skip_label: true %> <%= f.check_box :deleted, skip_label: true %> + <%= f.number_field :calories, skip_label: true %> <%= f.button "Update", class: "btn btn-primary" %> <%= javascript_tag do %> var id = "#edit_<%= dom_id(product) %>"; diff --git a/app/views/products_list/_product_row.html.erb b/app/views/products_list/_product_row.html.erb index 98b24bb..8b2ad5e 100644 --- a/app/views/products_list/_product_row.html.erb +++ b/app/views/products_list/_product_row.html.erb @@ -4,5 +4,6 @@ <%= euro(product.price) %> <%= product.stock %> "> + <%= product.calories %> <%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %> diff --git a/app/views/products_list/listview.html.erb b/app/views/products_list/listview.html.erb index 4f7488b..4393e5c 100644 --- a/app/views/products_list/listview.html.erb +++ b/app/views/products_list/listview.html.erb @@ -11,6 +11,7 @@ Price Stock Deleted + Kilocalorieën <%= render partial: 'products_list/product_row', collection: @products, as: :product %> diff --git a/db/migrate/20150707162949_add_calories_to_products.rb b/db/migrate/20150707162949_add_calories_to_products.rb new file mode 100644 index 0000000..78e4c6a --- /dev/null +++ b/db/migrate/20150707162949_add_calories_to_products.rb @@ -0,0 +1,5 @@ +class AddCaloriesToProducts < ActiveRecord::Migration + def change + add_column :products, :calories, :int + end +end diff --git a/db/schema.rb b/db/schema.rb index 0d121a6..2d92fa7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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