Skip to content

feature to add company to a person #3

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
8 changes: 5 additions & 3 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ def index

def new
@person = Person.new
@companies = Company.all.order(:name)
end

def create
@person = Person.new(person_attributes)

if @person.save
redirect_to people_path, notice: 'Successfully created entry'
redirect_to people_path, notice: 'Person successfully created'
else
render :new, status: :unprocessable_entity
@companies = Company.page(params[:page] || 1).per(10)
render :new
end
end

private

def person_attributes
params.require(:person).permit(:name, :email, :phone_number)
params.require(:person).permit(:name, :email, :phone_number, :company_id)
end
end

7 changes: 7 additions & 0 deletions app/views/people/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
.col-auto
= f.label :name, class: 'form-label'
= f.text_field :name, class: 'form-control'

.col-auto
= f.label :phone_number, class: 'form-label'
= f.text_field :phone_number, class: 'form-control'

.col-auto
= f.label :email, class: 'form-label'
= f.text_field :email, class: 'form-control'

.col-auto
= f.label :company_id, 'Select a Company', class: 'form-label'
= f.collection_select :company_id, @companies, :id, :name, prompt: 'Choose a company', class: 'form-select'

.col-auto.mt-4
= f.submit person.new_record? ? 'Create Person' : 'Update Person', class: 'btn btn-primary'
30 changes: 26 additions & 4 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,34 @@ def create_people_with_companies(count)
end

describe 'POST create' do
it 'Creates a record' do
expect{ post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo' } } }.to change{ Person.count }.by(1)
let!(:company) { FactoryBot.create(:company) }

context 'with valid attributes' do
it 'creates a record' do
expect {
post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo@example.com', company_id: company.id } }
}.to change { Person.count }.by(1)
end

it 'associates the person with the correct company' do
post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo@example.com', company_id: company.id } }
person = Person.last
expect(person.company).to eq(company)
end

it 'redirects to the index with a success notice' do
post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo@example.com', company_id: company.id } }
expect(response).to redirect_to(people_path)
expect(flash[:notice]).to eq('Person successfully created')
end
end

it 'has status found' do
expect(post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo' } }).to have_http_status(:found)
context 'with invalid attributes' do
it 'does not create a record' do
expect {
post :create, params: { person: { name: '', phone_number: '', email: '', company_id: nil } }
}.not_to change { Person.count }
end
end
end
end