simple_enum
simple_enum copied to clipboard
Default value for non-ActiveRecord models
I'm using as_enum
on a non-activerecord class by including it
is it possible to set a default value on an enum?
Actually rather than using ruby to handle that, I'd either set it A) in the initializer or B) in the reader of the _cd
value. Hope one of these solutions works for you?
class Message
extend SimpleEnum::Attribute
attr_accessor :status_cd
as_enum :status, %w{unread archived trashed}
# A) in the initializer...
def initializer
@status_cd = 0 # == :unread
end
# ...or B) ensure status_cd has a value when set to nil
def status_cd
@status_cd ||= 0 # == :unread
end
end
yeah, I was using like this but I find it to obscure
why can't we have the same syntax as the mongoid enum?
as_enum :gender, [:male, :female], field: { :type => Integer, :default => 1 }
Could you write a SimpleEnum::Convenience
similar to https://github.com/lwe/simple_enum/blob/master/lib/simple_enum/mongoid.rb which basically does
def as_enum(name, values, options = {})
source = options[:source].to_s.presence || "#{name}#{SimpleEnum.suffix}"
field_options = options.delete(:field) || {}
unless field_options === false
attr_accessor source
define_method(source) do
return field_options[:default] unless instance_variable_defined?("@#{source}")
instance_variable_get("@#{source}")
end
end
super
end
^^ not sure if it works at all, was just an idea, that we can have use include SimpleEnum::Convenience
similar to SimpleEnum::Mongoid
which creates the attribute, handles the default etc, what do you think? Also I'm not happy with the name Convenience, maybe you have a better idea :smile:
Update: how about SimpleEnum::Model
as it adds some "model" support by creating fields and stuff?
The whole point of enums is to be meaningful. I'd rather be able to set default values like this
as_enum :status, [:ok, :maintenance_needed, :broken], default: :ok
...rather than having to specify a default integer value that means nothing.
@Startouf +1
All right, it looks like a few people are interested in this feature. Following considerations:
- A) This only makes sense for non ActiveRecord models, as AR models should set the default value via Rails migration / in the DB schema and thus I do not think it makes sense to allow this for AR models
- B) Really like the proposal from @Startouf to allow setting it as enum value, which indeed would make sense
- C) This feature would then require to be implemented for MongoId and with Plain-Old-Ruby-Objects, any proposals?
- A) Yeah I guess you're right. I'm so glad I switched to Mongoid, I just can't stand migrations...
- B) Cool
- C) Is there going to be much difference between the Mongoid implementation and the Ruby object one ? do we want to change the
association_cd
value in the database after the field is created, or do we want the accessor to return the default value instead of nil ?
Anyway, you'll need to consider adding a :default key to your options param hash starting from attribute.rb
. Also, it wouldn't hurt to raise an exception of let the programmer/user know if the provided default value isn't in the enum hash.
To keep it simple I think both Ruby objects and Mongoid should use the same logic. For functionality like this my accessor model seems to start to break, as there's a single class handling reading, writing etc., so it's hard to have different behavior. Maybe it should be refactored to a reader / writer component and then the reader can be a DefaultValueReader
or something like that...