Specifind
Specifind offers advanced ActiveRecord dynamic find_by_* methods that include comparators (like the grails ORM). Coupled with some solid SQL injection mitigation through strict verification of type and string escaping, your find methods will be much more readable. If an object of the wrong type (based on the type of the corresponding column of the db) is passed into a finder, it will raise an exception. Ruby 1.9.2 and above are supported
Installation
Include specifind in your Rails Gemfile
gem 'specifind'
and
$ bundle
Comparators
- in_list (list)
- less_than (value)
- less_than_equals (value)
- greater_than (value)
- greater_than_equals (value)
- like (String) - as you'd enter it as SQL (eg. 'alpha%' for items starting with alpha)
- ilike (String) - case insensitive like
- not_equal (value)
- between (lower value, upper value)
- is_not_null
- is_null
- equals (value) - shorthand is to leave comparator blank (eg. find_by_name would mean find_by_name_equals)
Usage
Here is an example person class:
class Person < ActiveRecord::Base
attr_accessible :age, :birthday, :name
end
Specifind is automatically included in ActiveRecord once it is added to your Gemfile. Given the three attributes above, here are some examples of methods you could use to slice your data:
Person.find_by_age_greater_than_and_birthday_between 15, DateTime.new(1985, 1, 1), DateTime.new(1987,3,6)
Person.find_by_name_like '%a%'
Person.find_by_name_is_not_null
Notes
- Currently, the ilike comparator only works with mysql (to the best of my knowledge)
- Unlike built in ActiveRecord methods, Specifind methods will always return a list, even if it only has one instance in it
Todos
- Implement boolean operator logic (eg. find_by_age_greater_than_or_name_like)
- Implement ability to use the same parameter multiple times (currently find_by_age_greater_than_and_age_less_than will choke)