Support enumeration class methods #1053

Merged
merged 3 commits into from Apr 7, 2012

Conversation

Projects
None yet
2 participants
Contributor

amacneil commented Mar 28, 2012

Thanks for rails admin, it's amazing.

The only small issue I have is that for the automatic enum methods (object#name_enum) it seems much cleaner to use class methods rather than instance methods.

The reason for this is when creating e.g. search forms, I want access to the enum without having to create an instance of an object. It's a pain having to create both class and instance methods for this, or creating a new object just to access the enum values.

I've attached a failing test case to describe the expected behavior, basically RA should check for Foo.color_enum as well as Foo.new.color_enum.

I'm not really sure how to go about implementing this, but happy to give it a try if you like the feature.

Please let me know if there's a better way of doing this that I'm missing.

Thanks again!

Contributor

amacneil commented Mar 28, 2012

I just added my suggested fix for this.

Basically, if the model's class responds to enum_method, it will use that instead, and fall back to the instance enum_method.

Again, if there is a better way of doing things please let me know :)

Collaborator

bbenezech commented Mar 30, 2012

I added :enum because I needed it, but frankly, this is just a nice hack that saves some time, out of RailsAdmin's scope (which is about presentation of data)...

I used instance methods so that you can have 'object dynamic' proposals.

Are you sure you want that ? It doesn't add any functionality.

Contributor

amacneil commented Mar 31, 2012

Well, I wish rails had a standard way of dealing with enums. But lacking a standard, I really like your way of handling it. It doesn't require an extra gem or anything, you can simply add an enum method to the model and store an int in the database.

However, it just feels like a class method makes more sense, so that it can be accessed without needing to create a new object. My implementation doesn't really change any existing functionality, it just means you can specify either a class method or instance method with your enum_method setting.

Collaborator

bbenezech commented Apr 7, 2012

Ok, sure, let's merge!

Thanks a lot.

bbenezech merged commit 473c0a5 into sferik:master Apr 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment