polymorphic_integer_type 2.2.5 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +1 -0
- data/.github/workflows/ci.yml +34 -0
- data/.github/workflows/gem-push.yml +30 -0
- data/README.md +9 -11
- data/gemfiles/Gemfile.rails-6.0-stable +7 -0
- data/gemfiles/Gemfile.rails-6.1-stable +7 -0
- data/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb +27 -8
- data/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb +19 -0
- data/lib/polymorphic_integer_type/extensions.rb +7 -30
- data/lib/polymorphic_integer_type/mapping.rb +1 -1
- data/lib/polymorphic_integer_type/module_generator.rb +34 -0
- data/lib/polymorphic_integer_type/version.rb +1 -1
- data/lib/polymorphic_integer_type.rb +4 -11
- data/polymorphic_integer_type.gemspec +2 -2
- data/spec/polymorphic_integer_type_spec.rb +75 -16
- data/spec/spec_helper.rb +17 -2
- data/spec/support/configuration.rb +2 -2
- data/spec/support/link.rb +0 -8
- data/spec/support/migrations/1_create_link_table.rb +1 -1
- data/spec/support/migrations/2_create_animal_table.rb +1 -1
- data/spec/support/migrations/3_create_person_table.rb +1 -1
- data/spec/support/migrations/4_create_food_table.rb +1 -1
- data/spec/support/migrations/5_create_drink_table.rb +1 -1
- data/spec/support/migrations/6_create_plant_table.rb +17 -0
- data/spec/support/migrations/7_create_activity_table.rb +15 -0
- data/spec/support/namespaced_activity.rb +11 -0
- data/spec/support/namespaced_plant.rb +11 -0
- metadata +23 -15
- data/gemfiles/Gemfile.rails-4.2-stable +0 -8
- data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -68
- data/gemfiles/Gemfile.rails-5.0-stable.lock +0 -66
- data/gemfiles/Gemfile.rails-5.1-stable.lock +0 -66
- data/gemfiles/Gemfile.rails-5.2-stable.lock +0 -66
- data/lib/polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension.rb +0 -10
- data/lib/polymorphic_integer_type/activerecord_4/predicate_builder_extension.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb3b9a7f294ca84e3f4090aeb66dbd11046cb721e58b51b762d3fac2a77f90e3
|
4
|
+
data.tar.gz: 4b9ed1f6e1c36d98a573932bd747780aae00032e238ab8cf7385d6d12d6fb0a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a7cb4348a7be18c626d9a611a6005b991b6a09c016c0dd3908e10326da0bc8495072dfcf16ce998a55fce3c8f7bfbce34709df9b3882cceb01dbfe2ff274dc1
|
7
|
+
data.tar.gz: 1ddd7ed8095268053c37092ac0f1c8ebf73fee6111dc51bdda027dd4a2e3c4f23d92402ee60724c04f9d7737b06c8a022712683dc23503adf0f2c35b30090159
|
data/.github/CODEOWNERS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/.github/workflows/ @clio/application-security @clio/penguins
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
branches: [master]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
strategy:
|
14
|
+
fail-fast: false
|
15
|
+
matrix:
|
16
|
+
gemfile:
|
17
|
+
- Gemfile.rails-5.0-stable
|
18
|
+
- Gemfile.rails-5.1-stable
|
19
|
+
- Gemfile.rails-5.2-stable
|
20
|
+
- Gemfile.rails-6.0-stable
|
21
|
+
- Gemfile.rails-6.1-stable
|
22
|
+
env:
|
23
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby
|
27
|
+
uses: ruby/setup-ruby@v1
|
28
|
+
with:
|
29
|
+
ruby-version: "2.6"
|
30
|
+
- name: Install dependencies
|
31
|
+
run: bundle install
|
32
|
+
- name: Run tests
|
33
|
+
run:
|
34
|
+
bundle exec rspec
|
@@ -0,0 +1,30 @@
|
|
1
|
+
name: Ruby Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
release:
|
5
|
+
types: [ published ]
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
build:
|
9
|
+
name: Build + Publish
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
permissions:
|
12
|
+
contents: read
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby 2.7
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: 2.7.x
|
20
|
+
|
21
|
+
- name: Publish to RubyGems
|
22
|
+
env:
|
23
|
+
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
|
24
|
+
run: |
|
25
|
+
mkdir -p $HOME/.gem
|
26
|
+
touch $HOME/.gem/credentials
|
27
|
+
chmod 0600 $HOME/.gem/credentials
|
28
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
29
|
+
gem build *.gemspec
|
30
|
+
gem push *.gem
|
data/README.md
CHANGED
@@ -6,11 +6,11 @@ Rails' polymorphic associations are pretty useful. The example they give to set
|
|
6
6
|
class Picture < ActiveRecord::Base
|
7
7
|
belongs_to :imageable, polymorphic: true
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
class Employee < ActiveRecord::Base
|
11
11
|
has_many :pictures, as: :imageable
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
class Product < ActiveRecord::Base
|
15
15
|
has_many :pictures, as: :imageable
|
16
16
|
end
|
@@ -31,7 +31,7 @@ class CreatePictures < ActiveRecord::Migration
|
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
|
-
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
|
34
|
+
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
|
35
35
|
|
36
36
|
## Installation
|
37
37
|
|
@@ -47,8 +47,6 @@ Or install it yourself as:
|
|
47
47
|
|
48
48
|
$ gem install polymorphic_integer_type
|
49
49
|
|
50
|
-
For Rails 3.2 use version < 2. Version >= 2 has been tested on Rails 4.2 and Ruby 2.1
|
51
|
-
|
52
50
|
## Usage
|
53
51
|
|
54
52
|
For the model where the `belongs_to` is defined, include `PolymorphicIntegerType::Extensions` and set the `polymorphic:` option to a hash that maps an integer stored in the database to the name of a Ruby class.
|
@@ -60,7 +58,7 @@ class Picture < ActiveRecord::Base
|
|
60
58
|
belongs_to :imageable, polymorphic: {1 => "Employee", 2 => "Product"}
|
61
59
|
end
|
62
60
|
```
|
63
|
-
|
61
|
+
|
64
62
|
Next, include `PolymorphicIntegerType::Extensions` into any of the models that point back to the polymorphic integer type association (e.g., `Picture#imageable`) and add a [polymorphic association using `as:`](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations).
|
65
63
|
|
66
64
|
```ruby
|
@@ -69,7 +67,7 @@ class Employee < ActiveRecord::Base
|
|
69
67
|
|
70
68
|
has_many :pictures, as: :imageable
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
class Product < ActiveRecord::Base
|
74
72
|
include PolymorphicIntegerType::Extensions
|
75
73
|
|
@@ -84,10 +82,10 @@ You can also store polymorphic type mappings separate from your models. This sho
|
|
84
82
|
```ruby
|
85
83
|
PolymorphicIntegerType::Mapping.configuration do |config|
|
86
84
|
config.add :imageable, {1 => "Employee", 2 => "Product" }
|
87
|
-
end
|
85
|
+
end
|
88
86
|
```
|
89
87
|
|
90
|
-
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
|
88
|
+
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
|
91
89
|
|
92
90
|
### Migrating an existing association
|
93
91
|
|
@@ -95,14 +93,14 @@ If you want to convert a polymorphic association that is already a string, you'l
|
|
95
93
|
|
96
94
|
```ruby
|
97
95
|
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
|
98
|
-
|
96
|
+
|
99
97
|
def up
|
100
98
|
change_table :pictures do |t|
|
101
99
|
t.integer :new_imageable_type
|
102
100
|
end
|
103
101
|
|
104
102
|
execute <<-SQL
|
105
|
-
UPDATE
|
103
|
+
UPDATE picture
|
106
104
|
SET new_imageable_type = CASE imageable_type
|
107
105
|
WHEN 'Employee' THEN 1
|
108
106
|
WHEN 'Product' THEN 2
|
@@ -1,17 +1,36 @@
|
|
1
1
|
module PolymorphicIntegerType
|
2
2
|
module PolymorphicArrayValueExtension
|
3
|
+
|
4
|
+
# original method:
|
5
|
+
# def type_to_ids_mapping
|
6
|
+
# default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
7
|
+
# result = values.each_with_object(default_hash) do |value, hash|
|
8
|
+
# hash[klass(value).polymorphic_name] << convert_to_id(value)
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
|
3
12
|
def type_to_ids_mapping
|
4
|
-
|
13
|
+
if ACTIVE_RECORD_VERSION < Gem::Version.new("6.1")
|
5
14
|
association = @associated_table.send(:association)
|
6
|
-
|
7
|
-
|
15
|
+
else
|
16
|
+
association = @associated_table.send(:reflection)
|
17
|
+
end
|
18
|
+
|
19
|
+
name = association.name
|
20
|
+
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
21
|
+
values.each_with_object(default_hash) do |value, hash|
|
22
|
+
klass = respond_to?(:klass, true) ? klass(value) : value.class
|
23
|
+
if association.active_record.respond_to?("#{name}_type_mapping")
|
24
|
+
mapping = association.active_record.send("#{name}_type_mapping")
|
25
|
+
key ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
|
26
|
+
key ||= mapping.key(klass.sti_name)
|
27
|
+
key ||= mapping.key(klass.base_class.to_s)
|
28
|
+
key ||= mapping.key(klass.base_class.sti_name)
|
8
29
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
30
|
+
hash[key] << convert_to_id(value)
|
31
|
+
else
|
32
|
+
hash[klass.polymorphic_name] << convert_to_id(value)
|
13
33
|
end
|
14
|
-
result
|
15
34
|
end
|
16
35
|
end
|
17
36
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Associations
|
3
|
+
class BelongsToPolymorphicAssociation < BelongsToAssociation
|
4
|
+
private
|
5
|
+
|
6
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
|
7
|
+
def replace_keys(record)
|
8
|
+
super
|
9
|
+
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
|
10
|
+
end
|
11
|
+
elsif
|
12
|
+
def replace_keys(record, force: false)
|
13
|
+
super
|
14
|
+
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -20,35 +20,11 @@ module PolymorphicIntegerType
|
|
20
20
|
_polymorphic_foreign_types << foreign_type
|
21
21
|
|
22
22
|
# Required way to dynamically define a class method on the model
|
23
|
-
|
23
|
+
define_singleton_method("#{foreign_type}_mapping") do
|
24
24
|
mapping
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
define_method foreign_type do
|
29
|
-
t = super()
|
30
|
-
self.class.send("#{foreign_type}_mapping")[t]
|
31
|
-
end
|
32
|
-
|
33
|
-
define_method "#{foreign_type}=" do |klass|
|
34
|
-
mapping = self.class.send("#{foreign_type}_mapping")
|
35
|
-
enum = mapping.key(klass.to_s)
|
36
|
-
if klass.kind_of?(Class) && klass <= ActiveRecord::Base
|
37
|
-
enum ||= mapping.key(klass.sti_name)
|
38
|
-
enum ||= mapping.key(klass.base_class.to_s)
|
39
|
-
enum ||= mapping.key(klass.base_class.sti_name)
|
40
|
-
end
|
41
|
-
enum ||= klass if klass != NilClass
|
42
|
-
super(enum)
|
43
|
-
end
|
44
|
-
|
45
|
-
define_method "#{name}=" do |record|
|
46
|
-
super(record)
|
47
|
-
send("#{foreign_type}=", record.class)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
include(foreign_type_extension)
|
27
|
+
ModuleGenerator.generate_and_include(self, foreign_type, name)
|
52
28
|
|
53
29
|
validate do
|
54
30
|
t = send(foreign_type)
|
@@ -69,14 +45,15 @@ module PolymorphicIntegerType
|
|
69
45
|
if options[:as] && (polymorphic_type_mapping || integer_type)
|
70
46
|
poly_type = options.delete(:as)
|
71
47
|
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
|
72
|
-
if polymorphic_type_mapping
|
48
|
+
if polymorphic_type_mapping.nil?
|
73
49
|
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
|
74
50
|
end
|
75
51
|
|
76
|
-
klass_mapping =
|
52
|
+
klass_mapping = polymorphic_type_mapping.key(polymorphic_name) if respond_to?(:polymorphic_name)
|
53
|
+
klass_mapping ||= polymorphic_type_mapping.key(sti_name)
|
77
54
|
|
78
|
-
if klass_mapping
|
79
|
-
raise "Class not found for #{
|
55
|
+
if klass_mapping.nil?
|
56
|
+
raise "Class not found for #{inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
|
80
57
|
end
|
81
58
|
|
82
59
|
options[:foreign_key] ||= "#{poly_type}_id"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module PolymorphicIntegerType
|
2
|
+
class ModuleGenerator
|
3
|
+
def self.generate_and_include(klass,foreign_type, name)
|
4
|
+
foreign_type_extension = Module.new do
|
5
|
+
define_method foreign_type do
|
6
|
+
t = super()
|
7
|
+
self.class.send("#{foreign_type}_mapping")[t]
|
8
|
+
end
|
9
|
+
|
10
|
+
define_method "#{foreign_type}=" do |klass|
|
11
|
+
mapping = self.class.send("#{foreign_type}_mapping")
|
12
|
+
enum = mapping.key(klass.to_s)
|
13
|
+
if klass.kind_of?(Class) && klass <= ActiveRecord::Base
|
14
|
+
enum ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
|
15
|
+
enum ||= mapping.key(klass.sti_name)
|
16
|
+
enum ||= mapping.key(klass.base_class.to_s)
|
17
|
+
enum ||= mapping.key(klass.base_class.sti_name)
|
18
|
+
end
|
19
|
+
enum ||= klass if klass != NilClass
|
20
|
+
super(enum)
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method "#{name}=" do |record|
|
24
|
+
super(record)
|
25
|
+
send("#{foreign_type}=", record.class)
|
26
|
+
association(name).loaded!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
klass.include(foreign_type_extension)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -3,19 +3,12 @@ ACTIVE_RECORD_VERSION = Gem::Version.new(ActiveRecord::VERSION::STRING)
|
|
3
3
|
require "polymorphic_integer_type/version"
|
4
4
|
require "polymorphic_integer_type/extensions"
|
5
5
|
require "polymorphic_integer_type/mapping"
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
else
|
10
|
-
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
|
11
|
-
end
|
12
|
-
|
13
|
-
if ACTIVE_RECORD_VERSION >= Gem::Version.new("5.0") && ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
14
|
-
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
|
15
|
-
end
|
6
|
+
require "polymorphic_integer_type/module_generator"
|
7
|
+
require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
|
8
|
+
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
|
16
9
|
|
17
10
|
if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
18
|
-
require "polymorphic_integer_type/
|
11
|
+
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
|
19
12
|
end
|
20
13
|
|
21
14
|
module PolymorphicIntegerType; end
|
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activerecord"
|
21
|
+
spec.add_dependency "activerecord", "< 7"
|
22
22
|
spec.add_development_dependency "bundler"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
spec.add_development_dependency "rspec"
|
25
25
|
spec.add_development_dependency "sqlite3"
|
26
|
-
spec.add_development_dependency "byebug"
|
26
|
+
spec.add_development_dependency "pry-byebug"
|
27
27
|
end
|
@@ -14,6 +14,50 @@ describe PolymorphicIntegerType do
|
|
14
14
|
|
15
15
|
let(:link) { Link.create(source: source, target: target) }
|
16
16
|
|
17
|
+
|
18
|
+
context "when creating associations" do
|
19
|
+
it "sets the source_type" do
|
20
|
+
link = dog.source_links.new
|
21
|
+
expect(link.source_type).to eq("Animal")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets the target_type" do
|
25
|
+
link = kibble.target_links.new
|
26
|
+
expect(link.target_type).to eq("Food")
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when models are namespaced" do
|
30
|
+
context "and mappings include namespaces" do
|
31
|
+
it "sets the source_type" do
|
32
|
+
allow(Link).to receive(:source_type_mapping).and_return({3 => "Namespaced::Plant"})
|
33
|
+
allow(Link).to receive(:source_type_mapping2).and_return({3 => "Namespaced::Plant"})
|
34
|
+
|
35
|
+
link = Namespaced::Plant.create(name: "Oak").source_links.new
|
36
|
+
expect(link.source_type).to eq("Namespaced::Plant")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sets the target_type" do
|
40
|
+
allow(Link).to receive(:target_type_mapping).and_return({3 => "Namespaced::Activity"})
|
41
|
+
link = Namespaced::Activity.create(name: "swaying").target_links.new
|
42
|
+
expect(link.target_type).to eq("Namespaced::Activity")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "and mappings don't include namespaces" do
|
47
|
+
it "sets the source type" do
|
48
|
+
Link.source_type_mapping
|
49
|
+
link = Namespaced::Plant.create(name: "Oak").source_links.new
|
50
|
+
expect(link.source_type).to eq("Plant")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "sets the target type" do
|
54
|
+
link = Namespaced::Activity.create(name:"swaying").target_links.new
|
55
|
+
expect(link.target_type).to eq("Activity")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
17
61
|
context "when the source is nil" do
|
18
62
|
let(:source) { nil }
|
19
63
|
let(:target) { nil }
|
@@ -25,6 +69,25 @@ describe PolymorphicIntegerType do
|
|
25
69
|
end
|
26
70
|
|
27
71
|
context "when the source is a class that modifies the sti_name or polymorphic_name" do
|
72
|
+
context "and we leverage the polymorphic_name" do
|
73
|
+
before do
|
74
|
+
allow(PolymorphicIntegerType).to receive(:use_polymorphic_name).and_return(true)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "properly sets the source_type to the modified class name" do
|
78
|
+
link = Link.new(source: Namespaced::Animal.new)
|
79
|
+
expect(link.source_type).to eql "Animal"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "can read dirty attributes from an associated object" do
|
83
|
+
animal = Namespaced::Animal.create!(name: "Oldie")
|
84
|
+
animal.name = "Newton"
|
85
|
+
link = Link.create!(source: animal)
|
86
|
+
|
87
|
+
expect(link.source.name).to eq("Newton")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
28
91
|
it "properly sets the source_type to the modified class name" do
|
29
92
|
link = Link.new(source: Namespaced::Animal.new)
|
30
93
|
expect(link.source_type).to eql "Animal"
|
@@ -54,6 +117,15 @@ describe PolymorphicIntegerType do
|
|
54
117
|
it "properly finds the object with a find_by" do
|
55
118
|
expect(Link.find_by(source: source, id: link.id)).to eql link
|
56
119
|
end
|
120
|
+
|
121
|
+
context "when source and target are namespaced without modifying polymorphic_name" do
|
122
|
+
it "properly finds the object" do
|
123
|
+
plant = Namespaced::Plant.create(name: "Mighty", kind: "Oak", owner: owner)
|
124
|
+
activity = Namespaced::Activity.create(name: "swaying")
|
125
|
+
link = Link.create(source: plant, target: activity)
|
126
|
+
expect(Link.where(source: plant, id: link.id).first).to eql link
|
127
|
+
end
|
128
|
+
end
|
57
129
|
end
|
58
130
|
|
59
131
|
shared_examples "proper source" do
|
@@ -84,7 +156,6 @@ describe PolymorphicIntegerType do
|
|
84
156
|
expect(source.source_links[0].source).to eql source
|
85
157
|
end
|
86
158
|
end
|
87
|
-
|
88
159
|
end
|
89
160
|
context "When a link is given polymorphic record" do
|
90
161
|
let(:link) { Link.create(source: source) }
|
@@ -93,13 +164,11 @@ describe PolymorphicIntegerType do
|
|
93
164
|
|
94
165
|
context "and when it already has a polymorphic record" do
|
95
166
|
let(:target) { kibble }
|
96
|
-
before { link.
|
167
|
+
before { link.update(target: target) }
|
97
168
|
|
98
169
|
include_examples "proper source"
|
99
170
|
include_examples "proper target"
|
100
|
-
|
101
171
|
end
|
102
|
-
|
103
172
|
end
|
104
173
|
|
105
174
|
context "When a link is given polymorphic id and type" do
|
@@ -109,12 +178,10 @@ describe PolymorphicIntegerType do
|
|
109
178
|
|
110
179
|
context "and when it already has a polymorphic id and type" do
|
111
180
|
let(:target) { kibble }
|
112
|
-
before { link.
|
181
|
+
before { link.update(target_id: target.id, target_type: target.class.to_s) }
|
113
182
|
include_examples "proper source"
|
114
183
|
include_examples "proper target"
|
115
|
-
|
116
184
|
end
|
117
|
-
|
118
185
|
end
|
119
186
|
|
120
187
|
context "When using a relation to the links with eager loading" do
|
@@ -127,9 +194,7 @@ describe PolymorphicIntegerType do
|
|
127
194
|
it "should be able to return the links and the targets" do
|
128
195
|
expect(cat.source_links).to match_array links
|
129
196
|
expect(cat.source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
|
130
|
-
|
131
197
|
end
|
132
|
-
|
133
198
|
end
|
134
199
|
|
135
200
|
context "When using a through relation to the links with eager loading" do
|
@@ -142,9 +207,7 @@ describe PolymorphicIntegerType do
|
|
142
207
|
it "should be able to return the links and the targets" do
|
143
208
|
expect(owner.pet_source_links).to match_array links
|
144
209
|
expect(owner.pet_source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
|
145
|
-
|
146
210
|
end
|
147
|
-
|
148
211
|
end
|
149
212
|
|
150
213
|
context "When eager loading the polymorphic association" do
|
@@ -159,16 +222,12 @@ describe PolymorphicIntegerType do
|
|
159
222
|
expect(links.first.source).to eql cat
|
160
223
|
expect(links.last.source).to eql dog
|
161
224
|
end
|
162
|
-
|
163
225
|
end
|
164
226
|
|
165
227
|
it "should be able to preload the association" do
|
166
228
|
l = Link.includes(:source).where(id: link.id).first
|
167
229
|
expect(l.source).to eql cat
|
168
230
|
end
|
169
|
-
|
170
|
-
|
171
|
-
|
172
231
|
end
|
173
232
|
|
174
233
|
context "when the association is an STI table" do
|
@@ -220,7 +279,7 @@ describe PolymorphicIntegerType do
|
|
220
279
|
include_examples "proper target"
|
221
280
|
|
222
281
|
it "creates foreign_type mapping method" do
|
223
|
-
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"})
|
282
|
+
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal", 3 => "Plant"})
|
224
283
|
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
|
225
284
|
end
|
226
285
|
|
data/spec/spec_helper.rb
CHANGED
@@ -5,18 +5,33 @@ require 'support/configuration'
|
|
5
5
|
require 'support/link'
|
6
6
|
require 'support/animal'
|
7
7
|
require 'support/namespaced_animal'
|
8
|
+
require 'support/namespaced_plant'
|
8
9
|
require 'support/dog'
|
9
10
|
require 'support/person'
|
10
11
|
require 'support/food'
|
11
12
|
require 'support/drink'
|
13
|
+
require 'support/namespaced_activity'
|
12
14
|
require 'byebug'
|
15
|
+
require 'pry'
|
13
16
|
|
14
17
|
RSpec.configure do |config|
|
15
18
|
config.before(:suite) do
|
16
19
|
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
|
20
|
+
migrations_path = "#{File.dirname(__FILE__)}/support/migrations"
|
21
|
+
active_record_version = Gem::Version.new(ActiveRecord::VERSION::STRING)
|
22
|
+
|
17
23
|
ActiveRecord::Base.establish_connection(database_config)
|
18
|
-
|
19
|
-
|
24
|
+
|
25
|
+
if active_record_version < Gem::Version.new("5.2")
|
26
|
+
ActiveRecord::Migrator.migrate(migrations_path)
|
27
|
+
end
|
28
|
+
|
29
|
+
if active_record_version >= Gem::Version.new("5.2") && active_record_version < Gem::Version.new("6.0")
|
30
|
+
ActiveRecord::MigrationContext.new(migrations_path).migrate
|
31
|
+
end
|
32
|
+
|
33
|
+
if active_record_version >= Gem::Version.new("6.0")
|
34
|
+
ActiveRecord::MigrationContext.new(migrations_path, ActiveRecord::SchemaMigration).migrate
|
20
35
|
end
|
21
36
|
end
|
22
37
|
|
@@ -1,4 +1,4 @@
|
|
1
1
|
PolymorphicIntegerType::Mapping.configuration do |config|
|
2
|
-
config.add :source, {1 => "Person", 2 => "Animal"}
|
3
|
-
config.add :target, {1 => "Food", 2 => "Drink"}
|
2
|
+
config.add :source, {1 => "Person", 2 => "Animal", 3 => "Plant"}
|
3
|
+
config.add :target, {1 => "Food", 2 => "Drink", 3 => "Activity"}
|
4
4
|
end
|
data/spec/support/link.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Namespaced
|
2
|
+
class Activity < ActiveRecord::Base
|
3
|
+
include PolymorphicIntegerType::Extensions
|
4
|
+
|
5
|
+
self.store_full_sti_class = false
|
6
|
+
self.table_name = "activities"
|
7
|
+
|
8
|
+
has_many :target_links, as: :target, inverse_of: :target, integer_type: true, class_name: "Link"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Namespaced
|
2
|
+
class Plant < ActiveRecord::Base
|
3
|
+
include PolymorphicIntegerType::Extensions
|
4
|
+
|
5
|
+
self.store_full_sti_class = false
|
6
|
+
self.table_name = "plants"
|
7
|
+
|
8
|
+
belongs_to :owner, class_name: "Person"
|
9
|
+
has_many :source_links, as: :source, inverse_of: :source, integer_type: true, class_name: "Link"
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_integer_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle d'Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "<"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name: byebug
|
84
|
+
name: pry-byebug
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -102,27 +102,27 @@ executables:
|
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
|
+
- ".github/CODEOWNERS"
|
106
|
+
- ".github/workflows/ci.yml"
|
107
|
+
- ".github/workflows/gem-push.yml"
|
105
108
|
- ".gitignore"
|
106
109
|
- Gemfile
|
107
110
|
- LICENSE.txt
|
108
111
|
- README.md
|
109
112
|
- Rakefile
|
110
113
|
- bin/setup
|
111
|
-
- gemfiles/Gemfile.rails-4.2-stable
|
112
|
-
- gemfiles/Gemfile.rails-4.2-stable.lock
|
113
114
|
- gemfiles/Gemfile.rails-5.0-stable
|
114
|
-
- gemfiles/Gemfile.rails-5.0-stable.lock
|
115
115
|
- gemfiles/Gemfile.rails-5.1-stable
|
116
|
-
- gemfiles/Gemfile.rails-5.1-stable.lock
|
117
116
|
- gemfiles/Gemfile.rails-5.2-stable
|
118
|
-
- gemfiles/Gemfile.rails-
|
117
|
+
- gemfiles/Gemfile.rails-6.0-stable
|
118
|
+
- gemfiles/Gemfile.rails-6.1-stable
|
119
119
|
- lib/polymorphic_integer_type.rb
|
120
|
-
- lib/polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension.rb
|
121
|
-
- lib/polymorphic_integer_type/activerecord_4/predicate_builder_extension.rb
|
122
120
|
- lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb
|
123
121
|
- lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
|
122
|
+
- lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb
|
124
123
|
- lib/polymorphic_integer_type/extensions.rb
|
125
124
|
- lib/polymorphic_integer_type/mapping.rb
|
125
|
+
- lib/polymorphic_integer_type/module_generator.rb
|
126
126
|
- lib/polymorphic_integer_type/version.rb
|
127
127
|
- polymorphic_integer_type.gemspec
|
128
128
|
- spec/polymorphic_integer_type_spec.rb
|
@@ -139,7 +139,11 @@ files:
|
|
139
139
|
- spec/support/migrations/3_create_person_table.rb
|
140
140
|
- spec/support/migrations/4_create_food_table.rb
|
141
141
|
- spec/support/migrations/5_create_drink_table.rb
|
142
|
+
- spec/support/migrations/6_create_plant_table.rb
|
143
|
+
- spec/support/migrations/7_create_activity_table.rb
|
144
|
+
- spec/support/namespaced_activity.rb
|
142
145
|
- spec/support/namespaced_animal.rb
|
146
|
+
- spec/support/namespaced_plant.rb
|
143
147
|
- spec/support/person.rb
|
144
148
|
homepage: ''
|
145
149
|
licenses:
|
@@ -160,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
164
|
- !ruby/object:Gem::Version
|
161
165
|
version: '0'
|
162
166
|
requirements: []
|
163
|
-
rubygems_version: 3.
|
167
|
+
rubygems_version: 3.1.6
|
164
168
|
signing_key:
|
165
169
|
specification_version: 4
|
166
170
|
summary: Use integers rather than strings for the _type field
|
@@ -179,5 +183,9 @@ test_files:
|
|
179
183
|
- spec/support/migrations/3_create_person_table.rb
|
180
184
|
- spec/support/migrations/4_create_food_table.rb
|
181
185
|
- spec/support/migrations/5_create_drink_table.rb
|
186
|
+
- spec/support/migrations/6_create_plant_table.rb
|
187
|
+
- spec/support/migrations/7_create_activity_table.rb
|
188
|
+
- spec/support/namespaced_activity.rb
|
182
189
|
- spec/support/namespaced_animal.rb
|
190
|
+
- spec/support/namespaced_plant.rb
|
183
191
|
- spec/support/person.rb
|
@@ -1,68 +0,0 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/rails/rails.git
|
3
|
-
revision: e9d6b85f3e834ceea2aeabe4cbaa96a7c73eb896
|
4
|
-
branch: 4-2-stable
|
5
|
-
specs:
|
6
|
-
activemodel (4.2.11.1)
|
7
|
-
activesupport (= 4.2.11.1)
|
8
|
-
builder (~> 3.1)
|
9
|
-
activerecord (4.2.11.1)
|
10
|
-
activemodel (= 4.2.11.1)
|
11
|
-
activesupport (= 4.2.11.1)
|
12
|
-
arel (~> 6.0)
|
13
|
-
activesupport (4.2.11.1)
|
14
|
-
i18n (~> 0.7)
|
15
|
-
minitest (~> 5.1)
|
16
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
17
|
-
tzinfo (~> 1.1)
|
18
|
-
|
19
|
-
PATH
|
20
|
-
remote: ..
|
21
|
-
specs:
|
22
|
-
polymorphic_integer_type (2.2.4)
|
23
|
-
activerecord
|
24
|
-
|
25
|
-
GEM
|
26
|
-
remote: https://rubygems.org/
|
27
|
-
specs:
|
28
|
-
arel (6.0.4)
|
29
|
-
builder (3.2.4)
|
30
|
-
byebug (11.0.1)
|
31
|
-
concurrent-ruby (1.1.5)
|
32
|
-
diff-lcs (1.3)
|
33
|
-
i18n (0.9.5)
|
34
|
-
concurrent-ruby (~> 1.0)
|
35
|
-
minitest (5.13.0)
|
36
|
-
rake (13.0.1)
|
37
|
-
rspec (3.9.0)
|
38
|
-
rspec-core (~> 3.9.0)
|
39
|
-
rspec-expectations (~> 3.9.0)
|
40
|
-
rspec-mocks (~> 3.9.0)
|
41
|
-
rspec-core (3.9.0)
|
42
|
-
rspec-support (~> 3.9.0)
|
43
|
-
rspec-expectations (3.9.0)
|
44
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
-
rspec-support (~> 3.9.0)
|
46
|
-
rspec-mocks (3.9.0)
|
47
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
-
rspec-support (~> 3.9.0)
|
49
|
-
rspec-support (3.9.0)
|
50
|
-
sqlite3 (1.3.13)
|
51
|
-
thread_safe (0.3.6)
|
52
|
-
tzinfo (1.2.5)
|
53
|
-
thread_safe (~> 0.1)
|
54
|
-
|
55
|
-
PLATFORMS
|
56
|
-
ruby
|
57
|
-
|
58
|
-
DEPENDENCIES
|
59
|
-
activerecord!
|
60
|
-
bundler
|
61
|
-
byebug
|
62
|
-
polymorphic_integer_type!
|
63
|
-
rake
|
64
|
-
rspec
|
65
|
-
sqlite3 (~> 1.3.6)
|
66
|
-
|
67
|
-
BUNDLED WITH
|
68
|
-
1.16.1
|
@@ -1,66 +0,0 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/rails/rails.git
|
3
|
-
revision: ac6aa32f7cf66264ba87eabed7c042bb60bcf3a2
|
4
|
-
branch: 5-0-stable
|
5
|
-
specs:
|
6
|
-
activemodel (5.0.7.2)
|
7
|
-
activesupport (= 5.0.7.2)
|
8
|
-
activerecord (5.0.7.2)
|
9
|
-
activemodel (= 5.0.7.2)
|
10
|
-
activesupport (= 5.0.7.2)
|
11
|
-
arel (~> 7.0)
|
12
|
-
activesupport (5.0.7.2)
|
13
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
-
i18n (>= 0.7, < 2)
|
15
|
-
minitest (~> 5.1)
|
16
|
-
tzinfo (~> 1.1)
|
17
|
-
|
18
|
-
PATH
|
19
|
-
remote: ..
|
20
|
-
specs:
|
21
|
-
polymorphic_integer_type (2.2.4)
|
22
|
-
activerecord
|
23
|
-
|
24
|
-
GEM
|
25
|
-
remote: https://rubygems.org/
|
26
|
-
specs:
|
27
|
-
arel (7.1.4)
|
28
|
-
byebug (11.0.1)
|
29
|
-
concurrent-ruby (1.1.5)
|
30
|
-
diff-lcs (1.3)
|
31
|
-
i18n (1.7.0)
|
32
|
-
concurrent-ruby (~> 1.0)
|
33
|
-
minitest (5.13.0)
|
34
|
-
rake (13.0.1)
|
35
|
-
rspec (3.9.0)
|
36
|
-
rspec-core (~> 3.9.0)
|
37
|
-
rspec-expectations (~> 3.9.0)
|
38
|
-
rspec-mocks (~> 3.9.0)
|
39
|
-
rspec-core (3.9.0)
|
40
|
-
rspec-support (~> 3.9.0)
|
41
|
-
rspec-expectations (3.9.0)
|
42
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
-
rspec-support (~> 3.9.0)
|
44
|
-
rspec-mocks (3.9.0)
|
45
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.9.0)
|
47
|
-
rspec-support (3.9.0)
|
48
|
-
sqlite3 (1.3.13)
|
49
|
-
thread_safe (0.3.6)
|
50
|
-
tzinfo (1.2.5)
|
51
|
-
thread_safe (~> 0.1)
|
52
|
-
|
53
|
-
PLATFORMS
|
54
|
-
ruby
|
55
|
-
|
56
|
-
DEPENDENCIES
|
57
|
-
activerecord!
|
58
|
-
bundler
|
59
|
-
byebug
|
60
|
-
polymorphic_integer_type!
|
61
|
-
rake
|
62
|
-
rspec
|
63
|
-
sqlite3 (~> 1.3.6)
|
64
|
-
|
65
|
-
BUNDLED WITH
|
66
|
-
1.16.1
|
@@ -1,66 +0,0 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/rails/rails.git
|
3
|
-
revision: 663206d20aec374a28a24bb43bc7b1233042ed9b
|
4
|
-
branch: 5-1-stable
|
5
|
-
specs:
|
6
|
-
activemodel (5.1.7)
|
7
|
-
activesupport (= 5.1.7)
|
8
|
-
activerecord (5.1.7)
|
9
|
-
activemodel (= 5.1.7)
|
10
|
-
activesupport (= 5.1.7)
|
11
|
-
arel (~> 8.0)
|
12
|
-
activesupport (5.1.7)
|
13
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
-
i18n (>= 0.7, < 2)
|
15
|
-
minitest (~> 5.1)
|
16
|
-
tzinfo (~> 1.1)
|
17
|
-
|
18
|
-
PATH
|
19
|
-
remote: ..
|
20
|
-
specs:
|
21
|
-
polymorphic_integer_type (2.2.4)
|
22
|
-
activerecord
|
23
|
-
|
24
|
-
GEM
|
25
|
-
remote: https://rubygems.org/
|
26
|
-
specs:
|
27
|
-
arel (8.0.0)
|
28
|
-
byebug (11.0.1)
|
29
|
-
concurrent-ruby (1.1.5)
|
30
|
-
diff-lcs (1.3)
|
31
|
-
i18n (1.7.0)
|
32
|
-
concurrent-ruby (~> 1.0)
|
33
|
-
minitest (5.13.0)
|
34
|
-
rake (13.0.1)
|
35
|
-
rspec (3.9.0)
|
36
|
-
rspec-core (~> 3.9.0)
|
37
|
-
rspec-expectations (~> 3.9.0)
|
38
|
-
rspec-mocks (~> 3.9.0)
|
39
|
-
rspec-core (3.9.0)
|
40
|
-
rspec-support (~> 3.9.0)
|
41
|
-
rspec-expectations (3.9.0)
|
42
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
-
rspec-support (~> 3.9.0)
|
44
|
-
rspec-mocks (3.9.0)
|
45
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.9.0)
|
47
|
-
rspec-support (3.9.0)
|
48
|
-
sqlite3 (1.4.2)
|
49
|
-
thread_safe (0.3.6)
|
50
|
-
tzinfo (1.2.5)
|
51
|
-
thread_safe (~> 0.1)
|
52
|
-
|
53
|
-
PLATFORMS
|
54
|
-
ruby
|
55
|
-
|
56
|
-
DEPENDENCIES
|
57
|
-
activerecord!
|
58
|
-
bundler
|
59
|
-
byebug
|
60
|
-
polymorphic_integer_type!
|
61
|
-
rake
|
62
|
-
rspec
|
63
|
-
sqlite3
|
64
|
-
|
65
|
-
BUNDLED WITH
|
66
|
-
1.16.1
|
@@ -1,66 +0,0 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/rails/rails.git
|
3
|
-
revision: 892eab777c418135ce0646e91bc9ebb08a29ab9b
|
4
|
-
branch: 5-2-stable
|
5
|
-
specs:
|
6
|
-
activemodel (5.2.4.1)
|
7
|
-
activesupport (= 5.2.4.1)
|
8
|
-
activerecord (5.2.4.1)
|
9
|
-
activemodel (= 5.2.4.1)
|
10
|
-
activesupport (= 5.2.4.1)
|
11
|
-
arel (>= 9.0)
|
12
|
-
activesupport (5.2.4.1)
|
13
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
-
i18n (>= 0.7, < 2)
|
15
|
-
minitest (~> 5.1)
|
16
|
-
tzinfo (~> 1.1)
|
17
|
-
|
18
|
-
PATH
|
19
|
-
remote: ..
|
20
|
-
specs:
|
21
|
-
polymorphic_integer_type (2.2.4)
|
22
|
-
activerecord
|
23
|
-
|
24
|
-
GEM
|
25
|
-
remote: https://rubygems.org/
|
26
|
-
specs:
|
27
|
-
arel (9.0.0)
|
28
|
-
byebug (11.0.1)
|
29
|
-
concurrent-ruby (1.1.5)
|
30
|
-
diff-lcs (1.3)
|
31
|
-
i18n (1.7.0)
|
32
|
-
concurrent-ruby (~> 1.0)
|
33
|
-
minitest (5.13.0)
|
34
|
-
rake (13.0.1)
|
35
|
-
rspec (3.9.0)
|
36
|
-
rspec-core (~> 3.9.0)
|
37
|
-
rspec-expectations (~> 3.9.0)
|
38
|
-
rspec-mocks (~> 3.9.0)
|
39
|
-
rspec-core (3.9.0)
|
40
|
-
rspec-support (~> 3.9.0)
|
41
|
-
rspec-expectations (3.9.0)
|
42
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
-
rspec-support (~> 3.9.0)
|
44
|
-
rspec-mocks (3.9.0)
|
45
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.9.0)
|
47
|
-
rspec-support (3.9.0)
|
48
|
-
sqlite3 (1.4.2)
|
49
|
-
thread_safe (0.3.6)
|
50
|
-
tzinfo (1.2.5)
|
51
|
-
thread_safe (~> 0.1)
|
52
|
-
|
53
|
-
PLATFORMS
|
54
|
-
ruby
|
55
|
-
|
56
|
-
DEPENDENCIES
|
57
|
-
activerecord!
|
58
|
-
bundler
|
59
|
-
byebug
|
60
|
-
polymorphic_integer_type!
|
61
|
-
rake
|
62
|
-
rspec
|
63
|
-
sqlite3
|
64
|
-
|
65
|
-
BUNDLED WITH
|
66
|
-
1.16.1
|
@@ -1,65 +0,0 @@
|
|
1
|
-
module PolymorphicIntegerType
|
2
|
-
module PredicateBuilderExtension
|
3
|
-
|
4
|
-
# Original Code:
|
5
|
-
# def self.expand(klass, table, column, value)
|
6
|
-
# queries = []
|
7
|
-
|
8
|
-
# # Find the foreign key when using queries such as:
|
9
|
-
# # Post.where(author: author)
|
10
|
-
# #
|
11
|
-
# # For polymorphic relationships, find the foreign key and type:
|
12
|
-
# # PriceEstimate.where(estimate_of: treasure)
|
13
|
-
# if klass && reflection = klass._reflect_on_association(column)
|
14
|
-
# base_class = polymorphic_base_class_from_value(value)
|
15
|
-
|
16
|
-
# if reflection.polymorphic? && base_class
|
17
|
-
# queries << build(table[reflection.foreign_type], base_class)
|
18
|
-
# end
|
19
|
-
|
20
|
-
# column = reflection.foreign_key
|
21
|
-
|
22
|
-
# if base_class
|
23
|
-
# primary_key = reflection.association_primary_key(base_class)
|
24
|
-
# value = convert_value_to_association_ids(value, primary_key)
|
25
|
-
# end
|
26
|
-
# end
|
27
|
-
|
28
|
-
# queries << build(table[column], value)
|
29
|
-
# queries
|
30
|
-
# end
|
31
|
-
|
32
|
-
def expand(klass, table, column, value)
|
33
|
-
queries = []
|
34
|
-
|
35
|
-
# Find the foreign key when using queries such as:
|
36
|
-
# Post.where(author: author)
|
37
|
-
#
|
38
|
-
# For polymorphic relationships, find the foreign key and type:
|
39
|
-
# PriceEstimate.where(estimate_of: treasure)
|
40
|
-
if klass && reflection = klass._reflect_on_association(column)
|
41
|
-
base_class = polymorphic_base_class_from_value(value)
|
42
|
-
|
43
|
-
if reflection.polymorphic? && base_class
|
44
|
-
if klass.respond_to?("#{column}_type_mapping")
|
45
|
-
queries << build(table[reflection.foreign_type], klass.send("#{column}_type_mapping").key(base_class.to_s))
|
46
|
-
else
|
47
|
-
queries << build(table[reflection.foreign_type], base_class)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
column = reflection.foreign_key
|
52
|
-
|
53
|
-
if base_class
|
54
|
-
primary_key = reflection.association_primary_key(base_class)
|
55
|
-
value = convert_value_to_association_ids(value, primary_key)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
queries << build(table[column], value)
|
60
|
-
queries
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
ActiveRecord::PredicateBuilder.singleton_class.prepend(PolymorphicIntegerType::PredicateBuilderExtension)
|