polymorphic_integer_type 2.2.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a821ba758df6177c1f89d6f147bedd24e5a96874b65e9bff7c348b04f5a60013
4
- data.tar.gz: 23f8a6196d1523ef46d58af66612646a910744e511f959ff783af977e8ea56a6
3
+ metadata.gz: a0ad89af7c6b5e5602d7a55d61d3ba112b5db2afbeb5e43226972eaaff50761b
4
+ data.tar.gz: 5838309b72defdf55b950c155c3d5d87ebe7a663aaad9dc5e8453bb246dd28f7
5
5
  SHA512:
6
- metadata.gz: 0a4e980b7f71e2fd78f030fcd8053cb12923775075e1f9425ee999c24e65d017b30e248bfa484e5a1df7ee300afdf71c1ea03c8260663b475fc477431f81536e
7
- data.tar.gz: 5e7be692212675c3f346d10b18328d9b35a0fc5bfc68fbfe19dfea1e0be112ad8d9410b6429b2986b212d64cf94e8825d2aa27d1aed29197625cdf8d45b5e81b
6
+ metadata.gz: 07a499153097713ec3f90b5a42b796205ea773a04a1b77aedc4e9d8e1f0ac8f2d84238a2a6350e3cce7fba1b8f5fcf59e3b28642e39a0f55c532cf5d94f22a18
7
+ data.tar.gz: bf81c398c18cdc8d2f7edaf81f8a669237b4aff73152c0bd180bb264bd4b5216bc6cc358a7fdd48aa8631cc1f31758ba9ddacf23e49e16c1b1cf2a1553199c62
@@ -1,6 +1,10 @@
1
1
  require "polymorphic_integer_type/version"
2
2
  require "polymorphic_integer_type/extensions"
3
3
  require "polymorphic_integer_type/mapping"
4
- require "polymorphic_integer_type/polymorphic_array_value_extension"
4
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5")
5
+ require "polymorphic_integer_type/predicate_builder_extension"
6
+ else
7
+ require "polymorphic_integer_type/polymorphic_array_value_extension"
8
+ end
5
9
 
6
10
  module PolymorphicIntegerType; end
@@ -0,0 +1,65 @@
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)
@@ -1,3 +1,3 @@
1
1
  module PolymorphicIntegerType
2
- VERSION = "2.2.0"
2
+ VERSION = "2.2.1"
3
3
  end
@@ -13,7 +13,9 @@ RSpec.configure do |config|
13
13
  config.before(:suite) do
14
14
  database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
15
15
  ActiveRecord::Base.establish_connection(database_config)
16
- ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
16
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5")
17
+ ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
18
+ end
17
19
  end
18
20
 
19
21
  config.around do |example|
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.0
4
+ version: 2.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: 2019-06-25 00:00:00.000000000 Z
11
+ date: 2019-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -112,6 +112,7 @@ files:
112
112
  - lib/polymorphic_integer_type/extensions.rb
113
113
  - lib/polymorphic_integer_type/mapping.rb
114
114
  - lib/polymorphic_integer_type/polymorphic_array_value_extension.rb
115
+ - lib/polymorphic_integer_type/predicate_builder_extension.rb
115
116
  - lib/polymorphic_integer_type/version.rb
116
117
  - polymorphic_integer_type.gemspec
117
118
  - spec/polymorphic_integer_type_spec.rb