An open API service indexing awesome lists of open source software.

https://github.com/barek2k2/record_locator


https://github.com/barek2k2/record_locator

Last synced: over 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# Introduction
Sometimes you may not want to expose Database table ID on application or on browsers but you need to show/use the ID as encoded.

In that case, record_locator ruby gem will provide you flexible way to show the same ID encoded and at the same time
you can find ActiveRecord object by the encoded id from your application instead of using original database ID!

For an example:
Remember the last time that you bought an airline ticket? You got an email with a "record locator". When you checked in
for your flight, the airline didn't make you enter "23873498712098234095723469" to get your boarding pass. You entered
something like "FK92B9", and your name. The airline used your name and that code to find the big ID.

# How to install
Include the gem into your Gemfile



gem 'record_locator', '1.0.0', :git => 'git@github.com:barek2k2/record_locator.git'

Then run



bundle install

Suppose, you want to apply this encoding stuff on your Book Model's publisher_id then follow this way:



class Book < ActiveRecord::Base
extend RecordLocator
has_record_locator :publisher_id
end

Now restart your server!


has_record_locator will expect Numeric field only (here publisher_id is Integer)

From your ActiveRecord model object, You can get the encoded value by calling encoded_record_locator method:



encoded_publisher_id = @book.encoded_record_locator

Here, encoded_publisher_id will give you the encoded id of publisher_id of Book Model

You can find the ActiveRecord Model Object by passing encoded value, for example:



Book.record_locator.find(encoded_publisher_id)

You can get ActiveRecord objects as Array like this:



Book.record_locator.find_all(encoded_publisher_id)

If you want to pass origin ActiveRecord ID instead of encoded ID then you can pass simply the originla ID and this
gem will give you original Activerecord object as normal like this:



Book.record_locator.find(params[:publisher_id])

#Tested and Supports for
Ruby 1.8.7, 1.9.2,1.9.3,2.0
Rails 2.3,3.2,4.0