From 4c562ae82a15d381ab4c12cfc31e226a1d1f7f61 Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 11:56:06 +0400 Subject: [PATCH 1/7] add users controller --- app/controllers/web/admin/users_controller.rb | 12 ++++++++++++ app/views/web/admin/users/edit.html.haml | 2 ++ app/views/web/admin/users/index.html.haml | 2 ++ app/views/web/admin/users/new.html.haml | 2 ++ app/views/web/shared/_nav.html.haml | 1 + config/routes.rb | 1 + .../web/admin/users_controller_test.rb | 19 +++++++++++++++++++ 7 files changed, 39 insertions(+) create mode 100644 app/controllers/web/admin/users_controller.rb create mode 100644 app/views/web/admin/users/edit.html.haml create mode 100644 app/views/web/admin/users/index.html.haml create mode 100644 app/views/web/admin/users/new.html.haml create mode 100644 test/controllers/web/admin/users_controller_test.rb diff --git a/app/controllers/web/admin/users_controller.rb b/app/controllers/web/admin/users_controller.rb new file mode 100644 index 0000000..e503568 --- /dev/null +++ b/app/controllers/web/admin/users_controller.rb @@ -0,0 +1,12 @@ +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 + end + + def edit + end +end diff --git a/app/views/web/admin/users/edit.html.haml b/app/views/web/admin/users/edit.html.haml new file mode 100644 index 0000000..103db4e --- /dev/null +++ b/app/views/web/admin/users/edit.html.haml @@ -0,0 +1,2 @@ +%h1 Web::Admin::Users#edit +%p Find me in app/views/web/admin/users/edit.html.haml diff --git a/app/views/web/admin/users/index.html.haml b/app/views/web/admin/users/index.html.haml new file mode 100644 index 0000000..47662eb --- /dev/null +++ b/app/views/web/admin/users/index.html.haml @@ -0,0 +1,2 @@ +%h1 Web::Admin::Users#index +%p Find me in app/views/web/admin/users/index.html.haml diff --git a/app/views/web/admin/users/new.html.haml b/app/views/web/admin/users/new.html.haml new file mode 100644 index 0000000..ac6f3cd --- /dev/null +++ b/app/views/web/admin/users/new.html.haml @@ -0,0 +1,2 @@ +%h1 Web::Admin::Users#new +%p Find me in app/views/web/admin/users/new.html.haml diff --git a/app/views/web/shared/_nav.html.haml b/app/views/web/shared/_nav.html.haml index d15664e..61fa601 100644 --- a/app/views/web/shared/_nav.html.haml +++ b/app/views/web/shared/_nav.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index e4a7aea..cf8fef8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ root to: 'welcome#index' resources :dishes, only: [:index, :edit, :new, :create, :update, :destroy] resources :user_menus, only: [:index] + resources :users, only: [:index, :new, :create, :edit, :update] resources :menus, param: :date, only: [:edit, :update] do member do put :approve diff --git a/test/controllers/web/admin/users_controller_test.rb b/test/controllers/web/admin/users_controller_test.rb new file mode 100644 index 0000000..df8a35e --- /dev/null +++ b/test/controllers/web/admin/users_controller_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class Web::Admin::UsersControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get web_admin_users_index_url + assert_response :success + end + + test "should get new" do + get web_admin_users_new_url + assert_response :success + end + + test "should get edit" do + get web_admin_users_edit_url + assert_response :success + end + +end From f7373c9b159eaf23f7010799f00df648690f073d Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 12:19:47 +0400 Subject: [PATCH 2/7] add index page --- app/views/web/admin/users/index.html.haml | 28 +++++++++++++++++-- config/routes.rb | 2 +- .../web/admin/users_controller_test.rb | 18 +++++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/views/web/admin/users/index.html.haml b/app/views/web/admin/users/index.html.haml index 47662eb..362789e 100644 --- a/app/views/web/admin/users/index.html.haml +++ b/app/views/web/admin/users/index.html.haml @@ -1,2 +1,26 @@ -%h1 Web::Admin::Users#index -%p Find me in app/views/web/admin/users/index.html.haml +.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 Действия + - @users.each do |user| + %tr + %td + = user.name + %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 diff --git a/config/routes.rb b/config/routes.rb index cf8fef8..673a330 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ root to: 'welcome#index' resources :dishes, only: [:index, :edit, :new, :create, :update, :destroy] resources :user_menus, only: [:index] - resources :users, only: [:index, :new, :create, :edit, :update] + resources :users, only: [:index, :new, :create, :edit, :update, :destroy] resources :menus, param: :date, only: [:edit, :update] do member do put :approve diff --git a/test/controllers/web/admin/users_controller_test.rb b/test/controllers/web/admin/users_controller_test.rb index df8a35e..342d1d9 100644 --- a/test/controllers/web/admin/users_controller_test.rb +++ b/test/controllers/web/admin/users_controller_test.rb @@ -1,18 +1,22 @@ require 'test_helper' -class Web::Admin::UsersControllerTest < ActionDispatch::IntegrationTest - test "should get index" do - get web_admin_users_index_url +class Web::Admin::UsersControllerTest < ActionController::TestCase + setup do + admin_http_login + end + + test 'index' do + get :index assert_response :success end - test "should get new" do - get web_admin_users_new_url + test 'new' do + get :new assert_response :success end - test "should get edit" do - get web_admin_users_edit_url + test 'edit' do + get :edit assert_response :success end From c192cbd75b19aaae49008e85846ae7f41ec4bc32 Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 13:29:26 +0400 Subject: [PATCH 3/7] add email validation --- app/controllers/web/admin/users_controller.rb | 35 +++++++++++++++++++ app/models/user.rb | 3 ++ app/validators/restream_email_validator.rb | 7 ++++ app/views/web/admin/users/_form.html.haml | 7 ++++ app/views/web/admin/users/edit.html.haml | 7 ++-- app/views/web/admin/users/new.html.haml | 7 ++-- config/locales/ru.yml | 11 ++++++ 7 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 app/validators/restream_email_validator.rb create mode 100644 app/views/web/admin/users/_form.html.haml diff --git a/app/controllers/web/admin/users_controller.rb b/app/controllers/web/admin/users_controller.rb index e503568..824f06e 100644 --- a/app/controllers/web/admin/users_controller.rb +++ b/app/controllers/web/admin/users_controller.rb @@ -5,8 +5,43 @@ def index 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 + 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 diff --git a/app/models/user.rb b/app/models/user.rb index 1e3ae0f..c2dc5be 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,7 @@ class User < ApplicationRecord + validates :name, presence: true + validates :email, presence: true, restream_email: true + has_many :user_menus has_many :menus, through: :user_menus end diff --git a/app/validators/restream_email_validator.rb b/app/validators/restream_email_validator.rb new file mode 100644 index 0000000..6b068b7 --- /dev/null +++ b/app/validators/restream_email_validator.rb @@ -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 diff --git a/app/views/web/admin/users/_form.html.haml b/app/views/web/admin/users/_form.html.haml new file mode 100644 index 0000000..0f97582 --- /dev/null +++ b/app/views/web/admin/users/_form.html.haml @@ -0,0 +1,7 @@ += simple_form_for @user, url: url, html: { method: method, class: 'form-horizontal' } do |f| + = f.input :name + = f.input :email + = f.input :description + .form-group + .col-sm-offset-3.col-sm-9 + = f.submit 'Сохранить', class: 'btn btn-primary' diff --git a/app/views/web/admin/users/edit.html.haml b/app/views/web/admin/users/edit.html.haml index 103db4e..c2290b8 100644 --- a/app/views/web/admin/users/edit.html.haml +++ b/app/views/web/admin/users/edit.html.haml @@ -1,2 +1,5 @@ -%h1 Web::Admin::Users#edit -%p Find me in app/views/web/admin/users/edit.html.haml +.row + .col-md-6 + %h1 Редактировать пользователя + %br + = render 'form', url: admin_user_path(@user), method: 'PUT' diff --git a/app/views/web/admin/users/new.html.haml b/app/views/web/admin/users/new.html.haml index ac6f3cd..b51f9e4 100644 --- a/app/views/web/admin/users/new.html.haml +++ b/app/views/web/admin/users/new.html.haml @@ -1,2 +1,5 @@ -%h1 Web::Admin::Users#new -%p Find me in app/views/web/admin/users/new.html.haml +.row + .col-md-6 + %h1 Добавить пользователя + %br + = render 'form', url: admin_users_path, method: 'POST' diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 9326e2a..0a98e71 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -1,4 +1,15 @@ ru: + activerecord: + errors: + models: + user: + attributes: + email: + blank: Введите email + invalid_email: Введите Рестрим email, вида username@restream.rt.ru + name: + blank: Введите Фамилию и Имя + date: formats: menu_date: '%d %B, %A' From 4c39c03a8146d677b9071c19df0595c507e8a28b Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 13:41:19 +0400 Subject: [PATCH 4/7] fix locales --- app/views/web/admin/users/_form.html.haml | 2 +- app/views/web/admin/users/index.html.haml | 5 ++++- config/locales/activerecord.ru.yml | 22 +++++++++++++++++++--- config/locales/ru.yml | 11 ----------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/views/web/admin/users/_form.html.haml b/app/views/web/admin/users/_form.html.haml index 0f97582..40939b0 100644 --- a/app/views/web/admin/users/_form.html.haml +++ b/app/views/web/admin/users/_form.html.haml @@ -1,6 +1,6 @@ = simple_form_for @user, url: url, html: { method: method, class: 'form-horizontal' } do |f| = f.input :name - = f.input :email + = f.input :email, input_html: { placeholder: 'username@restream.rt.ru' } = f.input :description .form-group .col-sm-offset-3.col-sm-9 diff --git a/app/views/web/admin/users/index.html.haml b/app/views/web/admin/users/index.html.haml index 362789e..53c6db9 100644 --- a/app/views/web/admin/users/index.html.haml +++ b/app/views/web/admin/users/index.html.haml @@ -11,12 +11,15 @@ %table.table.table-hover.table-condensed.table-striped %tr - %th Имя + %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' diff --git a/config/locales/activerecord.ru.yml b/config/locales/activerecord.ru.yml index 2a78da4..f84e9c9 100644 --- a/config/locales/activerecord.ru.yml +++ b/config/locales/activerecord.ru.yml @@ -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, вида username@restream.rt.ru + name: + blank: Введите Фамилию и Имя + diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 0a98e71..9326e2a 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -1,15 +1,4 @@ ru: - activerecord: - errors: - models: - user: - attributes: - email: - blank: Введите email - invalid_email: Введите Рестрим email, вида username@restream.rt.ru - name: - blank: Введите Фамилию и Имя - date: formats: menu_date: '%d %B, %A' From b20f4e6998faec68087ca3390f81f8096e46edc0 Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 14:28:02 +0400 Subject: [PATCH 5/7] fix tests --- .../web/admin/users_controller_test.rb | 18 +++++++++++++++++- test/factories/users.rb | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/test/controllers/web/admin/users_controller_test.rb b/test/controllers/web/admin/users_controller_test.rb index 342d1d9..6bc4f9a 100644 --- a/test/controllers/web/admin/users_controller_test.rb +++ b/test/controllers/web/admin/users_controller_test.rb @@ -2,6 +2,8 @@ class Web::Admin::UsersControllerTest < ActionController::TestCase setup do + @user = create :user, neem: false + @user_attrs = attributes_for :user admin_http_login end @@ -15,9 +17,23 @@ class Web::Admin::UsersControllerTest < ActionController::TestCase assert_response :success end + test 'create' do + user_count = User.count + post :create, params: { user: @user_attrs } + assert_response :redirect + assert { User.count == user_count + 1 } + end + test 'edit' do - get :edit + get :edit, params: { id: @user.id } assert_response :success end + test 'update' do + new_email = 'test@restream.rt.ru' + put :update, params: { id: @user.id, + user: { email: new_email } } + assert { @user.reload.email = new_email } + end + end diff --git a/test/factories/users.rb b/test/factories/users.rb index c23b5d0..4bcb59c 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :user do name - email + email 'username@restream.rt.ru' description end end From f32d081e39de337802b2487b07025410296abb75 Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 18:16:12 +0400 Subject: [PATCH 6/7] add save default dishes for new users --- app/controllers/web/admin/users_controller.rb | 1 + app/models/user.rb | 2 +- app/models/user_menu.rb | 2 +- app/services/dishes_service.rb | 13 +++++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 app/services/dishes_service.rb diff --git a/app/controllers/web/admin/users_controller.rb b/app/controllers/web/admin/users_controller.rb index 824f06e..b633e72 100644 --- a/app/controllers/web/admin/users_controller.rb +++ b/app/controllers/web/admin/users_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index c2dc5be..7d41583 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_menu.rb b/app/models/user_menu.rb index ce43e93..c09270d 100644 --- a/app/models/user_menu.rb +++ b/app/models/user_menu.rb @@ -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)) } diff --git a/app/services/dishes_service.rb b/app/services/dishes_service.rb new file mode 100644 index 0000000..87ceb96 --- /dev/null +++ b/app/services/dishes_service.rb @@ -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 From 43489ea28c49a32fff8a778f64b0113710dda614 Mon Sep 17 00:00:00 2001 From: Konkov Mikhail Date: Thu, 16 Mar 2017 19:01:47 +0400 Subject: [PATCH 7/7] add destroy test --- config/routes.rb | 2 +- .../web/admin/users_controller_test.rb | 18 +++++++++++++----- test/factories/user_menus.rb | 10 ++++++++++ test/factories/users.rb | 10 ++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 673a330..1906a6a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ root to: 'welcome#index' resources :dishes, only: [:index, :edit, :new, :create, :update, :destroy] resources :user_menus, only: [:index] - resources :users, only: [:index, :new, :create, :edit, :update, :destroy] + resources :users, except: :show resources :menus, param: :date, only: [:edit, :update] do member do put :approve diff --git a/test/controllers/web/admin/users_controller_test.rb b/test/controllers/web/admin/users_controller_test.rb index 6bc4f9a..1a98ea6 100644 --- a/test/controllers/web/admin/users_controller_test.rb +++ b/test/controllers/web/admin/users_controller_test.rb @@ -2,7 +2,7 @@ class Web::Admin::UsersControllerTest < ActionController::TestCase setup do - @user = create :user, neem: false + @user = create :user_with_user_menus, neem: false @user_attrs = attributes_for :user admin_http_login end @@ -18,10 +18,10 @@ class Web::Admin::UsersControllerTest < ActionController::TestCase end test 'create' do - user_count = User.count - post :create, params: { user: @user_attrs } - assert_response :redirect - assert { User.count == user_count + 1 } + assert_difference('User.count', +1) do + post :create, params: { user: @user_attrs } + end + assert_redirected_to admin_users_path end test 'edit' do @@ -36,4 +36,12 @@ class Web::Admin::UsersControllerTest < ActionController::TestCase 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 diff --git a/test/factories/user_menus.rb b/test/factories/user_menus.rb index 345fb23..81e4de7 100644 --- a/test/factories/user_menus.rb +++ b/test/factories/user_menus.rb @@ -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 diff --git a/test/factories/users.rb b/test/factories/users.rb index 4bcb59c..112e9a4 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -3,5 +3,15 @@ name email 'username@restream.rt.ru' 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