William Notowidagdo Kiranatama Staff
Knowledge


Though Picky doesn’t have an ORM integration yet, it shouldn’t stop you to use it on your Rails app. I will show you how.

Picky server

First thing you want to do is to install Picky
$ gem install picky-generators
then generate a new Picky server app
$ picky generate classic_server contact_search

in example above, contact_search is my app name.

Now go to your newly create Picky server app, open up application.rb and let’s start the integration.

Include the model you’d like to use, in my case I want to get contact.rb from my Simple_Contact_Management app.
require "#{PICKY_ROOT}/../Simple_Contact_Management/app/models/contact"
Picky will need your Rails app database connection when generating indices
db_config = YAML.load(File.open("#{PICKY_ROOT}/../Simple_Contact_Management/config/database.yml"))

Contact.establish_connection db_config[PICKY_ENVIRONMENT]

Next, you’d want to define how Picky will do the indexing and searching. You can learn more on the subject here.

Then, create an index from the model we have required previously
contacts_index = Index.new :contact_each do
  source   Contact.order('full_name ASC')
  category :full_name
  category :job_title
  category :company_name
  category :phone
end
Last, add a route
route %r{\A/contacts\Z} => Search.new(contacts_index)
Now you ready to go, go to your Picky server app, build some indices and start the Picky server.
$ cd contact_search
$ bundle exec rake index
$ bundle exec rake start

 

Picky client (== your Rails app)

On your Rails app, add the picky-client in your Gemfile and run update your gem bundle.
gem 'picky-client', "~> 3.2.0"
Define a Picky client constant in your environment configuration file
ContactSearch = Picky::Client.new(
      :host => 'localhost',
      :port => 9090,
      :path => '/contacts'
  )
Use them in a controller
class ContactsController < ApplicationController
  def index
    @contacts = ContactSearch.search "maria"
    @contacts.extend Picky::Convenience
  end
and in the corresponding view
<% @contacts.populate_with Contact do |contact| %>

  <%= contact.full_name %>
  <%= contact.job_title %>
  <%= contact.company_name %>
  <%= contact.phone %>
  <%= link_to 'Show', contact %>
  <%= link_to 'Edit', edit_contact_path(contact) %>
  <%= link_to 'Destroy', contact, confirm: 'Are you sure?', method: :delete %>

<% end %>

 

Summary

You can use Picky as a fast search engine for your Rails app. Picky is usable when your app deal with categorized data (like names, or categories).

Not limited to ActiveRecord model, Picky could use database and CSV as its source. Actually, Picky could use any object supporting #each.

Sure there are still lot of Picky's features than I didn't cover here. I urge you to learn more by visit its website or get in touch with the mastermind: Florian Hanke.