Skip to content

Commit

Permalink
Add company model
Browse files Browse the repository at this point in the history
  • Loading branch information
GittavanderPol committed Feb 10, 2024
1 parent 6692c20 commit aff5ca7
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@

inherit_gem:
rubocop-shopify: rubocop.yml
AllCops:
Excludes:
- db/schema.rb
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ gem "rubocop", require: false

gem "rubocop-shopify", require: false

gem "ruby-lsp-rails", "~> 0.2.9", :group => :development
gem "ruby-lsp-rails", "~> 0.2.9", group: :development

gem "public_suffix", "~> 5.0"
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ DEPENDENCIES
importmap-rails
jbuilder
pg (~> 1.1)
public_suffix (~> 5.0)
puma (>= 5.0)
rails (~> 7.1.3)
redis (>= 4.0.1)
Expand Down
13 changes: 13 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Company < ApplicationRecord
validates :name, presence: true
validate :valid_url

def valid_url
return if website.nil?
return if PublicSuffix.valid?(website, default_rule: nil)

errors.add(:website, :valid_url)
end
end
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@
# enabled: "ON"

en:
hello: "Hello world"
errors:
messages:
valid_url: "must be a valid URL"
12 changes: 12 additions & 0 deletions db/migrate/20240210170336_create_companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCompanies < ActiveRecord::Migration[7.1]
def change
create_table(:companies) do |t|
t.string(:name, null: false)
t.string(:website)

t.timestamps
end
end
end
24 changes: 24 additions & 0 deletions db/schema.rb

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

5 changes: 5 additions & 0 deletions test/fixtures/companies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

pixelhub:
name: "Pixelhub"
website: "http://www.pixelhub.nl"
24 changes: 24 additions & 0 deletions test/models/company_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "test_helper"

class CompanyTest < ActiveSupport::TestCase
setup do
@company = companies(:pixelhub)
end

test "should not save company without name" do
@company.name = nil
assert_not @company.valid?
end

test "should not save company with invalid website" do
@company.website = "invalid"
assert_not @company.valid?
end

test "should save company without website" do
@company.website = nil
assert @company.valid?
end
end

0 comments on commit aff5ca7

Please sign in to comment.