Random color assigned to new post statuses

This commit is contained in:
riggraz
2019-09-18 21:12:52 +02:00
parent 109381e791
commit a1dbbb07b4
2 changed files with 14 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
class PostStatus < ApplicationRecord
after_initialize :set_order_to_last
after_initialize :set_random_color, :set_order_to_last
validates :name, presence: true, uniqueness: true
validates :color, format: { with: /\A#(?:[0-9a-fA-F]{3}){1,2}\z/ }
@@ -12,6 +12,13 @@ class PostStatus < ApplicationRecord
end
end
def set_random_color
return unless new_record?
return unless color.nil?
self.color = '#' + Random.bytes(3).unpack1('H*')
end
def set_order_to_last
return unless new_record?
return unless order.nil?