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

[V2] Add normalised location to Company resource #51

Merged
merged 1 commit into from
Sep 8, 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
10 changes: 8 additions & 2 deletions app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ class CompaniesController < InertiaController
include Pagy::Backend

def index
pagy, companies = pagy(Company.includes(:technologies).all)
pagy, companies = pagy(company_scope.all)
@companies = CompanySerializer.many(companies)

paginate(pagy)
end

def show
company = Company.includes(:technologies).find(params[:id])
company = company_scope.find(params[:id])
@company = CompanySerializer.one(company)
end

private

def company_scope
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Company.includes(:technologies, :continent, :country, :region, :city)
end
end
4 changes: 4 additions & 0 deletions app/frontend/pages/companies/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function Index({ companies, pagination }: Props) {
</Link>
),
},
{
accessorKey: "address.short",
header: "Location",
},
{
accessorKey: "website",
header: "Website",
Expand Down
43 changes: 29 additions & 14 deletions app/frontend/pages/companies/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ interface Props {
}

function Show({ company }: Props) {
const technologies = company.technologies.map((technology) => (
<Badge
key={technology.id}
style={{
backgroundColor: technology.backgroundColor,
color: technology.textColor,
}}
>
{technology.name}
</Badge>
));

return (
<div className="container mx-auto py-10">
<h1 className="my-4 text-xl font-bold">{company.name}</h1>
<div className="flex space-x-4">
<div className="flex space-x-4 my-8">
{company.website && (
<ExternalLink href={company.website}>Website</ExternalLink>
)}
Expand All @@ -24,19 +36,22 @@ function Show({ company }: Props) {
</ExternalLink>
)}
</div>
<div className="flex space-x-2">
{company.technologies.map((technology) => (
<Badge
key={technology.id}
style={{
backgroundColor: technology.backgroundColor,
color: technology.textColor,
}}
>
{technology.name}
</Badge>
))}
</div>
<dl className="grid grid-cols-1 gap-x-4 gap-y-8 sm:grid-cols-2">
<div className="sm:col-span-1">
<dt className="font-bold">Location</dt>
<dd className="mt-1">{company.address.medium}</dd>
</div>
<div className="sm:col-span-1">
<dt className="font-bold">Work Arrangements</dt>
<dd className="mt-1">
<span>Not specified</span>
</dd>
</div>
<div className="sm:col-span-2">
<dt className="font-bold">Stack</dt>
<dd className="mt-1 flex space-x-2">{technologies}</dd>
</div>
</dl>
<Separator className="my-4" />
<p>{company.description}</p>
</div>
Expand Down
10 changes: 10 additions & 0 deletions app/frontend/types/company.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GeoFragment } from "./geo_fragment";
import { Technology } from "./technology";

export interface Company {
Expand All @@ -7,4 +8,13 @@ export interface Company {
careersPage?: string;
description?: string;
technologies: Technology[];
address: {
short: string;
medium: string;
full: string;
};
continent: GeoFragment;
country: GeoFragment;
region: GeoFragment;
city: GeoFragment;
}
4 changes: 4 additions & 0 deletions app/frontend/types/geo_fragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface GeoFragment {
id: number;
name: string;
}
10 changes: 10 additions & 0 deletions app/models/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class City < ApplicationRecord
belongs_to :region
has_one :country, through: :region
has_one :continent, through: :country
has_many :companies

validates :name, presence: true
end
5 changes: 5 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class Company < ApplicationRecord
has_many :company_technologies, dependent: :destroy
has_many :technologies, through: :company_technologies

belongs_to :city
has_one :region, through: :city
has_one :country, through: :region
has_one :continent, through: :country

validates :name, presence: true
validates :website, url: { no_local: true, public_suffix: true }
validates :careers_page, url: { no_local: true, public_suffix: true }, allow_blank: true
Expand Down
10 changes: 10 additions & 0 deletions app/models/continent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Continent < ApplicationRecord
has_many :cities, through: :regions
has_many :regions, through: :countries
has_many :countries
has_many :companies, through: :cities

validates :name, presence: true
end
10 changes: 10 additions & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Country < ApplicationRecord
belongs_to :continent
has_many :regions
has_many :cities, through: :regions
has_many :companies, through: :cities

validates :name, presence: true
end
10 changes: 10 additions & 0 deletions app/models/region.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Region < ApplicationRecord
has_many :cities
belongs_to :country
has_one :continent, through: :country
has_many :companies, through: :cities

validates :name, presence: true
end
32 changes: 30 additions & 2 deletions app/serializers/company_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
# frozen_string_literal: true

class CompanySerializer < ApplicationSerializer
attributes :id,
attributes(
:id,
:name,
:website,
:careers_page,
:description
:description,
)

attribute :address do
{
short: short_address,
medium: medium_address,
full: full_address,
}
end

has_many :technologies, serializer: TechnologySerializer
has_one :continent, serializer: GeoFragmentSerializer
has_one :country, serializer: GeoFragmentSerializer
has_one :region, serializer: GeoFragmentSerializer
has_one :city, serializer: GeoFragmentSerializer

private

def short_address
[company.city&.name, company.country&.name].compact.join(", ")
end

def medium_address
[company.city&.name, company.region&.name, company.country&.name].compact.join(", ")
end

def full_address
[company.city&.name, company.region&.name, company.country&.name, company.continent&.name].compact.join(", ")
end
end
8 changes: 8 additions & 0 deletions app/serializers/geo_fragment_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class GeoFragmentSerializer < ApplicationSerializer
attributes(
:id,
:name,
)
end
6 changes: 4 additions & 2 deletions app/serializers/pagination_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

class PaginationSerializer < ApplicationSerializer
hash_attributes :scaffold_url,
hash_attributes(
:scaffold_url,
:page,
:pages,
:prev,
:next
:next,
)
end
6 changes: 4 additions & 2 deletions app/serializers/technology_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

class TechnologySerializer < ApplicationSerializer
attributes :id,
attributes(
:id,
:name,
:background_color,
:text_color
:text_color,
)
end
11 changes: 11 additions & 0 deletions db/migrate/20240823144658_create_continents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class CreateContinents < ActiveRecord::Migration[7.2]
def change
create_table(:continents) do |t|
t.string(:name, null: false)

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240823144705_create_countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCountries < ActiveRecord::Migration[7.2]
def change
create_table(:countries) do |t|
t.string(:name, null: false)
t.references(:continent, null: false, foreign_key: true)

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240823144711_create_regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateRegions < ActiveRecord::Migration[7.2]
def change
create_table(:regions) do |t|
t.string(:name, null: false)
t.references(:country, null: false, foreign_key: true)

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240823144715_create_cities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCities < ActiveRecord::Migration[7.2]
def change
create_table(:cities) do |t|
t.string(:name, null: false)
t.references(:region, null: false, foreign_key: true)

t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20240823144728_add_city_to_companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddCityToCompanies < ActiveRecord::Migration[7.2]
def change
add_reference(:companies, :city, null: false, foreign_key: true)
end
end
38 changes: 37 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading