12 Added notes and colours to customers
This commit is contained in:
@@ -2,6 +2,8 @@ class Customer < ApplicationRecord
|
||||
# Use Rails 7.1's native composite primary key
|
||||
self.primary_key = %i[first_name surname original_phone]
|
||||
|
||||
attribute :color, :string, default: 'green'
|
||||
|
||||
belongs_to :company
|
||||
|
||||
validates :first_name, presence: true
|
||||
@@ -21,20 +23,65 @@ class Customer < ApplicationRecord
|
||||
less_than_or_equal_to: -> { Time.current.year }
|
||||
}, allow_nil: true
|
||||
|
||||
validates :color, inclusion: { in: %w[green yellow red] }, allow_nil: true
|
||||
|
||||
before_validation :set_original_phone, on: :create
|
||||
before_validation :set_default_color, on: :create
|
||||
|
||||
def full_name
|
||||
[first_name, surname].compact_blank.join(' ')
|
||||
end
|
||||
|
||||
def green?
|
||||
color == 'green'
|
||||
end
|
||||
|
||||
def yellow?
|
||||
color == 'yellow'
|
||||
end
|
||||
|
||||
def red?
|
||||
color == 'red'
|
||||
end
|
||||
|
||||
# Add method for URL generation
|
||||
def to_param
|
||||
[first_name, surname, original_phone].join('_')
|
||||
end
|
||||
|
||||
def color_hex
|
||||
case color || 'green'
|
||||
when 'green'
|
||||
'#22c55e'
|
||||
when 'yellow'
|
||||
'#eab308'
|
||||
when 'red'
|
||||
'#ef4444'
|
||||
else
|
||||
'#22c55e'
|
||||
end
|
||||
end
|
||||
|
||||
def color_emoji
|
||||
case color || 'green'
|
||||
when 'green'
|
||||
'🟩'
|
||||
when 'yellow'
|
||||
'🟨'
|
||||
when 'red'
|
||||
'🟥'
|
||||
else
|
||||
'🟩'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_original_phone
|
||||
self.original_phone = phone if original_phone.blank?
|
||||
end
|
||||
|
||||
def set_default_color
|
||||
self.color = 'green' if color.blank?
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user