-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backend
CompanyTechnology
model and Technology
model
- Loading branch information
1 parent
ad98b42
commit 53fbcb3
Showing
12 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |