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.
Files changed (74) hide show
  1. checksums.yaml +5 -5
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +17 -9
  4. data/Appraisals +6 -6
  5. data/CHANGELOG.md +58 -0
  6. data/Gemfile +6 -10
  7. data/MIT-LICENSE +1 -1
  8. data/README.md +30 -27
  9. data/Rakefile +20 -2
  10. data/circle.yml +4 -2
  11. data/gemfiles/rails_6.0.gemfile +17 -0
  12. data/gemfiles/rails_6.1.gemfile +17 -0
  13. data/gemfiles/rails_7.0.gemfile +17 -0
  14. data/lib/lookup_by/association.rb +9 -3
  15. data/lib/lookup_by/cache.rb +17 -21
  16. data/lib/lookup_by/caching/safe_lru.rb +1 -1
  17. data/lib/lookup_by/hooks/formtastic.rb +2 -1
  18. data/lib/lookup_by/hooks/simple_form.rb +2 -1
  19. data/lib/lookup_by/lookup.rb +27 -13
  20. data/lib/lookup_by/version.rb +1 -1
  21. data/lib/lookup_by.rb +2 -0
  22. data/lookup_by.gemspec +2 -2
  23. data/spec/association_spec.rb +1 -0
  24. data/spec/cache_spec.rb +10 -0
  25. data/spec/{dummy → internal}/app/models/account.rb +0 -0
  26. data/spec/{dummy → internal}/app/models/address.rb +0 -0
  27. data/spec/{dummy → internal}/app/models/city.rb +0 -0
  28. data/spec/{dummy → internal}/app/models/country.rb +0 -0
  29. data/spec/{dummy → internal}/app/models/email_address.rb +0 -0
  30. data/spec/{dummy → internal}/app/models/ip_address.rb +0 -0
  31. data/spec/{dummy → internal}/app/models/path.rb +0 -0
  32. data/spec/{dummy → internal}/app/models/phone_number.rb +0 -0
  33. data/spec/{dummy → internal}/app/models/postal_code.rb +0 -0
  34. data/spec/{dummy → internal}/app/models/raisin.rb +0 -0
  35. data/spec/{dummy → internal}/app/models/read_through_raisin.rb +0 -0
  36. data/spec/{dummy → internal}/app/models/state.rb +0 -0
  37. data/spec/{dummy → internal}/app/models/status.rb +0 -0
  38. data/spec/{dummy → internal}/app/models/street.rb +0 -0
  39. data/spec/{dummy → internal}/app/models/uncacheable.rb +0 -0
  40. data/spec/{dummy → internal}/app/models/unfindable.rb +0 -0
  41. data/spec/{dummy → internal}/app/models/unsynchronizable.rb +0 -0
  42. data/spec/{dummy → internal}/app/models/user_agent.rb +0 -0
  43. data/spec/internal/config/database.yml +8 -0
  44. data/spec/internal/db/schema.rb +49 -0
  45. data/spec/{dummy → internal}/lib/missing.rb +0 -0
  46. data/spec/internal/log/.gitignore +1 -0
  47. data/spec/lookup_by_spec.rb +22 -6
  48. data/spec/rails_helper.rb +6 -13
  49. data/spec/support/shared_examples_for_a_lookup.rb +28 -7
  50. metadata +58 -94
  51. data/gemfiles/rails_4.0.gemfile +0 -22
  52. data/gemfiles/rails_4.1.gemfile +0 -22
  53. data/gemfiles/rails_4.2.gemfile +0 -22
  54. data/spec/dummy/Rakefile +0 -22
  55. data/spec/dummy/app/models/.gitkeep +0 -0
  56. data/spec/dummy/bin/bundle +0 -3
  57. data/spec/dummy/bin/rails +0 -4
  58. data/spec/dummy/bin/rake +0 -4
  59. data/spec/dummy/config/application.rb +0 -28
  60. data/spec/dummy/config/boot.rb +0 -6
  61. data/spec/dummy/config/database.yml +0 -44
  62. data/spec/dummy/config/environment.rb +0 -5
  63. data/spec/dummy/config/environments/development.rb +0 -20
  64. data/spec/dummy/config/environments/test.rb +0 -19
  65. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  66. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  67. data/spec/dummy/config/initializers/inflections.rb +0 -16
  68. data/spec/dummy/config/locales/en.yml +0 -23
  69. data/spec/dummy/config.ru +0 -4
  70. data/spec/dummy/db/migrate/20121019040009_create_tables.rb +0 -41
  71. data/spec/dummy/db/seeds.rb +0 -1
  72. data/spec/dummy/db/structure.sql +0 -1053
  73. data/spec/dummy/log/.gitkeep +0 -0
  74. data/spec/dummy/script/rails +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 355a248ab95bea93cf3a8699d345152225be7c4f
4
- data.tar.gz: ce156dc5c175049034b04de0e4cee2ba4a080aea
2
+ SHA256:
3
+ metadata.gz: 4c1d5aaec4ced07731c227dfb1ae27903fed94b0feb28cecb6f1e3a27e55eb3c
4
+ data.tar.gz: a7185879dbfc9bff84bf7b8795913731f577254925c760e9bc096bc4091493e2
5
5
  SHA512:
