Skip to content

Commit

Permalink
update controller with ability, close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
zakharoff committed May 23, 2019
1 parent f4df122 commit c29a0fc
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 44 deletions.
1 change: 0 additions & 1 deletion app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
*/

@import "bootstrap";
@import "fontawesome_all.min";
@import "style";
@import "custom";
1 change: 0 additions & 1 deletion app/assets/stylesheets/fontawesome_all.min.css

This file was deleted.

19 changes: 15 additions & 4 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class TasksController < ApplicationController
before_action :authenticate_user!, only: %i[new create]
before_action :authenticate_user!, only: %i[new create edit update]

authorize_resource
load_and_authorize_resource

def index
@tasks = Task.all
Expand All @@ -14,7 +14,7 @@ def new
end

def create
@task = Task.new(tasks_params)
@task = Task.new(task_params)
@task.author = current_user

if @task.save
Expand All @@ -24,9 +24,20 @@ def create
end
end

def edit
end

def update
if task.update(task_params)
redirect_to task, notice: 'Задание обновлено'
else
render :edit
end
end

private

def tasks_params
def task_params
params.require(:task).permit(:title, :body, :price)
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def guest_abilities
def customer_abilities
guest_abilities

can :manage, [Task]
can :create, [Task]
can :update, [Task], author_id: user.id
end

def executor_abilities
Expand Down
2 changes: 1 addition & 1 deletion app/views/tasks/_task.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ article class="blog_item task task-#{task.id}"
.blog_details
=link_to task_path(task), class: "d-inline-block"
h2=task.title
p=task.price
p #{task.price} руб.
p=task.body
ul.blog-info-link
li
Expand Down
11 changes: 11 additions & 0 deletions app/views/tasks/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
section.hero-banner-sm
.container
.hero-banner-sm-content
h1 Редактирование задания
p Старайтесь описать как можно подробнее ваше задание

.container
.row
.col-md-9
.task
=render 'form'
5 changes: 4 additions & 1 deletion app/views/tasks/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ section.blog_area.single-post-area.section-margin
.single-post.task
.blog_details
h2=task.title
h3 #{task.price} рублей
h3 #{task.price} руб.
p=task.body
ul.blog-info-link
li
Expand All @@ -16,3 +16,6 @@ section.blog_area.single-post-area.section-margin
// a href="qwerty"
// i.far.fa-comments
// | NUMBER COMMENTS
.col-md-3
-if current_user&.author_of?(task) && can?(:update, Task)
=link_to 'Изменить задание', edit_task_path, class: 'button button-hero'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
devise_for :users, controllers: { registrations: 'users/registrations' }
root to: 'tasks#index'

resources :tasks, only: %i[index show new create]
resources :tasks, except: %i[destroy]
end
67 changes: 34 additions & 33 deletions spec/features/task/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
require 'rails_helper'

feature 'Create task' do
describe 'Authenticated' do
given(:customer) { create :user }
given(:executor) { create :user, :executor }
scenario 'Unauthenticated user can not create task' do
visit tasks_path

describe 'Customer' do
background do
sign_in(customer)
visit tasks_path
click_on 'Создать задание'
end
expect(page).to_not have_content 'Создать задание'
end

scenario 'create valid task' do
fill_in 'Название задания', with: 'Тест заголовка'
fill_in 'Что нужно сделать?', with: 'Тестовое наполнение'
fill_in 'Цена', with: '777'
click_on 'Опубликовать задание'
describe 'Executor' do
given(:executor) { create :user, :executor }

expect(page).to have_content 'Задание создано'
expect(page).to have_content 'Тест заголовка'
expect(page).to have_content 'Тестовое наполнение'
scenario 'can not create task' do
sign_in(executor)
visit tasks_path

within('ul.blog-info-link') do
expect(page).to have_content customer.email
end
end
expect(page).to_not have_content 'Создать задание'
end
end

scenario 'create not valid task' do
click_on 'Опубликовать задание'
describe 'Customer' do
given(:customer) { create :user }

expect(page).to have_content 'Заголовок не может быть пустым'
end
background do
sign_in(customer)
visit tasks_path
click_on 'Создать задание'
end

scenario 'executor tries create task' do
sign_in(executor)
visit tasks_path
scenario 'create valid task' do
fill_in 'Название задания', with: 'Тест заголовка'
fill_in 'Что нужно сделать?', with: 'Тестовое наполнение'
fill_in 'Цена', with: '777'
click_on 'Опубликовать задание'

expect(page).to_not have_content 'Создать задание'
expect(page).to have_content 'Задание создано'
expect(page).to have_content 'Тест заголовка'
expect(page).to have_content 'Тестовое наполнение'

within 'ul.blog-info-link' do
expect(page).to have_content customer.email
end
end
end

scenario 'Unauthenticated user tries create task' do
visit tasks_path
scenario 'can not create not valid task' do
click_on 'Опубликовать задание'

expect(page).to_not have_content 'Создать задание'
expect(page).to have_content 'Заголовок не может быть пустым'
end
end
end
67 changes: 67 additions & 0 deletions spec/features/task/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'rails_helper'

feature 'Update task' do
given!(:customer) { create :user }
given(:task) { create :task, author: customer }

scenario 'Unauthenticated user can not edit task' do
visit task_path(task)

expect(page).to_not have_content 'Изменить задание'
end

describe 'Executor' do
given(:executor) { create :user, :executor }

scenario 'Executor tries update task' do
sign_in(executor)
visit task_path(task)

expect(page).to_not have_content 'Изменить задание'
end
end

describe 'Customer' do
describe 'As author' do
background do
sign_in(customer)
visit task_path(task)

click_on 'Изменить задание'
end

scenario 'can update task' do
fill_in 'Название задания', with: 'Измененный заголовок'
fill_in 'Что нужно сделать?', with: 'Измененное тело'
fill_in 'Цена', with: '14500541'
click_on 'Опубликовать задание'

expect(page).to have_content 'Измененный заголовок'
expect(page).to have_content 'Измененное тело'
expect(page).to have_content '14500541'

expect(page).to have_content 'Задание обновлено'
end

scenario 'can not update not valid task' do
fill_in 'Название задания', with: ''
fill_in 'Что нужно сделать?', with: ''
fill_in 'Цена', with: ''
click_on 'Опубликовать задание'

expect(page).to have_content 'Заголовок не может быть пустым'
end
end

describe 'As not author' do
given!(:customer2) { create :user }

scenario 'can not update task' do
sign_in(customer2)
visit task_path(task)

expect(page).to_not have_content 'Изменить задание'
end
end
end
end
8 changes: 7 additions & 1 deletion spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

describe 'for customer' do
let(:user) { create :user }
let(:user2) { create :user }
let(:task) { create :task, author: user }
let(:task2) { create :task, author: user2 }

it { should be_able_to :manage, Task }
it { should be_able_to :create, Task }

it { should be_able_to :update, task }
it { should_not be_able_to :update, task2 }
end

describe 'for executor' do
Expand Down

0 comments on commit c29a0fc

Please sign in to comment.