Skip to content

Commit

Permalink
Add backend CompanyTechnology model and Technology model
Browse files Browse the repository at this point in the history
  • Loading branch information
GittavanderPol committed Aug 17, 2024
1 parent ad98b42 commit 53fbcb3
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class CompaniesController < InertiaController
include Pagy::Backend

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

paginate(pagy)
end

def show
company = Company.find(params[:id])
company = Company.includes(:technologies).find(params[:id])
@company = CompanySerializer.one(company)
end
end
3 changes: 3 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# typed: strict

class Company < ApplicationRecord
has_many :company_technologies, dependent: :destroy
has_many :technologies, through: :company_technologies

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
9 changes: 9 additions & 0 deletions app/models/company_technology.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class CompanyTechnology < ApplicationRecord
belongs_to :company
belongs_to :technology

validates :company_id, uniqueness: { scope: :technology_id }
validates :technology_id, uniqueness: { scope: :company_id }
end
14 changes: 14 additions & 0 deletions app/models/technology.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class Technology < ApplicationRecord
has_many :company_technologies, dependent: :destroy
has_many :companies, through: :company_technologies

validates :name, presence: true, uniqueness: true
validates :background_color,
format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, message: "must be a valid hex color code" },
allow_blank: true
validates :text_color,
format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, message: "must be a valid hex color code" },
allow_blank: true
end
2 changes: 2 additions & 0 deletions app/serializers/company_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class CompanySerializer < ApplicationSerializer
:website,
:careers_page,
:description

has_many :technologies, serializer: TechnologySerializer
end
8 changes: 8 additions & 0 deletions app/serializers/technology_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

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

class CreateTechnologies < ActiveRecord::Migration[7.1]
def change
create_table(:technologies) do |t|
t.string(:name, null: false, index: { unique: true })
t.string(:background_color)
t.string(:text_color)

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

class CreateCompanyTechnologies < ActiveRecord::Migration[7.1]
def change
create_table(:company_technologies) do |t|
t.references(:company, null: false, foreign_key: true)
t.references(:technology, null: false, foreign_key: true)
end

add_index(:company_technologies, [:company_id, :technology_id], unique: true)
end
end
22 changes: 21 additions & 1 deletion db/schema.rb

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

26 changes: 23 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
class Seeder
class << self
def perform
destroy_data!
seed_technologies
seed_companies
seed_company_technologies
end

def seed_companies
def destroy_data!
CompanyTechnology.destroy_all
Technology.destroy_all
Company.destroy_all
end

def seed_technologies
@ruby = Technology.create!(name: "Ruby", background_color: "#cc0000", text_color: "#ffffff")
@rails = Technology.create!(name: "Ruby on Rails", background_color: "#cc0000", text_color: "#ffffff")
end

Company.create!(name: "Pixelhub", website: "https://pixelhub.nl")
Company.create!(
def seed_companies
@company_1 = Company.create!(name: "Pixelhub", website: "https://pixelhub.nl", technologies: [@ruby, @rails])
@company_2 = Company.create!(
name: "AvoHQ",
website: "https://avohq.io",
description: "Avo is a very custom Admin Panel Framework, Content Management System,
and Internal Tool Builder for Ruby on Rails that saves engineers and teams months of development time.",
technologies: [@ruby, @rails],
)

create_records(Company, 48) do |i|
Expand All @@ -31,6 +44,13 @@ def seed_companies
end
end

def seed_company_technologies
CompanyTechnology.create!(company: @company_1, technology: @ruby)
CompanyTechnology.create!(company: @company_1, technology: @rails)
CompanyTechnology.create!(company: @company_2, technology: @ruby)
CompanyTechnology.create!(company: @company_2, technology: @rails)
end

private

def create_records(model, count, &block)
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/technologies_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :technology do
sequence(:name) { |i| "Technology #{i}" }
end
end
35 changes: 35 additions & 0 deletions spec/models/technology_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe(Technology, type: :model) do
require_factories
describe "attributes" do
it "accepts all valid attributes" do
technology = Technology.new(
name: "Ruby",
background_color: "#FF0000",
text_color: "#FFFFFF",
)
expect(technology).to(be_valid)
end

it "throws an error if name is not present" do
technology = build(:technology, name: nil)
expect(technology).to_not(be_valid)
expect(technology.errors[:name]).to(be_present)
end

it "throws an error if background_color is not a valid hex color code" do
technology = build(:technology, background_color: "invalid")
expect(technology).to_not(be_valid)
expect(technology.errors[:background_color]).to(be_present)
end

it "throws an error if text_color is not a valid hex color code" do
technology = build(:technology, text_color: "invalid")
expect(technology).to_not(be_valid)
expect(technology.errors[:text_color]).to(be_present)
end
end
end

0 comments on commit 53fbcb3

Please sign in to comment.