Merge branch 'feature/17/products-tabs'

This commit is contained in:
Benjamin Cousaert 2014-12-15 19:08:54 +01:00
commit ef5abb6324
14 changed files with 67 additions and 11 deletions

View file

@ -1,4 +1,5 @@
class OrdersController < ApplicationController class OrdersController < ApplicationController
include OrdersHelper
def new def new
@user = User.find(params[:user_id]) @user = User.find(params[:user_id])
@ -17,7 +18,7 @@ class OrdersController < ApplicationController
@products = Product.all @products = Product.all
@order_products = @order.order_products @order_products = @order.order_products
if @order.save if @order.save
flash[:success] = "Ordered things! Get your stuff!" flash[:success] = order_to_sentence(@order) + " ordered. Enjoy it!"
redirect_to root_path redirect_to root_path
else else
render 'new' render 'new'

View file

@ -14,6 +14,7 @@ class ProductsController < ApplicationController
def index def index
@products = Product.all @products = Product.all
@categories = Product.categories
end end
def edit def edit
@ -39,7 +40,7 @@ class ProductsController < ApplicationController
private private
def product_params def product_params
params.require(:product).permit(:name, :price, :avatar) params.require(:product).permit(:name, :price, :avatar, :category)
end end
end end

View file

@ -1,2 +1,9 @@
module OrdersHelper 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 end

View file

@ -11,12 +11,15 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
has_many :order_products has_many :order_products
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
enum category: %w(food beverages other)
validates :name, presence: true validates :name, presence: true
validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0 } 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"] }, validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # 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 class User < ActiveRecord::Base

View file

@ -1,4 +1,4 @@
<h3>Order for <%= @user.nickname %></h3> <h3>Order for <%= @user.nickname %> (<%= euro(@user.balance) %>)</h3>
<div class="row"> <div class="row">
<%= form_for @order, url: user_orders_path(@user) do |f| %> <%= form_for @order, url: user_orders_path(@user) do |f| %>
@ -7,7 +7,7 @@
<div class="col-md-12"> <div class="col-md-12">
<%= f.fields_for :order_products do |op_field| %> <%= f.fields_for :order_products do |op_field| %>
<%= render 'order_products/form_row', f: op_field %> <%= render 'order_products/form_row', f: op_field %>
<% end %> <% end %>
</div> </div>
<div class="col-md-3 form_total"> <div class="col-md-3 form_total">

View file

@ -8,6 +8,8 @@
<%= f.label :price %> <%= 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 %> <%= 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.label :avatar %>
<%= f.file_field :avatar , class: "form-field" %> <%= f.file_field :avatar , class: "form-field" %>

View file

@ -1,8 +1,27 @@
<h1>All products</h1> <h1>All products</h1>
<%= render partial: 'flash' %> <%= render partial: 'flash' %>
<div class="row products"> <div role="tabpanel">
<div class="col-md-12"> <!-- Nav tabs -->
<%= render @products %> <ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#all" role="tab" data-toggle="tab">All</a></li>
<% @categories.each do |o, i| %>
<li role="presentation"><a href="#<%= o %>" aria-controls="<%= o %>" role="tab" data-toggle="tab"><%= o.titleize %></a></li>
<% end %>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<br />
<div role="tabpanel" class="tab-pane active" id="all">
<%= render @products %>
</div>
<% @categories.each do |o, i| %>
<div role="tabpanel" class="tab-pane" id="<%= o %>">
<%= render @products.where(category: i) %>
</div>
<% end %>
</div> </div>
</div> </div>

View file

@ -0,0 +1,5 @@
class AddCategoryToProducts < ActiveRecord::Migration
def change
add_column :products, :category, :integer, default: 0
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "order_products", force: true do |t|
t.integer "order_id" t.integer "order_id"
@ -38,6 +38,7 @@ ActiveRecord::Schema.define(version: 20141210090544) do
t.string "avatar_content_type" t.string "avatar_content_type"
t.integer "avatar_file_size" t.integer "avatar_file_size"
t.datetime "avatar_updated_at" t.datetime "avatar_updated_at"
t.integer "category", default: 0
end end
create_table "users", force: true do |t| create_table "users", force: true do |t|

View file

@ -11,6 +11,7 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # 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 # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -11,6 +11,7 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
require 'test_helper' require 'test_helper'

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # 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' require 'test_helper'