Add lib/server/mixin.rb

This commit is contained in:
0x1eef 2024-04-27 02:25:12 -03:00
parent e89687d445
commit 31600100d2
7 changed files with 40 additions and 35 deletions

View file

@ -62,6 +62,7 @@ module Twenty
end
require "sequel"
require_relative "server/mixin"
require_relative "server/migration"
require_relative "server/graphql"
require_relative "server/rack"

View file

@ -2,7 +2,7 @@
Sequel.migration do
up do
default = Twenty::ColorableMixin.random_color
default = Twenty::Mixin::Colorable.random_color
add_column :projects, :color, :string, null: false, default:
end

View file

@ -0,0 +1,3 @@
module Twenty::Mixin
require_relative "mixin/colorable"
end

View file

@ -0,0 +1,34 @@
# frozen_string_literal: true
module Twenty::Mixin
module Colorable
extend self
COLORS = [
"#222222", "#333333", "#444444", "#555555", "#666666",
"#777777", "#888888", "#999999", "#AA2222", "#22AA22",
"#2222AA", "#AA22AA", "#CC9900", "#0099CC", "#9900CC",
"#FF9900", "#00CC99", "#99CC00", "#CC0099", "#990000",
"#112233", "#445566", "#778899", "#AA4455", "#5544AA",
"#88AA44", "#AA88AA", "#CCBB00", "#1155CC", "#9900BB",
"#DD6600", "#00BBCC", "#CC0099", "#BB3300", "#006688",
"#993366", "#2200AA", "#557788", "#998877", "#BB4400"
]
def before_validation
super if defined?(super)
set_random_color
end
def random_color
COLORS.sample
end
private
def set_random_color
return if id
self.color = random_color
end
end
end

View file

@ -6,7 +6,6 @@ module Twenty::Model
model.plugin(:timestamps, update_on_create: true)
end
require_relative "model/mixin/colorable_mixin"
require_relative "model/project"
require_relative "model/task"
end

View file

@ -1,32 +0,0 @@
# frozen_string_literal: true
module Twenty::ColorableMixin
extend self
COLORS = [
"#222222", "#333333", "#444444", "#555555", "#666666",
"#777777", "#888888", "#999999", "#AA2222", "#22AA22",
"#2222AA", "#AA22AA", "#CC9900", "#0099CC", "#9900CC",
"#FF9900", "#00CC99", "#99CC00", "#CC0099", "#990000",
"#112233", "#445566", "#778899", "#AA4455", "#5544AA",
"#88AA44", "#AA88AA", "#CCBB00", "#1155CC", "#9900BB",
"#DD6600", "#00BBCC", "#CC0099", "#BB3300", "#006688",
"#993366", "#2200AA", "#557788", "#998877", "#BB4400"
]
def before_validation
super if defined?(super)
set_random_color
end
def random_color
COLORS.sample
end
private
def set_random_color
return if id
self.color = random_color
end
end

View file

@ -2,7 +2,7 @@
class Twenty::Project < Sequel::Model
include Twenty::Model
include Twenty::ColorableMixin
include Mixin::Colorable
validates_presence_of :name
validates_presence_of :path