active_enum icon indicating copy to clipboard operation
active_enum copied to clipboard

ID as symbols.

Open hackling opened this issue 12 years ago • 10 comments

Add functionality to active_enum to be able to use symbols as IDs.

Example usage

class Sex < ActiveEnum::Base
  value :id => :m, :name => 'Male'
  value :id => :f, :name => 'Female'
end

hackling avatar Jun 14 '13 04:06 hackling

Thanks very much! See #20 - it was my dream... And what about Strings, what do you think?

kuraga avatar Jun 14 '13 06:06 kuraga

The reason this PR came about was because we were initially using strings as IDs and it was causing all sorts of issues.

While I think that it can be done, after some discussion, we decided that it was best not to implement as it, quickly becomes confusing as to what is what, and the get and meta functions would need more of a rework.

Using IDs as symbols and names as strings, it's far easier to differentiate between the two.

hackling avatar Jun 14 '13 06:06 hackling

Thanks very much for deep explanation!

kuraga avatar Jun 14 '13 09:06 kuraga

I think that attribute setter is not working as expected. This code from extensions.rb

def define_active_enum_write_method(attribute)
  class_eval <<-DEF
     def #{attribute}=(arg)
       if arg.is_a?(Symbol)
         super self.class.active_enum_for(:#{attribute})[arg]
       else
         super arg
       end
     end
  DEF
end

causes saving name value to database, not id.

gsmetal avatar Jul 09 '13 13:07 gsmetal

And same for getter: string loaded from database, so it doesn't find value in storage.

gsmetal avatar Jul 09 '13 16:07 gsmetal

Just got back from a month holiday. Will have a look at this once I've been back behind the keyboard for a while and in a proper frame of mind.

No doubt the test coverage will need to be expanded to cover this.

hackling avatar Jul 09 '13 22:07 hackling

I'm sorry, but have you looked at this? Is it hard to correct?

gsmetal avatar Jul 24 '13 13:07 gsmetal

I haven't had a chance yet. Had a few major deadlines lately, however this is on the list of things to fix and review as it's come up in development of one of our apps.

hackling avatar Jul 30 '13 06:07 hackling

I also have the need for string ids. For now I solved it by forking active_enum and replacing all the checks for is_a?(Fixnum)with is_a?(Fixnum) || is_a?(String), see felixbuenemann/active_enum@a2bc720. So I'm using the opposite logic to what's proposed here.

I'm not yet sure as to what's better: Enforcing symbols or strings for IDs. In any case it breaks some behaviour of the bracket/get accessor.

felixbuenemann avatar Sep 24 '13 07:09 felixbuenemann

Finally, I corrected some problems and added tests in my PR: #38

gsmetal avatar Feb 28 '14 15:02 gsmetal