From 2f71e95a81f40e055c726249b5bc4160c52a7140 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 30 Jun 2015 22:30:34 +0200 Subject: [PATCH] Add private option to users --- app/controllers/orders_controller.rb | 7 ++++++- app/controllers/users_controller.rb | 2 +- app/models/order.rb | 2 +- app/models/order_item.rb | 4 ++-- app/models/user.rb | 1 + app/views/users/edit.html.erb | 5 +++++ db/migrate/20150630183223_add_private_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 8 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20150630183223_add_private_to_users.rb diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 21577ca..ea552b5 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -40,7 +40,7 @@ class OrdersController < ApplicationController end def overview - @users = User.members.order(:uid) + @users = User.members.publik.order(:uid) end def quickpay @@ -65,6 +65,11 @@ class OrdersController < ApplicationController redirect_to root_path end + if @user.private && current_user != @user + flash[:error] = "You can't order stuff for this person." + redirect_to root_path + end + unless current_user.koelkast? || current_user.admin? || current_user == @user flash[:error] = "Please don't order stuff for other people" redirect_to root_path diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ddef667..7f90b0c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -79,6 +79,6 @@ class UsersController < ApplicationController end def user_params - params.permit(:user).permit(:avatar) + params.fetch(:user, {}).permit(:avatar, :private) end end diff --git a/app/models/order.rb b/app/models/order.rb index cf6a450..c42cc81 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -13,7 +13,7 @@ class Order < ActiveRecord::Base include ActionView::Helpers::TextHelper - after_create { self.user.increment!(:debt_cents, price_cents) } + after_create { self.user.increment!(:debt_cents, price_cents) } belongs_to :user, counter_cache: true has_many :order_items, dependent: :destroy diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 534e3da..0ead532 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -13,9 +13,9 @@ class OrderItem < ActiveRecord::Base belongs_to :product validates :product, presence: true - validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 } - after_create :remove_from_stock + after_create :remove_from_stock accepts_nested_attributes_for :product diff --git a/app/models/user.rb b/app/models/user.rb index 4eb0cd5..936a7d1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,6 +41,7 @@ class User < ActiveRecord::Base content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] } scope :members, -> { where koelkast: false } + scope :publik, -> { where private: false } def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 63d128c..5a8e457 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -5,6 +5,11 @@ <%= f.error_messages %> <%= f.file_field :avatar %> +

+ If you check this option, nobody will be able to order stuff for you through koelkast. + Only on your account things can be ordered. +

+ <%= f.check_box :private %> <%= f.submit "Update" %> <% end %> diff --git a/db/migrate/20150630183223_add_private_to_users.rb b/db/migrate/20150630183223_add_private_to_users.rb new file mode 100644 index 0000000..77f6813 --- /dev/null +++ b/db/migrate/20150630183223_add_private_to_users.rb @@ -0,0 +1,5 @@ +class AddPrivateToUsers < ActiveRecord::Migration + def change + add_column :users, :private, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 6fe7769..ffa911b 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: 20150325154600) do +ActiveRecord::Schema.define(version: 20150630183223) do create_table "order_items", force: :cascade do |t| t.integer "order_id" @@ -64,6 +64,7 @@ ActiveRecord::Schema.define(version: 20150325154600) do t.string "provider" t.string "uid" t.string "encrypted_password", default: "", null: false + t.boolean "private", default: false end add_index "users", ["koelkast"], name: "index_users_on_koelkast"