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

Commit

Permalink
Merge pull request #3 from mikhkonkov/feature/add-users
Browse files Browse the repository at this point in the history
Feature/add users
  • Loading branch information
mikhkonkov authored Mar 16, 2017
2 parents 3f2a6a0 + 43489ea commit e0e78ef
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 6 deletions.
48 changes: 48 additions & 0 deletions app/controllers/web/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Web::Admin::UsersController < Web::Admin::ApplicationController
def index
@q = User.order(:name).ransack(params[:q])
@users = @q.result.page(params[:page])
end

def new
@user = User.new
end

def edit
@user = User.find(params[:id])
end

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
end
end

def update
@user = User.find(params[:id])

if @user.update user_params
redirect_to admin_users_path
else
render :edit
end
end

def destroy
@user = User.find(params[:id])
@user.destroy

redirect_to admin_users_path
end

private

def user_params
params.require(:user).permit(:name, :email, :neem, :description)
end
end
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class User < ApplicationRecord
has_many :user_menus
validates :name, presence: true
validates :email, presence: true, restream_email: true

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
7 changes: 7 additions & 0 deletions app/validators/restream_email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RestreamEmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@restream.rt.ru\z/i
record.errors.add(attribute, :invalid_email)
end
end
end
7 changes: 7 additions & 0 deletions app/views/web/admin/users/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= simple_form_for @user, url: url, html: { method: method, class: 'form-horizontal' } do |f|
= f.input :name
= f.input :email, input_html: { placeholder: '[email protected]' }
= f.input :description
.form-group
.col-sm-offset-3.col-sm-9
= f.submit 'Сохранить', class: 'btn btn-primary'
5 changes: 5 additions & 0 deletions app/views/web/admin/users/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.row
.col-md-6
%h1 Редактировать пользователя
%br
= render 'form', url: admin_user_path(@user), method: 'PUT'
29 changes: 29 additions & 0 deletions app/views/web/admin/users/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.row
.col-md-6
%h1 Пользователи
%br
.col-md-6
.pull-right
= link_to 'Добавить', new_admin_user_path, class: 'btn btn-default btn-lg btn-primary'

.row
.col-md-12

%table.table.table-hover.table-condensed.table-striped
%tr
%th Фамилия Имя
%th Email
%th Действия
- @users.each do |user|
%tr
%td
= user.name
%td
= user.email
%td
= link_to 'Редактировать', edit_admin_user_path(user), class: 'btn btn-info btn-sm'
= link_to 'Удалить', admin_user_path(user), method: :delete, data: { confirm: 'Вы уверены?' }, class: 'btn btn-danger btn-sm'

.row
.col-md-12
= paginate @users
5 changes: 5 additions & 0 deletions app/views/web/admin/users/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.row
.col-md-6
%h1 Добавить пользователя
%br
= render 'form', url: admin_users_path, method: 'POST'
1 change: 1 addition & 0 deletions app/views/web/shared/_nav.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
= active_link_to 'Меню', edit_admin_menu_path(Date.current), class_active: 'active', wrap_tag: :li
= active_link_to 'Отчеты', admin_user_menus_path(date: Date.current), class_active: 'active', wrap_tag: :li
= active_link_to 'Блюда', admin_dishes_path, class_active: 'active', wrap_tag: :li
= active_link_to 'Пользователи', admin_users_path, class_active: 'active', wrap_tag: :li
22 changes: 19 additions & 3 deletions config/locales/activerecord.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ ru:
activerecord:
attributes:
dish:
name: 'Название'
description: 'Описание'
dish_type: 'Тип блюда'
name: Название
description: Описание
dish_type: Тип блюда
user:
name: Фамилия Имя
email: Email
neem: Не есть
description: Описание

errors:
models:
user:
attributes:
email:
blank: Введите email
invalid_email: Введите Рестрим email, вида [email protected]
name:
blank: Введите Фамилию и Имя

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
root to: 'welcome#index'
resources :dishes, only: [:index, :edit, :new, :create, :update, :destroy]
resources :user_menus, only: [:index]
resources :users, except: :show
resources :menus, param: :date, only: [:edit, :update] do
member do
put :approve
Expand Down
47 changes: 47 additions & 0 deletions test/controllers/web/admin/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'test_helper'

class Web::Admin::UsersControllerTest < ActionController::TestCase
setup do
@user = create :user_with_user_menus, neem: false
@user_attrs = attributes_for :user
admin_http_login
end

test 'index' do
get :index
assert_response :success
end

test 'new' do
get :new
assert_response :success
end

test 'create' do
assert_difference('User.count', +1) do
post :create, params: { user: @user_attrs }
end
assert_redirected_to admin_users_path
end

test 'edit' do
get :edit, params: { id: @user.id }
assert_response :success
end

test 'update' do
new_email = '[email protected]'
put :update, params: { id: @user.id,
user: { email: new_email } }
assert { @user.reload.email = new_email }
end

test 'destroy' do
assert_difference('User.count', -1) do
delete :destroy, params: { id: @user.id }
end

assert_redirected_to admin_users_path
end

end
10 changes: 10 additions & 0 deletions test/factories/user_menus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@
create_list :user_menu_dish, 3, user_menu: user_menu
end
end

factory :user_menu_with_user_menu_dishes do
transient do
posts_count 5
end
after(:create) do |user_menu, evaluator|
create_list(:user_menu_dish, evaluator.posts_count, user_menu: user_menu)
end
end

end
end
12 changes: 11 additions & 1 deletion test/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
FactoryGirl.define do
factory :user do
name
email
email '[email protected]'
description

factory :user_with_user_menus do
transient do
posts_count 5
end
after(:create) do |user, evaluator|
create_list(:user_menu_with_user_menu_dishes, evaluator.posts_count, user: user)
end
end

end
end

0 comments on commit e0e78ef

Please sign in to comment.