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/controllers/products_controller.rb b/app/controllers/products_controller.rb index 65b79e7..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 @@ -39,7 +40,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/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/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/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 %>
diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb index 77706ce..b44fd45 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 form-field', placeholder: "0.00", step: :any %> + <%= form_collection_select f, :category, Product.categories.keys, :to_s, :titlecase %> + <%= f.label :avatar %> <%= f.file_field :avatar , class: "form-field" %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 12f0a24..c2cc583 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -1,8 +1,27 @@

All products

<%= render partial: 'flash' %> -
-
- <%= render @products %> +
+ + + + +
+
+
+ <%= render @products %> +
+ <% @categories.each do |o, i| %> +
+ <%= render @products.where(category: i) %> +
+ <% end %>
+
+ 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'