From e76a13920500f2abcea0837df6834e963d947850 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Wed, 10 Dec 2014 21:25:21 +0100 Subject: [PATCH] Add the migrations, add the category to the form --- app/controllers/products_controller.rb | 2 +- app/models/product.rb | 3 +++ app/models/user.rb | 7 ++++++- app/views/products/_form.html.erb | 2 ++ db/migrate/20141210200008_add_category_to_products.rb | 5 +++++ db/schema.rb | 3 ++- test/fixtures/products.yml | 1 + test/fixtures/users.yml | 7 ++++++- test/models/product_test.rb | 1 + test/models/user_test.rb | 7 ++++++- 10 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20141210200008_add_category_to_products.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 65b79e7..17f0899 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -39,7 +39,7 @@ class ProductsController < ApplicationController private def product_params - params.require(:product).permit(:name, :price, :avatar) + params.require(:product).permit(:name, :price, :avatar, :category) end end diff --git a/app/models/product.rb b/app/models/product.rb index e7f09dc..5a7542f 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -11,12 +11,15 @@ # avatar_content_type :string(255) # avatar_file_size :integer # avatar_updated_at :datetime +# category :integer default(0) # class Product < ActiveRecord::Base has_many :order_products has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium + enum category: %w(food beverages other) + validates :name, presence: true validates :price, numericality: { only_integer: 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/models/user.rb b/app/models/user.rb index 4468325..11f716e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,8 +16,13 @@ # last_sign_in_at :datetime # current_sign_in_ip :string(255) # last_sign_in_ip :string(255) -# dagschotel :reference +# admin :boolean # dagschotel_id :integer +# avatar_file_name :string(255) +# avatar_content_type :string(255) +# avatar_file_size :integer +# avatar_updated_at :datetime +# orders_count :integer default(0) # class User < ActiveRecord::Base diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb index 8a13796..8ee0799 100644 --- a/app/views/products/_form.html.erb +++ b/app/views/products/_form.html.erb @@ -8,6 +8,8 @@ <%= f.label :price %> <%= f.number_field :price, value: number_with_precision(f.object.price, precision: 2), class: 'form-control', placeholder: "0.00", step: :any %> + <%= form_collection_select f, :category, Product.categories.keys, :to_s, :titlecase %> + <%= f.label :avatar %> <%= f.file_field :avatar %> diff --git a/db/migrate/20141210200008_add_category_to_products.rb b/db/migrate/20141210200008_add_category_to_products.rb new file mode 100644 index 0000000..6cc913b --- /dev/null +++ b/db/migrate/20141210200008_add_category_to_products.rb @@ -0,0 +1,5 @@ +class AddCategoryToProducts < ActiveRecord::Migration + def change + add_column :products, :category, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index a7496f5..08ab3d3 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: 20141210090544) do +ActiveRecord::Schema.define(version: 20141210200008) do create_table "order_products", force: true do |t| t.integer "order_id" @@ -38,6 +38,7 @@ ActiveRecord::Schema.define(version: 20141210090544) do t.string "avatar_content_type" t.integer "avatar_file_size" t.datetime "avatar_updated_at" + t.integer "category", default: 0 end create_table "users", force: true do |t| diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 36b426d..6de2af1 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -11,6 +11,7 @@ # avatar_content_type :string(255) # avatar_file_size :integer # avatar_updated_at :datetime +# category :integer default(0) # # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 4d5b784..640f298 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -16,8 +16,13 @@ # last_sign_in_at :datetime # current_sign_in_ip :string(255) # last_sign_in_ip :string(255) -# dagschotel :reference +# admin :boolean # dagschotel_id :integer +# avatar_file_name :string(255) +# avatar_content_type :string(255) +# avatar_file_size :integer +# avatar_updated_at :datetime +# orders_count :integer default(0) # # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html diff --git a/test/models/product_test.rb b/test/models/product_test.rb index c2ac6dc..e089dca 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -11,6 +11,7 @@ # avatar_content_type :string(255) # avatar_file_size :integer # avatar_updated_at :datetime +# category :integer default(0) # require 'test_helper' diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 1e0afd3..086c7cf 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -16,8 +16,13 @@ # last_sign_in_at :datetime # current_sign_in_ip :string(255) # last_sign_in_ip :string(255) -# dagschotel :reference +# admin :boolean # dagschotel_id :integer +# avatar_file_name :string(255) +# avatar_content_type :string(255) +# avatar_file_size :integer +# avatar_updated_at :datetime +# orders_count :integer default(0) # require 'test_helper'