6
- metadata.gz: 30288404d1e8b43cb885caaeb83a71ac6cb8d557fecce56cb54ee76cc5c2e9372cc1a525384032d411bf61b1c75b228cd1f1cd41cb11f6c3c7d8422be3378cdc
7
- data.tar.gz: 7186d2cfa8894e8f20ebd299eaf3f558f52efb5590b161859160fd8601e6f8381abfb3521f8cb39bc7c3243afed8f65922bcc1d06c0b5c3a12afebed2da9cbea
6
+ metadata.gz: abfa6b7b5053fd41e61f419c873a02a05b833ae96f104101858f86331d96d7f598b7d09d4b3812637a964ab23ce89c1ead6137ca0d11220b93af592af7197aa9
7
+ data.tar.gz: 067763d23e4a3e6fa1dfa9b0e27e3fd90bc7f69710a7dd129a2fa00a26131af2632eabe0b208ac9733440fe89240285d273600c787afe235d21a8788bd13a59f
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.3.0
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
- - 1.9.3-p551
4
- - 2.0.0
5
- - 2.1.8
6
- - 2.2.4
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
- before_script:
12
- - psql -c 'create database lookup_by_test' -U postgres
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-4.0' do
2
- gem 'rails', '~> 4.0.0'
1
+ appraise 'rails-6.0' do
2
+ gem 'activerecord', '~> 6.0.0'
3
3
  end
4
4
 
5
- appraise 'rails-4.1' do
6
- gem 'rails', '~> 4.1.0'
5
+ appraise 'rails-6.1' do
6
+ gem 'activerecord', '~> 6.1.0'
7
7
  end
8
8
 
9
- appraise 'rails-4.2' do
10
- gem 'rails', '~> 4.2.0'
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', '~> 2.1.0', require: false
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 "pry", require: false
22
- gem 'colored', require: false
19
+ gem "simplecov", "~> 0.21.2", require: false
23
20
 
24
- gem 'racc'
25
- gem 'json'
21
+ gem "pry", require: false
26
22
  end
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright © 2014 Erik Peterson
1
+ Copyright © 2014–2020 Erik Peterson
2
2
 
3
3
  MIT License
4
4
 
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
- # Overview
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 recently used (LRU))
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 4.0+ (_tested on Rails 4.0, 4.1, and 4.2_)
29
- * Ruby 1.9.3+ (_tested on Ruby 1.9.3, 2.0, 2.1, 2.2, 2.3 and Rubinius 3.19_)
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, **_do not_** use the public issue tracker. Instead, send an email to: thecompanygardener@gmail.com._
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 (with LRU eviction)
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
- # Run through the your attribute's setter
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
- Force the LRU to be threadsafe (used to test the SafeLRU).
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
- # Safe
315
- # Use threadsafe cache
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
- ## Testing
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
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
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
@@ -2,8 +2,10 @@ dependencies:
2
2
  post:
3
3
  - bundle exec appraisal install
4
4
 
5
+ database:
6
+ override:
7
+ - echo "Skipping default"
8
+
5
9
  test:
6
- pre:
7
- - bundle exec rake app:db:test:prepare
8
10
  override:
9
11
  - bundle exec appraisal rake
@@ -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
- strict = options[:strict]
84
- strict = true if strict.nil?
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}
@@ -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[:safe] || concurrent?
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 reload
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
- created = @klass.create(*args, &block)
72
-
73
- cache_write(created) if cache?
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
- created = @klass.create!(*args, &block)
80
-
81
- cache_write(created) if cache?
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
@@ -1,4 +1,4 @@
1
- # TODO: Evaluate using ThreadSafe::Hash and ThreadSafe::Array.
1
+ # @todo: Evaluate using ThreadSafe::Hash and ThreadSafe::Array.
2
2
 
3
3
  require "mutex_m"
4
4
 
@@ -6,7 +6,8 @@ module LookupBy
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- alias_method_chain :input, :lookup
9
+ alias_method :input_without_lookup, :input
10
+ alias_method :input, :input_with_lookup
10
11
  end
11
12
 
12
13
  def input_with_lookup(method, options = {})
@@ -6,7 +6,8 @@ module LookupBy
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- alias_method_chain :input, :lookup
9
+ alias_method :input_without_lookup, :input
10
+ alias_method :input, :input_with_lookup
10
11
  end
11
12
 
12
13
  def input_with_lookup(method, options = {}, &block)
@@ -55,7 +55,7 @@ module LookupBy
55
55
  end
56
56
 
57
57
  @lookup = Cache.new(self, options.merge(field: field))
58
- @lookup.reload
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
- # TODO: Rails 4 needs to return a proxy object here
67
- def all(*args)
68
- return super if Rails::VERSION::MAJOR >= 4
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
- @lookup.cache.values
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
- def count(column_name = nil, options = {})
77
- return super if @lookup.read_through?
78
- return super if @lookup.disabled?
79
- return super if column_name
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
- @lookup.cache.size
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
@@ -1,3 +1,3 @@
1
1
  module LookupBy
2
- VERSION = "0.10.9"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/lookup_by.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "lookup_by/version"
2
2
  require "lookup_by/railtie" if defined? Rails
3
3
 
4
+ require 'active_support'
5
+
4
6
  module LookupBy
5
7
  class Error < StandardError; end
6
8
 
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", ">= 4.0.0"
25
+ gem.add_dependency "activerecord", ">= 6.0.0"
26
26
 
27
- gem.add_development_dependency "bundler", ">= 1.7.0"
27
+ gem.add_development_dependency "bundler", ">= 2.3.3"
28
28
  gem.add_development_dependency "rake"
29
29
  end
@@ -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