Skip to content

Commit

Permalink
create time entries controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Mar 8, 2024
1 parent 55e4fb1 commit 97d013f
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
55 changes: 55 additions & 0 deletions app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

class TimeEntriesController < ApplicationController
before_action :set_time_entry, only: %i[show update destroy]

# GET /time_entries
# GET /time_entries.json
def index
@time_entries = TimeEntry.all
end

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

# POST /time_entries
# POST /time_entries.json
def create
@time_entry = TimeEntry.new(time_entry_params)

if @time_entry.save
render :show, status: :created, location: @time_entry
else
render json: @time_entry.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /time_entries/1
# PATCH/PUT /time_entries/1.json
def update
if @time_entry.update(time_entry_params)
render :show, status: :ok, location: @time_entry
else
render json: @time_entry.errors, status: :unprocessable_entity
end
end

# DELETE /time_entries/1
# DELETE /time_entries/1.json
def destroy
@time_entry.destroy
end

private

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

# Only allow a list of trusted parameters through.
def time_entry_params
params.fetch(:time_entry, {})
end
end
1 change: 0 additions & 1 deletion app/views/time_entries/_time_entry.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true

json.extract! time_entry, :id, :date, :hours, :user_id, :statement_of_work_id, :created_at, :updated_at
json.url time_entry_url(time_entry, format: :json)
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'sidekiq/cron/web'

Rails.application.routes.draw do
resources :time_entries
resources :payments
resources :assignments
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
Expand Down
125 changes: 125 additions & 0 deletions spec/controllers/time_entries_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true

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 TimeEntriesController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# TimeEntry. As you add validations to TimeEntry, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end

let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
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
# TimeEntriesController. Be sure to keep this updated too.
let(:valid_session) { {} }

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

describe 'GET #show' do
it 'returns a success response' do
time_entry = TimeEntry.create! valid_attributes
get :show, params: { id: time_entry.to_param }, session: valid_session
expect(response).to be_successful
end
end

describe 'POST #create' do
context 'with valid params' do
it 'creates a new TimeEntry' do
expect do
post :create, params: { time_entry: valid_attributes }, session: valid_session
end.to change(TimeEntry, :count).by(1)
end

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

context 'with invalid params' do
it 'renders a JSON response with errors for the new time_entry' do
post :create, params: { time_entry: 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')
end

it 'updates the requested time_entry' do
time_entry = TimeEntry.create! valid_attributes
put :update, params: { id: time_entry.to_param, time_entry: new_attributes }, session: valid_session
time_entry.reload
skip('Add assertions for updated state')
end

it 'renders a JSON response with the time_entry' do
time_entry = TimeEntry.create! valid_attributes
put :update, params: { id: time_entry.to_param, time_entry: new_attributes }, session: valid_session
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 time_entry' do
time_entry = TimeEntry.create! valid_attributes
put :update, params: { id: time_entry.to_param, time_entry: 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 'DELETE #destroy' do
it 'destroys the requested time_entry' do
time_entry = TimeEntry.create! valid_attributes
expect do
delete :destroy, params: { id: time_entry.to_param }, session: valid_session
end.to change(TimeEntry, :count).by(-1)
end
end
end

0 comments on commit 97d013f

Please sign in to comment.