From 849d9ce04b159c6832941c043bf552de5d1cf075 Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Tue, 7 Jul 2015 20:09:58 +0200 Subject: [PATCH 1/5] Product calories: DB migration + Model update + attribute permit --- app/controllers/products_controller.rb | 2 +- app/models/product.rb | 2 ++ db/migrate/20150707162949_add_calories_to_products.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150707162949_add_calories_to_products.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index d44d99d..e4da98d 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) + params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories) end end diff --git a/app/models/product.rb b/app/models/product.rb index b0ba8a1..45eef95 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"), not null // 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/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 ffa911b..fdd48de 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| t.integer "order_id" @@ -41,6 +41,7 @@ ActiveRecord::Schema.define(version: 20150630183223) do t.datetime "avatar_updated_at" t.integer "category", default: 0 t.integer "stock", default: 0, null: false + t.integer "calories" end create_table "users", force: :cascade do |t| From a8055d6f098ba908ae2c3abc8419b51ee97299a0 Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Tue, 7 Jul 2015 20:13:37 +0200 Subject: [PATCH 2/5] Adding calories to several views --- app/views/order_items/_order_item.html.erb | 11 +++++++++++ app/views/products/_form.html.erb | 1 + app/views/products/_product.html.erb | 7 +++++++ app/views/products_list/_product_edit_row.html.erb | 1 + app/views/products_list/_product_row.html.erb | 1 + app/views/products_list/listview.html.erb | 1 + 6 files changed, 22 insertions(+) diff --git a/app/views/order_items/_order_item.html.erb b/app/views/order_items/_order_item.html.erb index 50334af..cb18833 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -5,6 +5,17 @@ <%= image_tag product.avatar %>
+
+ <% if product.calories != nil %> + <%= content_tag( :small) do + concat( product.calories) + concat ' kcal' + end + %> + <% else %> +   + <% end %> +

<%= 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..8ac2416 100644 --- a/app/views/products/_product.html.erb +++ b/app/views/products/_product.html.erb @@ -4,6 +4,13 @@ <%= image_tag product.avatar %>

+
+ <% if product.calories != nil %> + <%= product.calories %> kcal + <% else %> +   + <% end %> +

<%= 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 9ef9b8f..b41446d 100644 --- a/app/views/products_list/_product_edit_row.html.erb +++ b/app/views/products_list/_product_edit_row.html.erb @@ -4,6 +4,7 @@ <%= f.text_field :name, skip_label: true %> <%= f.price_field :price, skip_label: true %> <%= f.number_field :stock, 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 f2fc527..e554cf4 100644 --- a/app/views/products_list/_product_row.html.erb +++ b/app/views/products_list/_product_row.html.erb @@ -3,5 +3,6 @@ <%= product.name %> <%= 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 85091dd..58cf9ff 100644 --- a/app/views/products_list/listview.html.erb +++ b/app/views/products_list/listview.html.erb @@ -10,6 +10,7 @@ Name Price Stock + Kilocalorieën <%= render partial: 'products_list/product_row', collection: @products, as: :product %> From 91945b6983c43f55412eaef8bb3233fd75fc3624 Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Wed, 8 Jul 2015 13:31:32 +0200 Subject: [PATCH 3/5] Senpai Silox Styling Suggestions --- app/models/product.rb | 4 ++-- app/views/order_items/_order_item.html.erb | 10 +++++----- app/views/products/_product.html.erb | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/product.rb b/app/models/product.rb index 45eef95..af98eb9 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -13,7 +13,7 @@ # avatar_updated_at :datetime # category :integer default("0") # stock :integer default("0"), not null -# calories :integer default("0"), not null // expressed in kcal +# calories :integer default("0") // expressed in kcal # class Product < ActiveRecord::Base @@ -25,7 +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 :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 cb18833..2245ebb 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -6,12 +6,12 @@
- <% if product.calories != nil %> - <%= content_tag( :small) do - concat( product.calories) - concat ' kcal' + <% if product.calories %> + <%= content_tag :small do + concat product.calories + concat ' kcal' end - %> + %> <% else %>   <% end %> diff --git a/app/views/products/_product.html.erb b/app/views/products/_product.html.erb index 8ac2416..3ee3c50 100644 --- a/app/views/products/_product.html.erb +++ b/app/views/products/_product.html.erb @@ -5,7 +5,7 @@
- <% if product.calories != nil %> + <% if product.calories %> <%= product.calories %> kcal <% else %>   From ee86411d5844d3d1674d10f7114691de382f5eae Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Wed, 8 Jul 2015 17:38:51 +0200 Subject: [PATCH 4/5] Using helper to generate kcal tag As suggested by Benji --- app/views/order_items/_order_item.html.erb | 10 +--------- app/views/products/_product.html.erb | 8 +------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/app/views/order_items/_order_item.html.erb b/app/views/order_items/_order_item.html.erb index 2245ebb..db1c817 100644 --- a/app/views/order_items/_order_item.html.erb +++ b/app/views/order_items/_order_item.html.erb @@ -6,15 +6,7 @@
- <% if product.calories %> - <%= content_tag :small do - concat product.calories - concat ' kcal' - end - %> - <% else %> -   - <% end %> + <%= kcal_tag product.calories %>

<%= content_tag :span, product.name %> diff --git a/app/views/products/_product.html.erb b/app/views/products/_product.html.erb index 3ee3c50..095e73a 100644 --- a/app/views/products/_product.html.erb +++ b/app/views/products/_product.html.erb @@ -4,13 +4,7 @@ <%= image_tag product.avatar %>

-
- <% if product.calories %> - <%= product.calories %> kcal - <% else %> -   - <% end %> -
+ <%= kcal_tag product.calories %>

<%= product.name %>

<%= euro(product.price) %>

(In stock: <%= product.stock %>)
From 595d8ed2820354d79fa3d355d3a18761ddf69da0 Mon Sep 17 00:00:00 2001 From: David Vandorpe Date: Wed, 8 Jul 2015 17:43:20 +0200 Subject: [PATCH 5/5] Forgot to commit helper --- app/helpers/products_helper.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/helpers/products_helper.rb 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