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

Resolves N + 1 query #16

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
class PeopleController < ApplicationController

def index
@people = Person.all
@people = Person.includes(:company)
end

def new
@person = Person.new
end

def create
if Person.create(person_attributes)
@person = Person.new(person_attributes)
if @person.save
redirect_to people_path, notice: 'Successfully created entry'
else
render :create, alert: 'Unsuccessfully created entry'
render :new, alert: 'Unsuccessfully created entry', status: :unprocessable_entity
end
end

private

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

end
Expand Down
4 changes: 4 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
class Person < ApplicationRecord

belongs_to :company, optional: true

validates :name, presence: true
validates :phone_number, presence: true
validates :email, presence: true
end
2 changes: 1 addition & 1 deletion app/views/people/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ table.table
tr
th[scope="row"]= person&.id
td= person.try(:name)
td= person.try(:phone)
td= person.try(:phone_number)
td= person.try(:email)
td= person.try(:company).try(:name)

Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
it 'has status found' do
expect(post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo' } }).to have_http_status(:found)
end

it 'doesn\'t create a user when is missing phone_number' do
expect(post :create, params: { person: { name: 'foo', email: 'foo' } }).to have_http_status(:unprocessable_entity)
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :company do
name { "Company default" }
end
end
7 changes: 7 additions & 0 deletions spec/factories/people.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :person do
name { "John Doe" }
email { "john@doe.com" }
phone_number { "9770607060" }
end
end
3 changes: 2 additions & 1 deletion spec/features/people/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe 'Listing people', type: :feature do
before do
Person.create(
FactoryBot.create(
:person,
name: 'Foo Bar',
phone_number: 'Biz',
email: 'Baz'
Expand Down
14 changes: 13 additions & 1 deletion spec/views/people/index.html.slim_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
require "rails_helper"

describe "people/index.html.slim" do
it "Displays the users"
let(:company) { FactoryBot.create(:company, name: "Default Company") }
let(:first_person) { FactoryBot.create(:person, phone_number: "Foo", company: company) }
let(:second_person) { FactoryBot.create(:person, name: "Jane Doe", email: "jane@doe.com.br", phone_number: "Bar", company: company) }
it "Displays the users" do
assign(:people, [first_person, second_person])
render

expect(rendered).to match(/John/) # name (John)
expect(rendered).to match(/Foo/) # phone number
expect(rendered).to match(/Jane/) # name (Jane)
expect(rendered).to match(/Bar/) # phone number
expect(rendered).to match(/Default Company/) # company name
end
end