Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
add save default dishes for new users
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhkonkov committed Mar 16, 2017
1 parent b20f4e6 commit f32d081
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/web/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create
@user = User.new(user_params)

if @user.save
::DishesService.set_default_dishes_for_user(@user)
redirect_to admin_users_path
else
render :new
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class User < ApplicationRecord
validates :name, presence: true
validates :email, presence: true, restream_email: true

has_many :user_menus
has_many :user_menus, dependent: :destroy
has_many :menus, through: :user_menus
end
2 changes: 1 addition & 1 deletion app/models/user_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class UserMenu < ApplicationRecord
belongs_to :menu
belongs_to :user

has_many :user_menu_dishes
has_many :user_menu_dishes, dependent: :destroy
has_many :dishes, through: :user_menu_dishes

scope :em, -> { where(user: User.where(neem: false)) }
Expand Down
13 changes: 13 additions & 0 deletions app/services/dishes_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class DishesService
class << self
def set_default_dishes_for_user(user)
menus = Menu.ready.where('date >= ?', Date.current)
menus.each do |menu|
user_menu = UserMenu.create(user: user, menu: menu, neem: user.neem)
menu_dishes = menu.menu_dishes.default
dishes = menu_dishes.map(&:dish)
user_menu.dishes << dishes
end
end
end
end

0 comments on commit f32d081

Please sign in to comment.