Skip to content

companies creation, edition and listing #2

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

Merged
merged 1 commit into from
Dec 15, 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
40 changes: 40 additions & 0 deletions app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class CompaniesController < ApplicationController
before_action :load_company, only: [:edit, :update]

def index
@companies = Company.all.page(params[:page] || 1).per(10)
end

def new
@company = Company.new
end

def edit; end

def create
if Company.create(company_attributes)
redirect_to companies_path, notice: 'Successfully created entry'
else
render :create, alert: 'Unsuccessfully created entry'
end
end

def update
if @company.update(company_attributes)
redirect_to companies_path, notice: 'Successfully updated entry'
else
render :edit, alert: 'Unsuccessfully created entry'
end
end

private

def company_attributes
params.require(:company).permit(:name)
end

def load_company
@company = Company.find(params[:id])
end
end

8 changes: 8 additions & 0 deletions app/views/companies/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h2 Creating an entry

= form_for @company, class: 'row' do |f|
.col-auto
= f.label :name, class: 'form-label'
= f.text_field :name, class: 'form-control'
.col-auto.mt-4
= f.submit class: 'btn btn-primary'
25 changes: 25 additions & 0 deletions app/views/companies/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
h2 Viewing companies

table.table
thead
tr
th[scope="col"] ID
th[scope="col"] Name
th[scope="col"] Created At
th[scope="col"] Actions
tbody
- @companies.each do |company|
tr
th[scope="row"]= company&.id
td= company.try(:name)
td= company.try(:created_at).strftime("%m/%d/%Y")
td= link_to "Edit", edit_company_path(company), class: 'btn btn-primary'

.row.justify-content-between
.col-4
= paginate @companies, theme: 'bootstrap3'
= page_entries_info @companies
.col-4
= link_to "New Company", new_company_path, class: 'btn btn-success'


8 changes: 8 additions & 0 deletions app/views/companies/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h2 Creating an entry

= form_for @company, class: 'row' do |f|
.col-auto
= f.label :name, class: 'form-label'
= f.text_field :name, class: 'form-control'
.col-auto.mt-4
= f.submit class: 'btn btn-primary'
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ html[lang="en"]
a.nav-link[aria-current="page" href="/"]All
li.nav-item
= link_to('Create', new_person_path, class: 'nav-link')
li.nav-item
= link_to('Companies', companies_path, class: 'nav-link')
form.d-flex
input.form-control.me-2[type="search" placeholder="Search" aria-label="Search"]
button.btn.btn-outline-success[type="submit"] Search
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do

resources :people, only: [:index, :new, :create]
resources :companies
root to: 'people#index'
end
102 changes: 102 additions & 0 deletions spec/controllers/companies_controller.spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'rails_helper'

RSpec.describe CompaniesController, type: :controller do
let!(:company) { FactoryBot.create(:company, name: "Test Company") }

describe "GET #index" do
it "assigns @companies with paginated results" do
get :index, params: { page: 1 }
expect(assigns(:companies)).to be_a_kind_of(ActiveRecord::Relation)
expect(assigns(:companies)).to include(company)
end

it "renders the index template" do
get :index
expect(response).to render_template(:index)
end
end

describe "GET #new" do
it "assigns a new Company to @company" do
get :new
expect(assigns(:company)).to be_a_new(Company)
end

it "renders the new template" do
get :new
expect(response).to render_template(:new)
end
end

describe "GET #edit" do
it "assigns the requested company to @company" do
get :edit, params: { id: company.id }
expect(assigns(:company)).to eq(company)
end

it "renders the edit template" do
get :edit, params: { id: company.id }
expect(response).to render_template(:edit)
end
end

describe "POST #create" do
context "with valid attributes" do
it "creates a new company" do
expect {
post :create, params: { company: { name: "New Company" } }
}.to change(Company, :count).by(1)
end

it "redirects to the index with a success notice" do
post :create, params: { company: { name: "New Company" } }
expect(response).to redirect_to(companies_path)
expect(flash[:notice]).to eq("Successfully created entry")
end
end

context "with invalid attributes" do
it "does not create a new company" do
expect {
post :create, params: { company: { name: "" } }
}.not_to change(Company, :count)
end

it "renders the create template with an alert" do
post :create, params: { company: { name: "" } }
expect(response).to render_template(:create)
expect(flash[:alert]).to eq("Unsuccessfully created entry")
end
end
end

describe "PATCH #update" do
context "with valid attributes" do
it "updates the company" do
patch :update, params: { id: company.id, company: { name: "Updated Company" } }
company.reload
expect(company.name).to eq("Updated Company")
end

it "redirects to the index with a success notice" do
patch :update, params: { id: company.id, company: { name: "Updated Company" } }
expect(response).to redirect_to(companies_path)
expect(flash[:notice]).to eq("Successfully updated entry")
end
end

context "with invalid attributes" do
it "does not update the company" do
patch :update, params: { id: company.id, company: { name: "" } }
company.reload
expect(company.name).to eq("Test Company")
end

it "renders the edit template with an alert" do
patch :update, params: { id: company.id, company: { name: "" } }
expect(response).to render_template(:edit)
expect(flash[:alert]).to eq("Unsuccessfully created entry")
end
end
end
end
36 changes: 36 additions & 0 deletions spec/views/companies/index.html.slim_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "rails_helper"

describe "companies/index.html.slim" do
let!(:company1) { FactoryBot.create(:company, name: "Company One", created_at: Time.zone.parse("2023-01-01")) }
let!(:company2) { FactoryBot.create(:company, name: "Company Two", created_at: Time.zone.parse("2023-01-02")) }

it "displays the companies in a table" do
assign(:companies, Kaminari.paginate_array([company1, company2]).page(1).per(10))

render

# Check table headers
expect(rendered).to have_selector("table.table thead tr th", text: "ID")
expect(rendered).to have_selector("table.table thead tr th", text: "Name")
expect(rendered).to have_selector("table.table thead tr th", text: "Created At")
expect(rendered).to have_selector("table.table thead tr th", text: "Actions")

# Check rows for companies
expect(rendered).to have_selector("table.table tbody tr th", text: company1.id.to_s)
expect(rendered).to have_selector("table.table tbody tr td", text: company1.name)
expect(rendered).to have_selector("table.table tbody tr td", text: company1.created_at.strftime("%m/%d/%Y"))
expect(rendered).to have_link("Edit", href: edit_company_path(company1))

expect(rendered).to have_selector("table.table tbody tr th", text: company2.id.to_s)
expect(rendered).to have_selector("table.table tbody tr td", text: company2.name)
expect(rendered).to have_selector("table.table tbody tr td", text: company2.created_at.strftime("%m/%d/%Y"))
expect(rendered).to have_link("Edit", href: edit_company_path(company2))
end

it "displays a link to create a new company" do
assign(:companies, Kaminari.paginate_array([company1, company2]).page(1).per(10))

render
expect(rendered).to have_link("New Company", href: new_company_path)
end
end