Merge branch 'delete_products'

This commit is contained in:
benji 2015-08-25 16:05:22 +02:00
commit df91936ff8
10 changed files with 29 additions and 10 deletions

View file

@ -8,7 +8,7 @@ class OrdersController < ApplicationController
init
@order = @user.orders.build
products = (@user.products.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.all)
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
@order.g_order_items products
end
@ -22,7 +22,7 @@ class OrdersController < ApplicationController
slack_notification(@user, message)
redirect_to root_path
else
@order.g_order_items Product.all
@order.g_order_items Product.for_sale
render 'new'
end
end

View file

@ -57,6 +57,6 @@ class ProductsController < ApplicationController
private
def product_params
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories)
params.require(:product).permit(:name, :price, :avatar, :category, :stock, :calories, :deleted)
end
end

View file

@ -49,7 +49,7 @@ class UsersController < ApplicationController
@user = User.find(params[:user_id])
@dagschotel = @user.dagschotel
@products = Product.all
@products = Product.for_sale
@categories = Product.categories
end
@ -57,7 +57,7 @@ class UsersController < ApplicationController
@user = User.find(params[:user_id])
@user.dagschotel = Product.find(params[:product_id])
@products = Product.all
@products = Product.for_sale
@categories = Product.categories
if @user.save

View file

@ -48,7 +48,11 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
label_content = block_given? ? capture(&block) : options[:label]
content_tag :div, class: control_wrapper_class do
checkbox + " " + label(name, label_content)
if options[:skip_label]
checkbox
else
checkbox + " " + label(name, label_content)
end
end
end

View file

@ -30,6 +30,8 @@ class Product < ActiveRecord::Base
presence: true,
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
scope :for_sale, -> { where deleted: false }
def price
self.price_cents / 100.0
end
@ -38,4 +40,8 @@ class Product < ActiveRecord::Base
if value.is_a? String then value.sub!(',', '.') end
self.price_cents = (value.to_f * 100).to_int
end
def out_of_sale
update_attribute :deleted, true
end
end

View file

@ -4,6 +4,7 @@
<td><%= f.text_field :name, skip_label: true %></td>
<td><%= f.price_field :price, skip_label: true %></td>
<td><%= f.number_field :stock, skip_label: true %></td>
<td><%= f.check_box :deleted, skip_label: true %></td>
<td><%= f.number_field :calories, skip_label: true %></td>
<td><%= f.button "Update", class: "btn btn-primary" %></td>
<%= javascript_tag do %>

View file

@ -3,6 +3,7 @@
<td><%= product.name %></td>
<td><%= euro(product.price) %></td>
<td><%= product.stock %></td>
<td><span class="glyphicon <%= product.deleted ? "glyphicon-check" : "glyphicon-unchecked" %>"></span></td>
<td><%= product.calories %></td>
<td><%= button_to "Edit", edit_product_path(product), method: :get, class: "btn btn-default", remote: true %></td>
</tr>

View file

@ -10,6 +10,7 @@
<th>Name</th>
<th>Price</th>
<th>Stock</th>
<th>Deleted</th>
<th>Kilocalorieën</th>
<th></th>
</tr>

View file

@ -0,0 +1,5 @@
class AddDeletedToProducts < ActiveRecord::Migration
def change
add_column :products, :deleted, :boolean, default: false
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150707162949) do
ActiveRecord::Schema.define(version: 20150824142843) do
create_table "order_items", force: :cascade do |t|
t.integer "order_id"
@ -31,8 +31,8 @@ ActiveRecord::Schema.define(version: 20150707162949) do
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
create_table "products", force: :cascade do |t|
t.string "name", null: false
t.integer "price_cents", default: 0, null: false
t.string "name", null: false
t.integer "price_cents", default: 0, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
@ -40,8 +40,9 @@ ActiveRecord::Schema.define(version: 20150707162949) do
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "category", default: 0
t.integer "stock", default: 0, null: false
t.integer "stock", default: 0, null: false
t.integer "calories"
t.boolean "deleted", default: false
end
create_table "users", force: :cascade do |t|