We have a new gem
Minidusen
Show archive.org snapshot
which extracts Dusen's query parsing and LIKE
query functionality.
Minidusen can no longer index text in MySQL FULLTEXT columns, which was hardly used and didn't always help performance due to the cost of reindexing.
Minidusen is currently compatible with MySQL, PostgreSQL, Rails 3.2, Rails 4.2 and Rails 5.0.
Basic Usage
Our example will be a simple address book:
class Contact < ActiveRecord::Base
validates_presence_of :name, :street, :city, :email
end
We create a new class ContactFilter
that will describe the searchable columns:
class ContactFilter
include Minidusen::Filter
filter :text do |scope, phrases|
columns = [:name, :email]
scope.where_like(columns => phrases)
end
end
We can now use ContactFilter
to filter a scope of Contact
records:
# We start by building a scope of all contacts.
# No SQL query is made.
all_contacts = Contact.all
# => ActiveRecord::Relation
# Now we filter the scope to only contain contacts with "gmail" in either :name or :email column.
# Again, no SQL query is made.
gmail_contacts = ContactFilter.new.filter(all_contacts, 'gmail')
# => ActiveRecord::Relation
# Inspect the filtered scope.
gmail_contacts.to_sql
# => "SELECT * FROM contacts WHERE name LIKE '%gmail%' OR email LIKE '%gmail%'"
# Finally we load the scope to produce an array of Contact records.
gmail_contacts.to_a
# => Array
Posted by Henning Koch to makandra dev (2016-09-15 16:15)