Skip to content

Commit

Permalink
feat: Add customer_type to Customers (#2544)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomiguelpinto authored Sep 9, 2024
1 parent fa7c5ec commit 6159706
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
22 changes: 21 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ class Customer < ApplicationRecord
:finalize
].freeze

attribute :finalize_zero_amount_invoice, :integer # rails 7.1 check the field exists when defining enum and when running the migration first time is not there
CUSTOMER_TYPES = {
company: 'company',
individual: 'individual'
}.freeze

attribute :finalize_zero_amount_invoice, :integer
enum finalize_zero_amount_invoice: FINALIZE_ZERO_AMOUNT_INVOICE_OPTIONS, _prefix: :finalize_zero_amount_invoice
attribute :customer_type, :string
enum customer_type: CUSTOMER_TYPES, _prefix: :customer_type

before_save :ensure_slug

Expand Down Expand Up @@ -75,6 +82,16 @@ def self.ransackable_attributes(_auth_object = nil)
%w[id name external_id email]
end

def display_name
names = [legal_name.presence || name.presence]
if firstname.present? || lastname.present?
names << '-' if names.compact.present?
names << firstname
names << lastname
end
names.compact.join(' ')
end

def active_subscription
subscriptions.active.order(started_at: :desc).first
end
Expand Down Expand Up @@ -179,11 +196,14 @@ def ensure_slug
# city :string
# country :string
# currency :string
# customer_type :enum
# deleted_at :datetime
# document_locale :string
# email :string
# finalize_zero_amount_invoice :integer default("inherit"), not null
# firstname :string
# invoice_grace_period :integer
# lastname :string
# legal_name :string
# legal_number :string
# logo_url :string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddFirstnameAndLastnameToCustomers < ActiveRecord::Migration[7.1]
def change
safety_assured do
change_table :customers, bulk: true do |t|
t.string :firstname
t.string :lastname
end
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240906170048_add_customer_type_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AddCustomerTypeToCustomers < ActiveRecord::Migration[7.1]
def change
create_enum :customer_type, %w[company individual]

safety_assured do
change_table :customers do |t|
t.enum :customer_type, enum_type: 'customer_type', null: true
end
end
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

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

98 changes: 98 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,104 @@
end
end

describe '#display_name' do
subject(:customer) { build_stubbed(:customer, name:, legal_name:, firstname:, lastname:) }

let(:name) { 'ACME Inc' }
let(:legal_name) { 'ACME International Corporation' }
let(:firstname) { 'Thomas' }
let(:lastname) { 'Anderson' }

context 'when all fields are nil' do
let(:name) { nil }
let(:legal_name) { nil }
let(:firstname) { nil }
let(:lastname) { nil }

it 'returns an empty string' do
expect(customer.display_name).to eq('')
end
end

context 'when name and legal_name are nil' do
let(:name) { nil }
let(:legal_name) { nil }

it 'returns only firstname and lastname if present' do
expect(customer.display_name).to eq('Thomas Anderson')
end
end

context 'when firstname and lastname are nil' do
let(:firstname) { nil }
let(:lastname) { nil }

it 'returns only the legal_name' do
expect(customer.display_name).to eq('ACME International Corporation')
end

context 'when we dont have a legal_name' do
let(:legal_name) { nil }

it 'returns only the name if present' do
expect(customer.display_name).to eq('ACME Inc')
end
end
end

context 'when name is present and both firstname and lastname are present' do
let(:legal_name) { nil }

it 'returns name with firstname and lastname' do
expect(customer.display_name).to eq('ACME Inc - Thomas Anderson')
end
end

context 'when legal_name is present and both firstname and lastname are present' do
let(:name) { nil }

it 'returns legal_name with firstname and lastname' do
expect(customer.display_name).to eq('ACME International Corporation - Thomas Anderson')
end
end

context 'when all fields are present' do
it 'returns legal_name with firstname and lastname' do
expect(customer.display_name).to eq('ACME International Corporation - Thomas Anderson')
end
end
end

describe 'customer_type enum' do
subject(:customer) { build_stubbed(:customer, customer_type:) }

context 'when customer_type is company' do
let(:customer_type) { 'company' }

it 'identifies the customer as a company' do
expect(customer.customer_type).to eq('company')
expect(customer.customer_type_company?).to be true
end
end

context 'when customer_type is individual' do
let(:customer_type) { 'individual' }

it 'identifies the customer as an individual' do
expect(customer.customer_type).to eq('individual')
expect(customer.customer_type_individual?).to be true
end
end

context 'when customer_type is nil' do
subject(:customer) { build(:customer) }

it 'defaults to nil for existing customers' do
expect(customer.customer_type).to be_nil
end
end
end

describe 'preferred_document_locale' do
subject(:customer) do
described_class.new(
Expand Down

0 comments on commit 6159706

Please sign in to comment.