If your colors never change - or every type1 always has the same color as another type1 you could use different CONSTANTS in your model or a bitmask.
example for using constants:
class Event < ActiveRecord::Base
COLOR_TYPE1 = "#2EAC6A"
COLOR_TYPE2 = "#000000"
COLOR_TYPE3 = "#ffffff"
def color
case self.type
when type1
COLOR_TYPE1
when type2
COLOR_TYPE2
when type3
COLOR_TYPE3
else
raise "for this type is no color defined!"
end
end
end
You won´t need to set the color - because you do not have an attribute color. Simple use a normal method which returns the correct color for an instance, dependent on the type of the instance.
Another option without constants: (this approach is better than the above one, in my opinion :-) )
class Event < ActiveRecord::Base
def color
case self.type
when type1
"#2EAC6A"
when type2
"#000000"
when type3
"#ffffff"
else
raise "for this type is no color defined!"
end
end
end
If you have different classes for each instance, you must not use the constants, you just can define the colors directly:
class Type1 < Event
def color
"#2EAC6A"
end
end
def Type2 < Event
def color
"#000000"
end
end
def Type3 < Event
def color
"#ffffff"
end
end
Different classes have the advantage, that you can handle all your stuff which depends on the type directly in a subclass of your parent class "Event". Do you get the idea?