From e76a13920500f2abcea0837df6834e963d947850 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Wed, 10 Dec 2014 21:25:21 +0100 Subject: [PATCH 1/5] 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' From 0b0d780e91ed60025bbc22c562373bce2b3c8f0d Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Wed, 10 Dec 2014 22:06:36 +0100 Subject: [PATCH 2/5] Tabs <3 --- app/controllers/products_controller.rb | 1 + app/views/layouts/_header.html.erb | 2 +- app/views/products/index.html.erb | 21 ++++++++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 17f0899..ec7b916 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -14,6 +14,7 @@ class ProductsController < ApplicationController def index @products = Product.all + @categories = Product.categories end def edit diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 60f5119..2cd2a64 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -40,7 +40,7 @@
  • <%= link_to "List" , users_path %>
  • -
  • <%= link_to 'Download schulden', admins_schulden_path(format: :csv) %>/li> +
  • <%= link_to 'Download schulden', admins_schulden_path(format: :csv) %>
  • <% end %>
  • class="active"<% end %>><%= o.titleize %>
  • <% end %> +
  • All
  • @@ -17,6 +18,9 @@ <%= render @products.where(category: i) %> <% end %> +
    + <%= render @products %> +
    From f5b97d9b5db3055c6e9efc4eb1f291f93fed39d2 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Fri, 12 Dec 2014 13:41:40 +0100 Subject: [PATCH 4/5] Move all tab to front --- app/views/products/index.html.erb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index ddef7ba..c2cc583 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -4,23 +4,23 @@

    +
    + <%= render @products %> +
    <% @categories.each do |o, i| %> -
    +
    <%= render @products.where(category: i) %>
    <% end %> -
    - <%= render @products %> -
    From b7cd3a7060274737b6ac82d51bb48fbaebe7bae3 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Mon, 15 Dec 2014 18:56:13 +0100 Subject: [PATCH 5/5] Add info about order --- app/controllers/orders_controller.rb | 3 ++- app/helpers/orders_helper.rb | 7 +++++++ app/views/orders/new.html.erb | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 84a2e57..f1ff050 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -1,4 +1,5 @@ class OrdersController < ApplicationController + include OrdersHelper def new @user = User.find(params[:user_id]) @@ -17,7 +18,7 @@ class OrdersController < ApplicationController @products = Product.all @order_products = @order.order_products if @order.save - flash[:success] = "Ordered things! Get your stuff!" + flash[:success] = order_to_sentence(@order) + " ordered. Enjoy it!" redirect_to root_path else render 'new' diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index 443227f..96d4633 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -1,2 +1,9 @@ module OrdersHelper + include ActionView::Helpers::TextHelper + + def order_to_sentence(order) + order.order_products.map { + |op| pluralize(op.count, op.product.name) + }.to_sentence + end end diff --git a/app/views/orders/new.html.erb b/app/views/orders/new.html.erb index 4d42f29..916e9d9 100644 --- a/app/views/orders/new.html.erb +++ b/app/views/orders/new.html.erb @@ -1,4 +1,4 @@ -

    Order for <%= @user.nickname %>

    +

    Order for <%= @user.nickname %> (<%= euro(@user.balance) %>)

    <%= form_for @order, url: user_orders_path(@user) do |f| %> @@ -7,7 +7,7 @@
    <%= f.fields_for :order_products do |op_field| %> <%= render 'order_products/form_row', f: op_field %> - <% end %> + <% end %>