A time ago I want to create a Gem to learn how this works. But in the Ruby/Rails world we live in a mature community when we have a lot of libs to do our job, so I give up for a moment, until I have a problem that more people have experiencied too.
So in a moment I find a pattern, that I and my friends do. When I have a model, with validation of inclusion like this:
I normally need predicate methods like
@post.published? and scopes like
Post.published. So by this idea and some work born Jacaranda my first gem.
But now in the rails 4.1.0, I testing using 4.1.0.beta1 since no release yet. We have the ActiveRecord::Enum that can do the same thing we do with Jacaranda.
Meeting the ActiveRecord::Enum
Lets create the same behavior of Jacaranda now using the
We started with a migration like this.
The important part is the column is a integer and we set the default value as 0.
In our model we simply use the macro
In the Wild
We start with a post with status is
published. Remember you need to pass to the model the string and not integer, a
ArgumentError is raised if you try pass the integer value.
We can use the scopes with the same name as declareted on our model.
Search is made using the integer value on the database.
The bang are not in Jacaranda, but are cool =)
You can use the bang method to change the status of post.
Now our post have the status as a draft.
One cool thing about the
ActiveRecord::Enum is that all values are stored like a integer and accessed as a string.
Using explicit mapping
Add status without key can be annoying since if one new status is added in a wrong position all objects have values changed.
We add reviewed before the published, so now all published posts have the status as reviewed.
To avoid this problem, we can use the explicit mapping like.
This way we dont care about the order but with the value.
Of course good specs ensure we are adding a thing that dont break others.
Jacaranda is now deprecated, since we can use the
I learned a lot in the way using services like: