polymorphic_integer_type 3.1.1 → 3.2.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/.gitignore +2 -0
- data/CHANGELOG.md +13 -0
- data/gemfiles/Gemfile.rails-7.0-stable +7 -0
- data/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb +1 -1
- data/lib/polymorphic_integer_type/extensions.rb +31 -4
- data/lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb +11 -0
- data/lib/polymorphic_integer_type/version.rb +1 -1
- data/lib/polymorphic_integer_type.rb +1 -0
- data/polymorphic_integer_type.gemspec +2 -2
- data/spec/polymorphic_integer_type_spec.rb +26 -0
- data/spec/support/animal.rb +1 -1
- metadata +8 -8
- data/.github/CODEOWNERS +0 -1
- data/.github/workflows/ci.yml +0 -34
- data/.github/workflows/gem-push.yml +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeec5115208f7fee02b00a10f05e660a46550b2c0815999133a1fe541950c1dc
|
4
|
+
data.tar.gz: 8268bfe5dd15c0cb9f3c6cc240989ad89cc112dff8ac9723ed64e6de8e7ddc61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 801b9bf926c3a23237f0838013d8e425c902292f186f38f65ff41c0e80b94ac5df15152e8d8d108800650439e069c990342f2d6c4fdcfb79b4e4870403bc93f9
|
7
|
+
data.tar.gz: c81b0af3ef8593d8f607c12db9ef0b685f91cd7b21b4e85f70289cd30fcfe839427e2eecc43cb2de01295ba454522e15dd04be101cfa001d86061c68403d1432
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
@@ -2,6 +2,10 @@ module PolymorphicIntegerType
|
|
2
2
|
|
3
3
|
module Extensions
|
4
4
|
module ClassMethods
|
5
|
+
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:foreign_integer_type)
|
6
|
+
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:integer_type)
|
7
|
+
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:foreign_integer_type)
|
8
|
+
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:integer_type)
|
5
9
|
|
6
10
|
def belongs_to(name, scope = nil, **options)
|
7
11
|
options = scope if scope.kind_of? Hash
|
@@ -64,8 +68,10 @@ module PolymorphicIntegerType
|
|
64
68
|
condition = instance_exec(&scope).merge(condition) if scope.is_a?(Proc)
|
65
69
|
condition
|
66
70
|
}
|
71
|
+
return foreign_type, klass_mapping.to_i
|
67
72
|
else
|
68
73
|
options[:scope] ||= scope
|
74
|
+
return nil, nil
|
69
75
|
end
|
70
76
|
end
|
71
77
|
|
@@ -86,8 +92,10 @@ module PolymorphicIntegerType
|
|
86
92
|
scope = nil
|
87
93
|
end
|
88
94
|
|
89
|
-
remove_type_and_establish_mapping(name, options, scope)
|
90
|
-
super(name, options.delete(:scope), options, &extension)
|
95
|
+
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
|
96
|
+
super(name, options.delete(:scope), **options, &extension).tap do
|
97
|
+
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
|
98
|
+
end
|
91
99
|
end
|
92
100
|
|
93
101
|
def has_one(name, scope = nil, **options)
|
@@ -96,8 +104,27 @@ module PolymorphicIntegerType
|
|
96
104
|
scope = nil
|
97
105
|
end
|
98
106
|
|
99
|
-
remove_type_and_establish_mapping(name, options, scope)
|
100
|
-
super(name, options.delete(:scope), options)
|
107
|
+
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
|
108
|
+
super(name, options.delete(:scope), **options).tap do
|
109
|
+
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflection)
|
114
|
+
foreign_integer_type = integer_type_values[0]
|
115
|
+
integer_type = integer_type_values[1]
|
116
|
+
is_polymorphic_integer = foreign_integer_type && integer_type
|
117
|
+
|
118
|
+
if is_polymorphic_integer
|
119
|
+
reflection.foreign_integer_type = foreign_integer_type
|
120
|
+
reflection.integer_type = integer_type
|
121
|
+
|
122
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
|
123
|
+
ActiveRecord::Associations::Association.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
|
124
|
+
else
|
125
|
+
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
|
126
|
+
end
|
127
|
+
end
|
101
128
|
end
|
102
129
|
|
103
130
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module PolymorphicIntegerType
|
2
|
+
module PolymorphicForeignAssociationExtension
|
3
|
+
|
4
|
+
def set_owner_attributes(record)
|
5
|
+
super
|
6
|
+
if reflection.foreign_integer_type && reflection.integer_type
|
7
|
+
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -6,6 +6,7 @@ require "polymorphic_integer_type/mapping"
|
|
6
6
|
require "polymorphic_integer_type/module_generator"
|
7
7
|
require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
|
8
8
|
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
|
9
|
+
require "polymorphic_integer_type/polymorphic_foreign_association_extension"
|
9
10
|
|
10
11
|
if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
|
11
12
|
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
|
@@ -13,12 +13,12 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.files = `git ls-files -- . ':!.github/'`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
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", "< 7"
|
21
|
+
spec.add_dependency "activerecord", "< 7.1"
|
22
22
|
spec.add_development_dependency "bundler"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
spec.add_development_dependency "rspec"
|
@@ -26,6 +26,28 @@ describe PolymorphicIntegerType do
|
|
26
26
|
expect(link.target_type).to eq("Food")
|
27
27
|
end
|
28
28
|
|
29
|
+
context "from HasManyReflection" do
|
30
|
+
it "sets the source properly HasManyReflection" do
|
31
|
+
link_1 = Link.create()
|
32
|
+
link_2 = Link.create()
|
33
|
+
dog.source_links = [link_1, link_2]
|
34
|
+
expect(link_1.source_type).to eq("Animal")
|
35
|
+
expect(link_1.source_id).to eq(dog.id)
|
36
|
+
expect(link_2.source_type).to eq("Animal")
|
37
|
+
expect(link_1.source_id).to eq(dog.id)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "from HasOneReflection" do
|
42
|
+
it "sets the source properly HasOneReflection" do
|
43
|
+
link = Link.create()
|
44
|
+
dog.source_link = link
|
45
|
+
|
46
|
+
expect(link.source_type).to eq("Animal")
|
47
|
+
expect(link.source_id).to eq(dog.id)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
29
51
|
context "when models are namespaced" do
|
30
52
|
context "and mappings include namespaces" do
|
31
53
|
it "sets the source_type" do
|
@@ -340,6 +362,10 @@ describe PolymorphicIntegerType do
|
|
340
362
|
expect(link[:target_type]).to eq(13)
|
341
363
|
end
|
342
364
|
|
365
|
+
it "pulls mapping from given hash" do
|
366
|
+
animal.source_links.new
|
367
|
+
end
|
368
|
+
|
343
369
|
it "doesn't break string type polymorphic associations" do
|
344
370
|
expect(link.normal_target).to eq(drink)
|
345
371
|
expect(link.normal_target_type).to eq("InlineDrink2")
|
data/spec/support/animal.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_integer_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.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: 2023-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "<"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '7'
|
19
|
+
version: '7.1'
|
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: '7'
|
26
|
+
version: '7.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -102,10 +102,8 @@ 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"
|
108
105
|
- ".gitignore"
|
106
|
+
- CHANGELOG.md
|
109
107
|
- Gemfile
|
110
108
|
- LICENSE.txt
|
111
109
|
- README.md
|
@@ -116,6 +114,7 @@ files:
|
|
116
114
|
- gemfiles/Gemfile.rails-5.2-stable
|
117
115
|
- gemfiles/Gemfile.rails-6.0-stable
|
118
116
|
- gemfiles/Gemfile.rails-6.1-stable
|
117
|
+
- gemfiles/Gemfile.rails-7.0-stable
|
119
118
|
- lib/polymorphic_integer_type.rb
|
120
119
|
- lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb
|
121
120
|
- lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
|
@@ -123,6 +122,7 @@ files:
|
|
123
122
|
- lib/polymorphic_integer_type/extensions.rb
|
124
123
|
- lib/polymorphic_integer_type/mapping.rb
|
125
124
|
- lib/polymorphic_integer_type/module_generator.rb
|
125
|
+
- lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb
|
126
126
|
- lib/polymorphic_integer_type/version.rb
|
127
127
|
- polymorphic_integer_type.gemspec
|
128
128
|
- spec/polymorphic_integer_type_spec.rb
|
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
requirements: []
|
167
|
-
rubygems_version: 3.
|
167
|
+
rubygems_version: 3.4.10
|
168
168
|
signing_key:
|
169
169
|
specification_version: 4
|
170
170
|
summary: Use integers rather than strings for the _type field
|
data/.github/CODEOWNERS
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
/.github/workflows/ @clio/application-security @clio/penguins
|
data/.github/workflows/ci.yml
DELETED
@@ -1,34 +0,0 @@
|
|
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
|
@@ -1,30 +0,0 @@
|
|
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
|