polymorphic_integer_type 2.2.3 → 2.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f818991a7fa6b0835e39fd4133d6bc7c163e6d4e
4
- data.tar.gz: 2e2715bfdb1d56e36d142b9c5b9bebf785795ab3
2
+ SHA256:
3
+ metadata.gz: 71d86331b1421461b63d205d371f2194139604604850c2fca777fa03d779d5b6
4
+ data.tar.gz: 051307d6d4844ff3657acca8b2e5b75ce45afd774553da5801d7fad1a991dd6d
5
5
  SHA512:
6
- metadata.gz: d9616ed1ea9e3bd402b4632b0c3b9aa4355a282424941f1538a86498420f84f91e06e9471cf7d0e243afad70f9dc74f3348b5252a4c2aea455a6b0bdc7f40c04
7
- data.tar.gz: e6b3723b6b3de1b558797c2334252369896674c451f40760ff0a1f8ccde01c14a55058c30f4fc1a7d117c70406a1e67880ecaf5cf597270358033d3e88c96d01
6
+ metadata.gz: a495033bdbe6a71f90268ed764521d8b1be527dbc614244dbfad48a49ec9a71f680906457e7f0fb69196dc79122906783c62ac909f8770d55107ad23fd78b825
7
+ data.tar.gz: 1ab16457dfae29bebf25e180053e3061f5b14a69187d0fa540bab78f74dfa5953fcf1fdf25aa0ea2d1136cc3336cb2db79be1dacaf0af8e7c619ab50e28ecbb8
@@ -1,10 +1,21 @@
1
+ ACTIVE_RECORD_VERSION = Gem::Version.new(ActiveRecord::VERSION::STRING)
2
+
1
3
  require "polymorphic_integer_type/version"
2
4
  require "polymorphic_integer_type/extensions"
3
5
  require "polymorphic_integer_type/mapping"
4
- if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5")
5
- require "polymorphic_integer_type/predicate_builder_extension"
6
+
7
+ if ACTIVE_RECORD_VERSION < Gem::Version.new("5")
8
+ require "polymorphic_integer_type/activerecord_4/predicate_builder_extension"
6
9
  else
7
- require "polymorphic_integer_type/polymorphic_array_value_extension"
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
16
+
17
+ if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
18
+ require "polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension"
8
19
  end
9
20
 
10
21
  module PolymorphicIntegerType; end
@@ -0,0 +1,10 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ class BelongsToPolymorphicAssociation < BelongsToAssociation
4
+ private def replace_keys(record)
5
+ super
6
+ owner[reflection.foreign_type] = record.class.base_class
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ module PolymorphicIntegerType
2
+ module AssociationQueryHandlerExtension
3
+ def call(attribute, value)
4
+ queries = {}
5
+ table = value.associated_table
6
+
7
+ if value.base_class
8
+ queries[table.association_foreign_type.to_s] = polymorphic_value_for(value)
9
+ end
10
+
11
+ queries[table.association_foreign_key.to_s] = value.ids
12
+ predicate_builder.build_from_hash(queries)
13
+ end
14
+
15
+ protected
16
+
17
+ def polymorphic_value_for(query_value)
18
+ table = query_value.associated_table
19
+ association = table.send(:association)
20
+ klass = association.active_record
21
+ name = association.name
22
+
23
+ if klass.respond_to?("#{name}_type_mapping")
24
+ type_mapping = klass.send("#{name}_type_mapping")
25
+
26
+ type_mapping.key(query_value.value.class.sti_name) ||
27
+ type_mapping.key(query_value.base_class.to_s) ||
28
+ type_mapping.key(query_value.base_class.sti_name)
29
+ else
30
+ query_value.base_class.name
31
+ end
32
+ end
33
+
34
+
35
+ end
36
+ end
37
+
38
+ ActiveRecord::PredicateBuilder::AssociationQueryHandler.prepend(PolymorphicIntegerType::AssociationQueryHandlerExtension)
@@ -1,3 +1,3 @@
1
1
  module PolymorphicIntegerType
2
- VERSION = "2.2.3"
2
+ VERSION = "2.2.4"
3
3
  end
@@ -24,19 +24,33 @@ describe PolymorphicIntegerType do
24
24
  end
25
25
  end
26
26
 
27
- context "when the source is a class that modifies the sti_name" do
27
+ context "when the source is a class that modifies the sti_name or polymorphic_name" do
28
28
  it "properly sets the source_type to the modified class name" do
29
29
  link = Link.new(source: Namespaced::Animal.new)
30
30
  expect(link.source_type).to eql "Animal"
31
31
  end
32
+
33
+ it "can read dirty attributes from an associated object" do
34
+ animal = Namespaced::Animal.create!(name: "Oldie")
35
+ animal.name = "Newton"
36
+ link = Link.create!(source: animal)
37
+
38
+ expect(link.source.name).to eq("Newton")
39
+ end
32
40
  end
33
41
 
34
42
  context "when querying the associations" do
35
43
  let(:source) { cat }
36
44
  let(:target) { nil }
45
+
37
46
  it "properly finds the object with a where" do
38
47
  expect(Link.where(source: source, id: link.id).first).to eql link
39
48
  end
49
+
50
+ it "properly finds the object when passing an array of sources" do
51
+ expect(Link.where(source: [source])).to eq [link]
52
+ end
53
+
40
54
  it "properly finds the object with a find_by" do
41
55
  expect(Link.find_by(source: source, id: link.id)).to eql link
42
56
  end
@@ -103,7 +117,7 @@ describe PolymorphicIntegerType do
103
117
 
104
118
  end
105
119
 
106
- context "When using a relation to the links with eagar loading" do
120
+ context "When using a relation to the links with eager loading" do
107
121
  let!(:links){
108
122
  [Link.create(source: source, target: kibble),
109
123
  Link.create(source: source, target: water)]
@@ -118,7 +132,7 @@ describe PolymorphicIntegerType do
118
132
 
119
133
  end
120
134
 
121
- context "When using a through relation to the links with eagar loading" do
135
+ context "When using a through relation to the links with eager loading" do
122
136
  let!(:links){
123
137
  [Link.create(source: source, target: kibble),
124
138
  Link.create(source: source, target: water)]
@@ -133,7 +147,7 @@ describe PolymorphicIntegerType do
133
147
 
134
148
  end
135
149
 
136
- context "When eagar loading the polymorphic association" do
150
+ context "When eager loading the polymorphic association" do
137
151
  let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
138
152
  let(:source) { cat }
139
153
 
@@ -154,6 +168,7 @@ describe PolymorphicIntegerType do
154
168
  end
155
169
 
156
170
 
171
+
157
172
  end
158
173
 
159
174
  context "when the association is an STI table" do
data/spec/spec_helper.rb CHANGED
@@ -9,12 +9,13 @@ require 'support/dog'
9
9
  require 'support/person'
10
10
  require 'support/food'
11
11
  require 'support/drink'
12
+ require 'byebug'
12
13
 
13
14
  RSpec.configure do |config|
14
15
  config.before(:suite) do
15
16
  database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
16
17
  ActiveRecord::Base.establish_connection(database_config)
17
- if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5")
18
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5.2.0")
18
19
  ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
19
20
  end
20
21
  end
@@ -3,5 +3,9 @@ module Namespaced
3
3
 
4
4
  self.store_full_sti_class = false
5
5
  self.table_name = "animals"
6
+
7
+ def self.polymorphic_name
8
+ "Animal"
9
+ end
6
10
  end
7
- end
11
+ end
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: 2.2.3
4
+ version: 2.2.4
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: 2019-12-09 00:00:00.000000000 Z
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -109,10 +109,12 @@ files:
109
109
  - Rakefile
110
110
  - bin/setup
111
111
  - lib/polymorphic_integer_type.rb
112
+ - lib/polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension.rb
113
+ - lib/polymorphic_integer_type/activerecord_4/predicate_builder_extension.rb
114
+ - lib/polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension.rb
115
+ - lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb
112
116
  - lib/polymorphic_integer_type/extensions.rb
113
117
  - lib/polymorphic_integer_type/mapping.rb
114
- - lib/polymorphic_integer_type/polymorphic_array_value_extension.rb
115
- - lib/polymorphic_integer_type/predicate_builder_extension.rb
116
118
  - lib/polymorphic_integer_type/version.rb
117
119
  - polymorphic_integer_type.gemspec
118
120
  - spec/polymorphic_integer_type_spec.rb
@@ -151,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
153
  version: '0'
152
154
  requirements: []
153
155
  rubyforge_project:
154
- rubygems_version: 2.6.14
156
+ rubygems_version: 2.7.9
155
157
  signing_key:
156
158
  specification_version: 4
157
159
  summary: Use integers rather than strings for the _type field