This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mikhkonkov/feature/add-users
Feature/add users
- Loading branch information
Showing
15 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: Введите Фамилию и Имя | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
FactoryGirl.define do | ||
factory :user do | ||
name | ||
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 |