In the Ruby world, mixins are very popular. In fact, I’ve created a number of basic mixins (or concerns) in Rails projects and so it came to a point where I wanted to try converting it to a gem. Why? Because why not? Well, just for the experience.
Anyway, my first RubyGem is called Sorty Sorter. It sorts AR collection when given a set of parameters that will be validated against the whitelisted attributes in the model.
RubyGem: https://rubygems.org/gems/sorty_sorter
GitHub: https://github.com/katpadi/sorty_sorter
GemGoalz
Given an ActiveRecord::Relation collection and some arguments, the gem should:
- define the model attributes (and their defaults) for sorting
- whitelist the only sortable columns
- alias the column name so it won’t be exposed to the outside world
- extend ActiveRecord::Relation and add a convenience method to sort according to arguments passed
Howz
I already have the mixin and other classes that will do the goals I mentioned above.
To convert it to a gem, I browsed the http://guides.rubygems.org/make-your-own-gem. Of course. And then I came up with the hows.
2 Things
I want to be able to define something in the model on the class level. This hypothetical (at that point when I was thinking) method will take a hash argument so I can configure options. Anyway, I need to extend ActiveSupport::Concern and define class method and then mix in that method.
ActiveRecord::Base.send(:include, SortySorter::Declaration)
Module#include
is private so one way to “hack” it is the code above. It mixes in the sort_with method in the model.
[pastacode lang=”ruby” user=”katpadi” repos=”sorty_sorter” path_id=”lib/sorty_sorter/declaration.rb” revision=”” highlight=”” lines=”” provider=”github”/]
And then, I would add to ActiveRecord::Relation exstensions so I can use method sorty_sort
within a collection.
[pastacode lang=”ruby” user=”katpadi” repos=”sorty_sorter” path_id=”lib/sorty_sorter/relation.rb” revision=”” highlight=”” lines=”” provider=”github”/]
After doing these things, user can define sort_with
in the model and AR collections will have a sorty_sort
method.