Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/requirements assignments #167

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
class AssignmentsController < ApplicationController
before_action :set_assignment, only: %i[show update destroy]

# GET /assignments
# GET /assignments.json
def index
@assignments = Assignment.all
project = Project.find(requirements_filter[:project_id])
statement_of_works_ids = project.statement_of_works.map(&:id)
requirements = Requirement.where(statement_of_work_id: statement_of_works_ids).active_in_period(
requirements_filter[:start_date], requirements_filter[:end_date]
)

@assignments = Assignment.where(requirement: requirements)
end

# GET /assignments/1
# GET /assignments/1.json
def show; end

# POST /assignments
# POST /assignments.json
def create
@assignment = Assignment.new(assignment_params)

Expand All @@ -25,8 +25,6 @@ def create
end
end

# PATCH/PUT /assignments/1
# PATCH/PUT /assignments/1.json
def update
if @assignment.update(assignment_params)
render :show, status: :ok, location: @assignment
Expand All @@ -35,21 +33,21 @@ def update
end
end

# DELETE /assignments/1
# DELETE /assignments/1.json
def destroy
@assignment.destroy
end

private

# Use callbacks to share common setup or constraints between actions.
def set_assignment
@assignment = Assignment.find(params[:id])
end

# Only allow a list of trusted parameters through.
def assignment_params
params.require(:assignment).permit(:coverage, :requirement_id)
params.require(:assignment).permit(:coverage, :requirement_id, :start_date, :end_date, :user_id)
end

def requirements_filter
params.permit(:project_id, :start_date, :end_date)
end
end
26 changes: 12 additions & 14 deletions app/controllers/requirements_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

class RequirementsController < ApplicationController
before_action :set_project
before_action :set_statement_of_work
before_action :set_requirement, only: %i[show update destroy]

def index
@requirements = @statement_of_work.requirements
project = Project.find(requirements_filter[:project_id])
statement_of_works_ids = project.statement_of_works.map(&:id)
@requirements = Requirement.where(statement_of_work_id: statement_of_works_ids).active_in_period(
requirements_filter[:start_date], requirements_filter[:end_date]
)
end

def show; end

def create
@requirement = @statement_of_work.requirements.new(requirement_params)
@requirement = Requirement.new(requirement_params)

if @requirement.save
render :show, status: :created
Expand All @@ -35,19 +37,15 @@ def destroy

private

def set_project
@project = Project.find(params[:project_id])
end

def set_statement_of_work
@statement_of_work = @project.statement_of_works.find(params[:statement_of_work_id])
end

def set_requirement
@requirement = @statement_of_work.requirements.find(params[:id])
@requirement = Requirement.find(params[:id])
end

def requirement_params
params.require(:requirement).permit(:profession_id, :coverage)
params.require(:requirement).permit(:profession_id, :coverage, :statement_of_work_id, :start_date, :end_date)
end

def requirements_filter
params.permit(:project_id, :start_date, :end_date)
end
end
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

mount Sidekiq::Web => '/sidekiq'

resources :requirements
resources :projects do
resources :statement_of_works do
resources :requirements
end
resources :statement_of_works
end

namespace :analytics do
Expand Down
90 changes: 24 additions & 66 deletions spec/controllers/assignments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,33 @@

require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
#
# Also compared to earlier versions of this generator, there are no longer any
# expectations of assigns and templates rendered. These features have been
# removed from Rails core in Rails 5, but can be added back in via the
# `rails-controller-testing` gem.

RSpec.describe AssignmentsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Assignment. As you add validations to Assignment, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
include_context 'authentication'
render_views

let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
let(:valid_attributes) do
user = create(:user)
requirement = create(:requirement)
attributes_for(:assignment, user_id: user.id, requirement_id: requirement.id)
end

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# AssignmentsController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe 'GET #index' do
it 'returns a success response' do
Assignment.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_successful
requirement = create(:requirement)
project = requirement.statement_of_work.project

create_list(:assignment, 2, coverage: 0.5, requirement:)
get :index,
params: { start_date: Time.zone.today - 6.days, end_date: Time.zone.today + 6.days, project_id: project.id }

expect(response.parsed_body.size).to eq(2)
end
end

describe 'GET #show' do
it 'returns a success response' do
assignment = Assignment.create! valid_attributes
get :show, params: { id: assignment.to_param }, session: valid_session
get :show, params: { id: assignment.to_param }
expect(response).to be_successful
end
end
Expand All @@ -62,54 +37,37 @@
context 'with valid params' do
it 'creates a new Assignment' do
expect do
post :create, params: { assignment: valid_attributes }, session: valid_session
post :create, params: { assignment: valid_attributes }
end.to change(Assignment, :count).by(1)
end

it 'renders a JSON response with the new assignment' do
post :create, params: { assignment: valid_attributes }, session: valid_session
post :create, params: { assignment: valid_attributes }
expect(response).to have_http_status(:created)
expect(response.content_type).to eq('application/json')
expect(response.location).to eq(assignment_url(Assignment.last))
end
end

context 'with invalid params' do
it 'renders a JSON response with errors for the new assignment' do
post :create, params: { assignment: invalid_attributes }, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq('application/json')
end
end
end

describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
{
coverage: 1
}
end

it 'updates the requested assignment' do
assignment = Assignment.create! valid_attributes
put :update, params: { id: assignment.to_param, assignment: new_attributes }, session: valid_session
put :update, params: { id: assignment.to_param, assignment: new_attributes }
assignment.reload
skip('Add assertions for updated state')
expect(assignment.coverage).to eq(1.0)
end

it 'renders a JSON response with the assignment' do
assignment = Assignment.create! valid_attributes
put :update, params: { id: assignment.to_param, assignment: new_attributes }, session: valid_session
put :update, params: { id: assignment.to_param, assignment: new_attributes }
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/json')
end
end

context 'with invalid params' do
it 'renders a JSON response with errors for the assignment' do
assignment = Assignment.create! valid_attributes
put :update, params: { id: assignment.to_param, assignment: invalid_attributes }, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq('application/json')
end
end
end
Expand All @@ -118,7 +76,7 @@
it 'destroys the requested assignment' do
assignment = Assignment.create! valid_attributes
expect do
delete :destroy, params: { id: assignment.to_param }, session: valid_session
delete :destroy, params: { id: assignment.to_param }
end.to change(Assignment, :count).by(-1)
end
end
Expand Down
51 changes: 28 additions & 23 deletions spec/controllers/requirements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
render_views

let(:project) { create(:project) }

let(:statement_of_work) { create(:statement_of_work, :with_maintenance, project:) }

let(:valid_attributes) do
Expand All @@ -15,44 +16,47 @@
profession_id: profession.id)
end

xdescribe 'GET #index' do
it 'returns a success response' do
create(:requirement, statement_of_work:)
get :index, params: { project_id: project.id, statement_of_work_id: statement_of_work.id }
expect(response).to be_successful
describe 'GET #index' do
context 'when there are requirements in the period' do
it 'returns the requirements' do
create_list(:requirement, 2, statement_of_work:)
get :index,
params: { start_date: Time.zone.today - 6.days, end_date: Time.zone.today + 6.days, project_id: project.id }

expect(response.parsed_body.size).to eq(2)
end
end
end

xdescribe 'GET #show' do
describe 'GET #show' do
it 'returns a success response' do
requirement = create(:requirement, statement_of_work:)
get :show,
params: { project_id: project.id, statement_of_work_id: statement_of_work.id,
id: requirement.to_param }
params: { id: requirement.to_param }
expect(response).to be_successful
end
end

xdescribe 'POST #create' do
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Requirement' do
expect do
post :create,
params: { project_id: project.id, statement_of_work_id: statement_of_work.id,
params: { statement_of_work_id: statement_of_work.id,
requirement: valid_attributes }
end.to change(Requirement, :count).by(1)
end

it 'renders a JSON response with the new requirement' do
post :create,
params: { project_id: project.id, statement_of_work_id: statement_of_work.id,
params: { statement_of_work_id: statement_of_work.id,
requirement: valid_attributes }
expect(response).to have_http_status(:created)
end
end
end

xdescribe 'PUT #update' do
describe 'PUT #update' do
context 'with valid params' do
let(:profession) { create(:profession) }

Expand All @@ -65,33 +69,34 @@
it 'updates the requested requirement' do
requirement = create(:requirement, statement_of_work:)
put :update,
params: { project_id: project.id,
statement_of_work_id: statement_of_work.id,
id: requirement.to_param,
requirement: new_attributes }
params: {

id: requirement.to_param,
requirement: new_attributes
}
requirement.reload
expect(requirement.profession).to eq(profession)
end

it 'renders a JSON response with the requirement' do
requirement = create(:requirement, statement_of_work:)
put :update,
params: { project_id: project.id,
statement_of_work_id: statement_of_work.id,
id: requirement.to_param,
requirement: new_attributes }
params: {

id: requirement.to_param,
requirement: new_attributes
}
expect(response).to have_http_status(:ok)
end
end
end

xdescribe 'DELETE #destroy' do
describe 'DELETE #destroy' do
it 'destroys the requested requirement' do
requirement = create(:requirement, statement_of_work:)
expect do
delete :destroy,
params: { project_id: project.id, statement_of_work_id: statement_of_work.id,
id: requirement.to_param }
params: { id: requirement.to_param }
end.to change(Requirement, :count).by(-1)
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/factories/assignments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
#
FactoryBot.define do
factory :assignment do
coverage { 1.5 }
requirement { nil }
coverage { 1.0 }
requirement
user
start_date { Time.zone.today - 1.month }
end_date { Time.zone.today + 1.month }
end
end
Loading
Loading