![Code Climate](https://codeclimate.com/github/companygardener/lookup_by.png)
LookupBy
LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains.
- Configurable lookup column
- Caching (read-through, write-through, least-recently used (LRU))
- Symbolized values
- Normalized values, e.g. canonicalizing UTF-8 before lookup
Dependencies
- Rails 6.0+ (tested on Rails 6.0, 6.1, and 7.0)
- Ruby 2.7+ (tested on Ruby 2.7, 3.0, 3.1)
- PostgreSQL 9.2+ (tested on 14.2)
Deprecations
- Rails <= 5.2 (5.x breaks test suite on ruby 3.1; 4.x is incompatible with bundler 2.x, too hard to maintain)
- Ruby <= 2.6 (end of life; may depend on openssl@1.0, which is also end of life)
If you must use an old version of Ruby, good luck to you. You could try:
brew install rbenv/tap/openssl@1.0
brew install ruby-install
ruby-install ruby 2.2.10 --no-install-deps -- --with-openssl-dir=$(brew --prefix openssl@1.0) --disable-install-doc
Development
Source
- git clone git://github.com/companygardener/lookup_by.git
Bug reports
Please create Issues to submit bug reports and feature requests. However, I ask that you'd kindly review these bug reporting guidelines first.
If you find a security bug, do not use the public issue tracker. Instead, send an email to: thecompanygardener[removethisifnotspam]@gmail.com.
Installation
Add this line to your application's Gemfile:
gem "lookup_by"
And then execute:
$ bundle
Or install it manually:
$ gem install lookup_by
Usage
ActiveRecord Plugin
LookupBy adds two "macro" methods to ActiveRecord::Base
class ExampleLookup < ActiveRecord::Base
lookup_by :column_name
end
class ExampleObject < ActiveRecord::Base
lookup_for :status
end
class Address < ActiveRecord::Base
lookup_for :city, scope: :inside_city, inverse_scope: :outside_city
end
Define the lookup model
create_table :statuses, primary_key: :status_id do |t|
t.text :status, null: false
end
create_lookup_table :statuses
class Status < ActiveRecord::Base
lookup_by :status
end
Status.seed *%w[unpaid paid shipped]
Status.new(name: "paid")
Define an association
create_table :orders do |t|
t.belongs_to :status
end
class Order < ActiveRecord::Base
lookup_for :status
end
LookupBy creates methods that use the status
attribute transparently:
order = Order.new(status: "paid")
order.status
=> "paid"
order.status_id
=> 1
order.raw_status
=> #<Status id: 1, status: "paid">
order.status_before_type_cast
=> "paid"
Order.column_names
=> ["order_id", "status_id"]
Seed the lookup table
Status.seed *%w[unpaid paid shipped returned]
Manage lookups globally
LookupBy.clear
LookupBy.disable
LookupBy.enable
LookupBy.reload
Configuration
Symbolize
Casts the attribute to a symbol. Enables the setter to take a symbol.
Bad idea when the set of lookup values is large. Symbols are never garbage collected.
class Order < ActiveRecord::Base
lookup_for :status, symbolize: true
end
order = Order.new(status: "paid")
order.status
=> :paid
order.status = :shipped
=> :shipped
Strict
By default, missing lookup values will raise an error.
lookup_for :status
Order.status = "non-existent status"
lookup_for :status, strict: false
Caching
The default is no caching. You can also cache all records or use an LRU.
Note: caching is per process, make sure you think through the implications.
lookup_by :column_name
lookup_by :column_name, cache: true
lookup_by :column_name, cache: 50
Cache miss
Enable cache read-throughs using the :find
option.
lookup_by :column_name, cache: true
lookup_by :column_name, cache: 10
lookup_by :column_name, cache: true, find: true
DB miss
Enable cache write-throughs using the :find_or_create
option.
Note: This will only work if the primary key is a sequence and all columns but the lookup column are optional.
lookup_by :column_name
lookup_by :column_name, cache: 20, find_or_create: true
Raise on miss
Configure cache misses to raise a LookupBy::RecordNotFound
error.
lookup_by :column_name, cache: true
lookup_by :column_name, cache: true, raise: true
lookup_by :column_name, cache: true, find: true, raise: true
Normalize values
lookup_by :column_name, normalize: true
Allow blank
Can be useful to handle params
that are not required.
lookup_by :column_name, allow_blank: true
Threadsafety
Disable threadsafety using the :safe
option.
lookup_by :column_name, cache: 10, safe: false
Integration
Cucumber
require 'lookup_by/cucumber'
This provides: Given I reload the cache for $plural_class_name
SimpleForm
= simple_form_for @order do |f|
= f.input :status
= f.input :status, :as => :radio_buttons
Formtastic
= semantic_form_for @order do |f|
= f.input :status
= f.input :status, :as => :radio
Testing
This plugin uses rspec and pry for testing. Make sure you have them installed:
bundle
To run the test suite:
rake app:db:test:prepare
rake
Contribute
- Fork
- Create a feature branch
git checkout -b new-hotness
- Commit your changes
git commit -am 'Added some feature'
- Push to the branch
git push origin new-hotness
- Create a Pull Request
A list of authors can be found on the Contributors page.
License
Copyright © 2014–2022 Erik Peterson
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.