-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29bac0d
commit d6d6377
Showing
12 changed files
with
304 additions
and
1 deletion.
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class PaymentsController < ApplicationController | ||
before_action :set_payment, only: %i[show update destroy] | ||
|
||
# GET /payments | ||
# GET /payments.json | ||
def index | ||
@payments = Payment.all | ||
end | ||
|
||
# GET /payments/1 | ||
# GET /payments/1.json | ||
def show; end | ||
|
||
# POST /payments | ||
# POST /payments.json | ||
def create | ||
@payment = Payment.new(payment_params) | ||
|
||
if @payment.save | ||
render :show, status: :created, location: @payment | ||
else | ||
render json: @payment.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /payments/1 | ||
# PATCH/PUT /payments/1.json | ||
def update | ||
if @payment.update(payment_params) | ||
render :show, status: :ok, location: @payment | ||
else | ||
render json: @payment.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /payments/1 | ||
# DELETE /payments/1.json | ||
def destroy | ||
@payment.destroy | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_payment | ||
@payment = Payment.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def payment_params | ||
params.require(:payment).permit(:date, :amount, :statement_of_work_id) | ||
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: payments | ||
# | ||
# id :bigint not null, primary key | ||
# amount :float | ||
# date :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# statement_of_work_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_payments_on_statement_of_work_id (statement_of_work_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (statement_of_work_id => statement_of_works.id) | ||
# | ||
class Payment < ApplicationRecord | ||
belongs_to :statement_of_work | ||
|
||
validates :amount, presence: true | ||
validates :date, presence: true | ||
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,4 @@ | ||
# frozen_string_literal: true | ||
|
||
json.extract! payment, :id, :date, :amount, :statement_of_work_id, :created_at, :updated_at | ||
json.url payment_url(payment, format: :json) |
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,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.array! @payments, partial: 'payments/payment', as: :payment |
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,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'payments/payment', payment: @payment |
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 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreatePayments < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :payments do |t| | ||
t.datetime :date | ||
t.float :amount | ||
t.references :statement_of_work, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 PaymentsController, type: :controller do | ||
# This should return the minimal set of attributes required to create a valid | ||
# Payment. As you add validations to Payment, 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 | ||
# PaymentsController. Be sure to keep this updated too. | ||
let(:valid_session) { {} } | ||
|
||
describe 'GET #index' do | ||
it 'returns a success response' do | ||
Payment.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 | ||
payment = Payment.create! valid_attributes | ||
get :show, params: { id: payment.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 Payment' do | ||
expect do | ||
post :create, params: { payment: valid_attributes }, session: valid_session | ||
end.to change(Payment, :count).by(1) | ||
end | ||
|
||
it 'renders a JSON response with the new payment' do | ||
post :create, params: { payment: 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(payment_url(Payment.last)) | ||
end | ||
end | ||
|
||
context 'with invalid params' do | ||
it 'renders a JSON response with errors for the new payment' do | ||
post :create, params: { payment: 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 payment' do | ||
payment = Payment.create! valid_attributes | ||
put :update, params: { id: payment.to_param, payment: new_attributes }, session: valid_session | ||
payment.reload | ||
skip('Add assertions for updated state') | ||
end | ||
|
||
it 'renders a JSON response with the payment' do | ||
payment = Payment.create! valid_attributes | ||
put :update, params: { id: payment.to_param, payment: 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 payment' do | ||
payment = Payment.create! valid_attributes | ||
put :update, params: { id: payment.to_param, payment: 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 payment' do | ||
payment = Payment.create! valid_attributes | ||
expect do | ||
delete :destroy, params: { id: payment.to_param }, session: valid_session | ||
end.to change(Payment, :count).by(-1) | ||
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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: payments | ||
# | ||
# id :bigint not null, primary key | ||
# amount :float | ||
# date :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# statement_of_work_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_payments_on_statement_of_work_id (statement_of_work_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (statement_of_work_id => statement_of_works.id) | ||
# | ||
FactoryBot.define do | ||
factory :payment do | ||
date { '2023-11-20 17:33:52' } | ||
amount { 1.5 } | ||
statement_of_work { nil } | ||
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: payments | ||
# | ||
# id :bigint not null, primary key | ||
# amount :float | ||
# date :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# statement_of_work_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_payments_on_statement_of_work_id (statement_of_work_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (statement_of_work_id => statement_of_works.id) | ||
# | ||
require 'rails_helper' | ||
|
||
RSpec.describe Payment, type: :model do | ||
describe 'associations' do | ||
it { should belong_to(:statement_of_work) } | ||
end | ||
|
||
describe 'validations' do | ||
it { should validate_presence_of(:date) } | ||
it { should validate_presence_of(:amount) } | ||
end | ||
end |