lookup_by 0.10.9 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +17 -9
- data/Appraisals +6 -6
- data/CHANGELOG.md +58 -0
- data/Gemfile +6 -10
- data/MIT-LICENSE +1 -1
- data/README.md +30 -27
- data/Rakefile +20 -2
- data/circle.yml +4 -2
- data/gemfiles/rails_6.0.gemfile +17 -0
- data/gemfiles/rails_6.1.gemfile +17 -0
- data/gemfiles/rails_7.0.gemfile +17 -0
- data/lib/lookup_by/association.rb +9 -3
- data/lib/lookup_by/cache.rb +17 -21
- data/lib/lookup_by/caching/safe_lru.rb +1 -1
- data/lib/lookup_by/hooks/formtastic.rb +2 -1
- data/lib/lookup_by/hooks/simple_form.rb +2 -1
- data/lib/lookup_by/lookup.rb +27 -13
- data/lib/lookup_by/version.rb +1 -1
- data/lib/lookup_by.rb +2 -0
- data/lookup_by.gemspec +2 -2
- data/spec/association_spec.rb +1 -0
- data/spec/cache_spec.rb +10 -0
- data/spec/{dummy → internal}/app/models/account.rb +0 -0
- data/spec/{dummy → internal}/app/models/address.rb +0 -0
- data/spec/{dummy → internal}/app/models/city.rb +0 -0
- data/spec/{dummy → internal}/app/models/country.rb +0 -0
- data/spec/{dummy → internal}/app/models/email_address.rb +0 -0
- data/spec/{dummy → internal}/app/models/ip_address.rb +0 -0
- data/spec/{dummy → internal}/app/models/path.rb +0 -0
- data/spec/{dummy → internal}/app/models/phone_number.rb +0 -0
- data/spec/{dummy → internal}/app/models/postal_code.rb +0 -0
- data/spec/{dummy → internal}/app/models/raisin.rb +0 -0
- data/spec/{dummy → internal}/app/models/read_through_raisin.rb +0 -0
- data/spec/{dummy → internal}/app/models/state.rb +0 -0
- data/spec/{dummy → internal}/app/models/status.rb +0 -0
- data/spec/{dummy → internal}/app/models/street.rb +0 -0
- data/spec/{dummy → internal}/app/models/uncacheable.rb +0 -0
- data/spec/{dummy → internal}/app/models/unfindable.rb +0 -0
- data/spec/{dummy → internal}/app/models/unsynchronizable.rb +0 -0
- data/spec/{dummy → internal}/app/models/user_agent.rb +0 -0
- data/spec/internal/config/database.yml +8 -0
- data/spec/internal/db/schema.rb +49 -0
- data/spec/{dummy → internal}/lib/missing.rb +0 -0
- data/spec/internal/log/.gitignore +1 -0
- data/spec/lookup_by_spec.rb +22 -6
- data/spec/rails_helper.rb +6 -13
- data/spec/support/shared_examples_for_a_lookup.rb +28 -7
- metadata +58 -94
- data/gemfiles/rails_4.0.gemfile +0 -22
- data/gemfiles/rails_4.1.gemfile +0 -22
- data/gemfiles/rails_4.2.gemfile +0 -22
- data/spec/dummy/Rakefile +0 -22
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/bin/bundle +0 -3
- data/spec/dummy/bin/rails +0 -4
- data/spec/dummy/bin/rake +0 -4
- data/spec/dummy/config/application.rb +0 -28
- data/spec/dummy/config/boot.rb +0 -6
- data/spec/dummy/config/database.yml +0 -44
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -20
- data/spec/dummy/config/environments/test.rb +0 -19
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy/config/locales/en.yml +0 -23
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20121019040009_create_tables.rb +0 -41
- data/spec/dummy/db/seeds.rb +0 -1
- data/spec/dummy/db/structure.sql +0 -1053
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/script/rails +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4c1d5aaec4ced07731c227dfb1ae27903fed94b0feb28cecb6f1e3a27e55eb3c
|
4
|
+
data.tar.gz: a7185879dbfc9bff84bf7b8795913731f577254925c760e9bc096bc4091493e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abfa6b7b5053fd41e61f419c873a02a05b833ae96f104101858f86331d96d7f598b7d09d4b3812637a964ab23ce89c1ead6137ca0d11220b93af592af7197aa9
|
7
|
+
data.tar.gz: 067763d23e4a3e6fa1dfa9b0e27e3fd90bc7f69710a7dd129a2fa00a26131af2632eabe0b208ac9733440fe89240285d273600c787afe235d21a8788bd13a59f
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-
|
1
|
+
ruby-3.1.2
|
data/.travis.yml
CHANGED
@@ -1,16 +1,24 @@
|
|
1
1
|
language: ruby
|
2
|
+
|
3
|
+
cache:
|
4
|
+
bundler: true
|
5
|
+
|
2
6
|
rvm:
|
3
|
-
-
|
4
|
-
- 2.
|
5
|
-
- 2.1
|
6
|
-
|
7
|
-
- 2.3.0
|
8
|
-
- rbx-3.19
|
7
|
+
- 2.2.2
|
8
|
+
- 2.2.5
|
9
|
+
- 2.3.1
|
10
|
+
|
9
11
|
services:
|
10
12
|
- postgresql
|
11
|
-
|
12
|
-
|
13
|
+
|
14
|
+
# Travis defaults to PostgreSQL 9.1
|
15
|
+
addons:
|
16
|
+
postgresql: "9.4"
|
17
|
+
|
13
18
|
script:
|
14
|
-
- bundle exec rake app:db:test:prepare
|
15
19
|
- bundle exec appraisal install
|
16
20
|
- bundle exec appraisal rake
|
21
|
+
|
22
|
+
general:
|
23
|
+
artifacts:
|
24
|
+
- coverage
|
data/Appraisals
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
appraise 'rails-
|
2
|
-
gem '
|
1
|
+
appraise 'rails-6.0' do
|
2
|
+
gem 'activerecord', '~> 6.0.0'
|
3
3
|
end
|
4
4
|
|
5
|
-
appraise 'rails-
|
6
|
-
gem '
|
5
|
+
appraise 'rails-6.1' do
|
6
|
+
gem 'activerecord', '~> 6.1.0'
|
7
7
|
end
|
8
8
|
|
9
|
-
appraise 'rails-
|
10
|
-
gem '
|
9
|
+
appraise 'rails-7.0' do
|
10
|
+
gem 'activerecord', '~> 7.0.0'
|
11
11
|
end
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
### [v0.12.0](https://github.com/companygardener/lookup_by/compare/v0.11.2...v0.12.0)
|
2
|
+
|
3
|
+
Require ruby 2.7. Support ruby 3.1
|
4
|
+
|
5
|
+
* Appraisal: remove rails 5.0, 5.1, and 5.2
|
6
|
+
* Appraisal: add rails 7.0
|
7
|
+
* Dev: .ruby-version 3.1.2
|
8
|
+
|
9
|
+
### [v0.11.2](https://github.com/companygardener/lookup_by/compare/v0.11.1...v0.11.2)
|
10
|
+
|
11
|
+
Merged a few PRs. Shouldn't break anything.
|
12
|
+
|
13
|
+
* Appraisal: add rails 5.1 and 5.2
|
14
|
+
* Appraisal: bundle update
|
15
|
+
* Fix: alias_method_chain was deprecated a long time ago
|
16
|
+
* Fix: clearing the cache will also clear the reverse lookup cache
|
17
|
+
* Dev: .ruby-version 2.6.1
|
18
|
+
* Doc: docs for :scope and :inverse_scope options on `lookup_for` method
|
19
|
+
* Doc: add changelog
|
20
|
+
|
21
|
+
### [v0.11.1](https://github.com/companygardener/lookup_by/compare/v0.11.0...v0.11.1)
|
22
|
+
|
23
|
+
#### Fix .all on lookup models
|
24
|
+
|
25
|
+
Version 0.11.0 breaks Model.where(field: 'value').first_or_create! when
|
26
|
+
Model is configured with `lookup_by :field, cache: true`.
|
27
|
+
|
28
|
+
### [v0.11.0](https://github.com/companygardener/lookup_by/compare/v0.10.9...v0.11.0)
|
29
|
+
|
30
|
+
#### Rails 5.0.0 support
|
31
|
+
|
32
|
+
Update .count signature to support Rails 5.
|
33
|
+
|
34
|
+
- Require ruby 2.2.2+
|
35
|
+
- PostgreSQL 9.2+
|
36
|
+
- Drop support for JRuby
|
37
|
+
|
38
|
+
#### Default to a threadsafe cache
|
39
|
+
|
40
|
+
### [v0.10.9](https://github.com/companygardener/lookup_by/compare/v0.10.8...v0.10.9)
|
41
|
+
|
42
|
+
### [v0.10.8](https://github.com/companygardener/lookup_by/compare/v0.10.7...v0.10.8)
|
43
|
+
|
44
|
+
### [v0.10.7](https://github.com/companygardener/lookup_by/compare/v0.10.6...v0.10.7)
|
45
|
+
|
46
|
+
### [v0.10.6](https://github.com/companygardener/lookup_by/compare/v0.10.5...v0.10.6)
|
47
|
+
|
48
|
+
### [v0.10.5](https://github.com/companygardener/lookup_by/compare/v0.10.4...v0.10.5)
|
49
|
+
|
50
|
+
### [v0.10.4](https://github.com/companygardener/lookup_by/compare/v0.10.3...v0.10.4)
|
51
|
+
|
52
|
+
### [v0.10.3](https://github.com/companygardener/lookup_by/compare/v0.10.2...v0.10.3)
|
53
|
+
|
54
|
+
### [v0.10.2](https://github.com/companygardener/lookup_by/compare/v0.10.1...v0.10.2)
|
55
|
+
|
56
|
+
### [v0.10.1](https://github.com/companygardener/lookup_by/compare/v0.10.0...v0.10.1)
|
57
|
+
|
58
|
+
### [v0.10.0](https://github.com/companygardener/lookup_by/compare/v0.9.1...v0.10.0)
|
data/Gemfile
CHANGED
@@ -6,21 +6,17 @@ source "https://rubygems.org"
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
group :development, :test do
|
9
|
-
gem 'appraisal',
|
9
|
+
gem 'appraisal', "~> 2.4.1", require: false
|
10
|
+
|
11
|
+
gem 'combustion'
|
10
12
|
|
11
13
|
gem 'rspec'
|
12
|
-
gem 'rspec-its'
|
13
14
|
gem 'rspec-rails'
|
14
15
|
|
15
16
|
gem "pg", platform: :ruby
|
16
|
-
gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
|
17
|
-
|
18
|
-
gem "simplecov", require: false
|
19
|
-
gem 'coveralls', require: false
|
17
|
+
# gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
|
20
18
|
|
21
|
-
gem "
|
22
|
-
gem 'colored', require: false
|
19
|
+
gem "simplecov", "~> 0.21.2", require: false
|
23
20
|
|
24
|
-
gem
|
25
|
-
gem 'json'
|
21
|
+
gem "pry", require: false
|
26
22
|
end
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,33 +1,35 @@
|
|
1
|
-
# LookupBy
|
2
1
|
|
3
2
|
[![Gem Version](https://badge.fury.io/rb/lookup_by.png)][rubygems]
|
4
|
-
[![Build Status](https://secure.travis-ci.org/companygardener/lookup_by.png?branch=master)][travis]
|
5
|
-
[![Dependency Status](https://gemnasium.com/companygardener/lookup_by.png)][gemnasium]
|
6
|
-
[![Coverage Status](https://coveralls.io/repos/companygardener/lookup_by/badge.png?branch=master)][coveralls]
|
7
3
|
[![Code Climate](https://codeclimate.com/github/companygardener/lookup_by.png)][codeclimate]
|
8
|
-
[![Gittip](http://img.shields.io/gittip/companygardener.png)][gittip]
|
9
4
|
|
10
5
|
[rubygems]: https://rubygems.org/gems/lookup_by
|
11
|
-
[travis]: http://travis-ci.org/companygardener/lookup_by
|
12
|
-
[gemnasium]: https://gemnasium.com/companygardener/lookup_by
|
13
|
-
[coveralls]: https://coveralls.io/r/companygardener/lookup_by?branch=master
|
14
6
|
[codeclimate]: https://codeclimate.com/github/companygardener/lookup_by
|
15
|
-
[gittip]: https://www.gittip.com/companygardener
|
16
7
|
|
17
|
-
#
|
8
|
+
# LookupBy
|
18
9
|
|
19
10
|
LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces normalization pains.
|
20
11
|
|
21
12
|
* Configurable lookup column
|
22
|
-
* Caching (read-through, write-through, least
|
13
|
+
* Caching (read-through, write-through, least-recently used (LRU))
|
23
14
|
* Symbolized values
|
24
15
|
* Normalized values, _e.g. canonicalizing UTF-8 before lookup_
|
25
16
|
|
26
17
|
### Dependencies
|
27
18
|
|
28
|
-
* Rails
|
29
|
-
* Ruby
|
30
|
-
* PostgreSQL
|
19
|
+
* Rails 6.0+ (_tested on Rails 6.0, 6.1, and 7.0_)
|
20
|
+
* Ruby 2.7+ (_tested on Ruby 2.7, 3.0, 3.1_)
|
21
|
+
* PostgreSQL 9.2+ (tested on 14.2)
|
22
|
+
|
23
|
+
### Deprecations
|
24
|
+
|
25
|
+
- Rails <= 5.2 (5.x breaks test suite on ruby 3.1; 4.x is incompatible with bundler 2.x, too hard to maintain)
|
26
|
+
- Ruby <= 2.6 (end of life; may depend on openssl@1.0, which is also end of life)
|
27
|
+
|
28
|
+
If you must use an old version of Ruby, good luck to you. You could try:
|
29
|
+
|
30
|
+
brew install rbenv/tap/openssl@1.0
|
31
|
+
brew install ruby-install
|
32
|
+
ruby-install ruby 2.2.10 --no-install-deps -- --with-openssl-dir=$(brew --prefix openssl@1.0) --disable-install-doc
|
31
33
|
|
32
34
|
### Development
|
33
35
|
|
@@ -41,7 +43,7 @@ LookupBy is a thread-safe lookup table cache for ActiveRecord that reduces norma
|
|
41
43
|
|
42
44
|
Please create [Issues][] to submit bug reports and feature requests. However, I ask that you'd kindly review [these bug reporting guidelines](https://github.com/companygardener/lookup_by/wiki/Bug-Reports) first.
|
43
45
|
|
44
|
-
_If you find a security bug, **
|
46
|
+
_If you find a security bug, **do not** use the public issue tracker. Instead, send an email to: thecompanygardener[removethisifnotspam]@gmail.com._
|
45
47
|
|
46
48
|
# Installation
|
47
49
|
|
@@ -75,6 +77,11 @@ class ExampleObject < ActiveRecord::Base
|
|
75
77
|
# Defines #status and #status= instance methods that transparently reference the lookup table.
|
76
78
|
# Defines .with_status(*names) and .without_status(*names) scopes on the model.
|
77
79
|
end
|
80
|
+
|
81
|
+
class Address < ActiveRecord::Base
|
82
|
+
# scopes can be renamed
|
83
|
+
lookup_for :city, scope: :inside_city, inverse_scope: :outside_city
|
84
|
+
end
|
78
85
|
```
|
79
86
|
|
80
87
|
### Define the lookup model
|
@@ -223,11 +230,10 @@ lookup_by :column_name
|
|
223
230
|
# Cache all
|
224
231
|
# Use for a small finite list (e.g. status codes, US states)
|
225
232
|
#
|
226
|
-
# Defaults to no read-through
|
227
|
-
# options[:find] = false
|
233
|
+
# Defaults to no read-through, e.g. options[:find] = false
|
228
234
|
lookup_by :column_name, cache: true
|
229
235
|
|
230
|
-
# Cache N (
|
236
|
+
# Cache N records, evicting the least-recently used (LRU)
|
231
237
|
# Use for large sets with uneven distribution (e.g. email domain, city)
|
232
238
|
#
|
233
239
|
# Requires read-through
|
@@ -290,7 +296,7 @@ lookup_by :column_name, cache: true, find: true, raise: true
|
|
290
296
|
|
291
297
|
```ruby
|
292
298
|
# Normalize
|
293
|
-
#
|
299
|
+
# Call the attribute's setter
|
294
300
|
lookup_by :column_name, normalize: true
|
295
301
|
```
|
296
302
|
|
@@ -306,14 +312,11 @@ lookup_by :column_name, allow_blank: true
|
|
306
312
|
|
307
313
|
### Threadsafety
|
308
314
|
|
309
|
-
|
310
|
-
|
311
|
-
With Rails 4, the Safe LRU is selected automatically in production-like environments.
|
315
|
+
Disable threadsafety using the `:safe` option.
|
312
316
|
|
313
317
|
```ruby
|
314
|
-
#
|
315
|
-
|
316
|
-
lookup_by :column_name, cache: 10, safe: true
|
318
|
+
# Default: true
|
319
|
+
lookup_by :column_name, cache: 10, safe: false
|
317
320
|
```
|
318
321
|
|
319
322
|
# Integration
|
@@ -343,7 +346,7 @@ This provides: `Given I reload the cache for $plural_class_name`
|
|
343
346
|
= f.input :status, :as => :radio
|
344
347
|
```
|
345
348
|
|
346
|
-
|
349
|
+
# Testing
|
347
350
|
|
348
351
|
This plugin uses rspec and pry for testing. Make sure you have them installed:
|
349
352
|
|
@@ -366,7 +369,7 @@ A list of authors can be found on the [Contributors][] page.
|
|
366
369
|
|
367
370
|
# License
|
368
371
|
|
369
|
-
Copyright © 2014 Erik Peterson
|
372
|
+
Copyright © 2014–2022 Erik Peterson
|
370
373
|
|
371
374
|
MIT License
|
372
375
|
|
data/Rakefile
CHANGED
@@ -5,12 +5,30 @@ rescue LoadError
|
|
5
5
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
load 'rails/tasks/engine.rake'
|
8
|
+
require 'combustion'
|
10
9
|
|
11
10
|
Bundler::GemHelper.install_tasks
|
12
11
|
|
13
12
|
require "rspec/core/rake_task"
|
14
13
|
RSpec::Core::RakeTask.new(:spec)
|
15
14
|
|
15
|
+
desc 'Start a console'
|
16
|
+
task :console do
|
17
|
+
Combustion.initialize! :active_record
|
18
|
+
|
19
|
+
ARGV.clear
|
20
|
+
|
21
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
22
|
+
|
23
|
+
require 'pry'
|
24
|
+
Pry.start
|
25
|
+
end
|
26
|
+
|
27
|
+
namespace :db do
|
28
|
+
desc 'Setup db'
|
29
|
+
task :setup do
|
30
|
+
Combustion.initialize! :active_record
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
16
34
|
task :default => :spec
|
data/circle.yml
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 6.0.0"
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "appraisal", "~> 2.4.1", require: false
|
9
|
+
gem "combustion"
|
10
|
+
gem "rspec"
|
11
|
+
gem "rspec-rails"
|
12
|
+
gem "pg", platform: :ruby
|
13
|
+
gem "simplecov", "~> 0.21.2", require: false
|
14
|
+
gem "pry", require: false
|
15
|
+
end
|
16
|
+
|
17
|
+
gemspec path: "../"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 6.1.0"
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "appraisal", "~> 2.4.1", require: false
|
9
|
+
gem "combustion"
|
10
|
+
gem "rspec"
|
11
|
+
gem "rspec-rails"
|
12
|
+
gem "pg", platform: :ruby
|
13
|
+
gem "simplecov", "~> 0.21.2", require: false
|
14
|
+
gem "pry", require: false
|
15
|
+
end
|
16
|
+
|
17
|
+
gemspec path: "../"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "activerecord", "~> 7.0.0"
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "appraisal", "~> 2.4.1", require: false
|
9
|
+
gem "combustion"
|
10
|
+
gem "rspec"
|
11
|
+
gem "rspec-rails"
|
12
|
+
gem "pg", platform: :ruby
|
13
|
+
gem "simplecov", "~> 0.21.2", require: false
|
14
|
+
gem "pry", require: false
|
15
|
+
end
|
16
|
+
|
17
|
+
gemspec path: "../"
|
@@ -23,7 +23,7 @@ module LookupBy
|
|
23
23
|
end
|
24
24
|
|
25
25
|
options.symbolize_keys!
|
26
|
-
options.assert_valid_keys(:class_name, :foreign_key, :symbolize, :strict, :scope, :inverse_scope)
|
26
|
+
options.assert_valid_keys(:class_name, :foreign_key, :symbolize, :strict, :scope, :inverse_scope, :belongs_to)
|
27
27
|
|
28
28
|
field = field.to_sym
|
29
29
|
|
@@ -80,8 +80,11 @@ module LookupBy
|
|
80
80
|
|
81
81
|
Rails.logger.error "foreign key `#{foreign_key}` is required on #{self}" unless attribute_names.include?(foreign_key.to_s)
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
belongs = options[:belongs_to] || false
|
84
|
+
|
85
|
+
class_eval <<-BELONGS_TO, __FILE__, __LINE__.next if belongs
|
86
|
+
belongs_to :#{field}, class_name: "#{class_name}", foreign_key: :#{foreign_key}, autosave: false, optional: true
|
87
|
+
BELONGS_TO
|
85
88
|
|
86
89
|
class_eval <<-SCOPES, __FILE__, __LINE__.next if scope_name
|
87
90
|
scope :#{scope_name}, ->(*names) { where(#{foreign_key}: #{class_name}[*names]) }
|
@@ -102,6 +105,9 @@ module LookupBy
|
|
102
105
|
lookup_field = klass.lookup.field
|
103
106
|
lookup_object = "#{class_name}[#{foreign_key}]"
|
104
107
|
|
108
|
+
strict = options[:strict]
|
109
|
+
strict = true if strict.nil?
|
110
|
+
|
105
111
|
class_eval <<-METHODS, __FILE__, __LINE__.next
|
106
112
|
def raw_#{field}
|
107
113
|
#{lookup_object}
|
data/lib/lookup_by/cache.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module LookupBy
|
2
2
|
class Cache
|
3
|
-
attr_reader :cache, :field, :stats
|
3
|
+
attr_reader :cache, :reverse, :field, :stats
|
4
|
+
|
4
5
|
attr_accessor :testing
|
5
6
|
|
6
7
|
def initialize(klass, options = {})
|
@@ -18,7 +19,7 @@ module LookupBy
|
|
18
19
|
@raise_on_miss = options[:raise] || false
|
19
20
|
@testing = false
|
20
21
|
@enabled = true
|
21
|
-
@safe = options
|
22
|
+
@safe = options.fetch(:safe, true)
|
22
23
|
@mutex = Mutex.new if @safe
|
23
24
|
|
24
25
|
@stats = { db: Hash.new(0), cache: Hash.new(0) }
|
@@ -51,36 +52,36 @@ module LookupBy
|
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
|
-
def
|
55
|
+
def load
|
55
56
|
return unless @type == :all
|
56
57
|
|
57
|
-
clear
|
58
|
-
|
59
58
|
::ActiveRecord::Base.connection.send :log, "", "#{@klass.name} Load Cache All" do
|
60
|
-
@klass.order(@order).each do |object|
|
59
|
+
@klass.order(@order).readonly.each do |object|
|
61
60
|
cache_write(object)
|
62
61
|
end
|
63
62
|
end
|
64
63
|
end
|
65
64
|
|
65
|
+
def reload
|
66
|
+
clear
|
67
|
+
load
|
68
|
+
end
|
69
|
+
|
66
70
|
def clear
|
67
71
|
@cache.clear
|
72
|
+
@reverse.clear
|
68
73
|
end
|
69
74
|
|
70
75
|
def create(*args, &block)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
created
|
76
|
+
@klass.create(*args, &block).tap do |created|
|
77
|
+
cache_write(created) if cache?
|
78
|
+
end
|
76
79
|
end
|
77
80
|
|
78
81
|
def create!(*args, &block)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
created
|
82
|
+
@klass.create!(*args, &block).tap do |created|
|
83
|
+
cache_write(created) if cache?
|
84
|
+
end
|
84
85
|
end
|
85
86
|
|
86
87
|
def seed(*values)
|
@@ -154,11 +155,6 @@ module LookupBy
|
|
154
155
|
|
155
156
|
private
|
156
157
|
|
157
|
-
# RAILS_ENV=test will not use the SafeLRU
|
158
|
-
def concurrent?
|
159
|
-
Rails.configuration.cache_classes && Rails.configuration.eager_load
|
160
|
-
end
|
161
|
-
|
162
158
|
def primary_key?(value)
|
163
159
|
case @primary_key_type
|
164
160
|
when :integer
|
data/lib/lookup_by/lookup.rb
CHANGED
@@ -55,7 +55,7 @@ module LookupBy
|
|
55
55
|
end
|
56
56
|
|
57
57
|
@lookup = Cache.new(self, options.merge(field: field))
|
58
|
-
@lookup.
|
58
|
+
@lookup.load
|
59
59
|
end
|
60
60
|
|
61
61
|
LookupBy.register self
|
@@ -63,22 +63,36 @@ module LookupBy
|
|
63
63
|
end
|
64
64
|
|
65
65
|
module ClassMethods
|
66
|
-
#
|
67
|
-
def all
|
68
|
-
return super if
|
66
|
+
# Rails 4.1, 4.2, 5.0+
|
67
|
+
def all
|
68
|
+
return super if current_scope
|
69
|
+
|
69
70
|
return super if @lookup.read_through?
|
71
|
+
return super if @lookup.cache.empty?
|
70
72
|
return super if @lookup.disabled?
|
71
|
-
return super if args.any?
|
72
73
|
|
73
|
-
|
74
|
+
relation.tap do |rel|
|
75
|
+
rel.instance_variable_set(:@records, @lookup.cache.values)
|
76
|
+
rel.instance_variable_set(:@loaded, true)
|
77
|
+
end
|
74
78
|
end
|
75
79
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
+
if Rails::VERSION::MAJOR <= 4
|
81
|
+
# Rails 4.1, 4.2
|
82
|
+
def count(column_name = nil, options = {})
|
83
|
+
return super if @lookup.read_through?
|
84
|
+
return super if column_name
|
80
85
|
|
81
|
-
|
86
|
+
@lookup.cache.size
|
87
|
+
end
|
88
|
+
else
|
89
|
+
# Rails 5.0+
|
90
|
+
def count(column_name = nil)
|
91
|
+
return super if @lookup.read_through?
|
92
|
+
return super if column_name
|
93
|
+
|
94
|
+
@lookup.cache.size
|
95
|
+
end
|
82
96
|
end
|
83
97
|
|
84
98
|
def pluck(*column_names)
|
@@ -103,7 +117,7 @@ module LookupBy
|
|
103
117
|
when self then arg
|
104
118
|
else raise TypeError, "#{name}[*args]: args must be at least one String, Symbol, Integer, IPAddr, nil, or #{name}"
|
105
119
|
end
|
106
|
-
else return args.map { |arg| self[arg] }
|
120
|
+
else return args.map { |arg| self[arg] }
|
107
121
|
end
|
108
122
|
end
|
109
123
|
|
@@ -166,7 +180,7 @@ module LookupBy
|
|
166
180
|
|
167
181
|
table_options[:id] = false if options[:small]
|
168
182
|
|
169
|
-
create_table name, table_options do |t|
|
183
|
+
create_table name, **table_options do |t|
|
170
184
|
t.column table_options[:primary_key], 'smallserial primary key' if options[:small]
|
171
185
|
|
172
186
|
t.column lookup_column, lookup_type, null: false
|
data/lib/lookup_by/version.rb
CHANGED
data/lib/lookup_by.rb
CHANGED
data/lookup_by.gemspec
CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
23
|
gem.require_paths = ["lib"]
|
24
24
|
|
25
|
-
gem.add_dependency "activerecord", ">=
|
25
|
+
gem.add_dependency "activerecord", ">= 6.0.0"
|
26
26
|
|
27
|
-
gem.add_development_dependency "bundler", ">=
|
27
|
+
gem.add_development_dependency "bundler", ">= 2.3.3"
|
28
28
|
gem.add_development_dependency "rake"
|
29
29
|
end
|
data/spec/association_spec.rb
CHANGED
@@ -113,6 +113,7 @@ describe LookupBy::Association do
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
# spec/internal/lib/missing.rb
|
116
117
|
context "Missing.lookup_for :city" do
|
117
118
|
it "does not raise foreign key error when table hasn't been created" do
|
118
119
|
expect { require "missing" }.to_not raise_error
|
data/spec/cache_spec.rb
CHANGED
@@ -9,6 +9,11 @@ describe LookupBy::Cache do
|
|
9
9
|
expect { subject.clear }.to change(subject.cache, :size).from(1).to(0)
|
10
10
|
expect { subject.clear }.not_to change(subject.cache, :size)
|
11
11
|
end
|
12
|
+
|
13
|
+
it "clears the reverse cache" do
|
14
|
+
expect { subject.clear }.to change(subject.reverse, :size).from(1).to(0)
|
15
|
+
expect { subject.clear }.not_to change(subject.reverse, :size)
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
19
|
describe "#reload" do
|
@@ -18,6 +23,11 @@ describe LookupBy::Cache do
|
|
18
23
|
expect { subject.reload }.to change(subject.cache, :size).from(0).to(1)
|
19
24
|
expect { subject.reload }.not_to change(subject.cache, :size)
|
20
25
|
end
|
26
|
+
|
27
|
+
it "loads the reverse cache" do
|
28
|
+
expect { subject.reload }.to change(subject.reverse, :size).from(0).to(1)
|
29
|
+
expect { subject.reload }.not_to change(subject.reverse, :size)
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
33
|
describe "#disable!" do
|