active_record-union_relation 0.2.1 → 0.3.0

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: d75105a966e30a6a18ed59c42c39bd22d5685e60b3f8d96eba472a2f5b894507
4
- data.tar.gz: 86a4699ab3cb5f6af0d2748607d16de5ab4b1b100cf0cd41ab26b1707686b79e
3
+ metadata.gz: 85cab300b41481aa6b308293a0cad8d5d57ba6cb69d7038584aa6d6853e8871b
4
+ data.tar.gz: 3519ebba7762500a87dc12239ed6eba82cfc411bc2f75c894e78c2777283393b
5
5
  SHA512:
6
- metadata.gz: 4ba4ea419535dc83c1466220fc6546a76c3970fb3152d210a98dc7a40eac7fef54b127d3e1056bc9e257c4fd30a419bf3a186da5334205ef096de74248effd5f
7
- data.tar.gz: 4cac1923b62d6505ee0cb0701329d61164a8e2301bad505d93c01b31a70fb1bc4d981cf87cf9ea68455be35458bde4c76a2eca67570ff527026bfb8f500e4592
6
+ metadata.gz: af74e42519746bddea42fc1dc86988f87db85afd5de8cc3cfb141b9d59bf601510ba74a717eed8cd2aabb5b2dac0e66f0a95ba87b90aca21fdacbb79e9e04319
7
+ data.tar.gz: b4b49cbf3975e6577b0a9c4e3683d318d9f8d312b2e63ec0b4bc127e9ae979a91e7f575d8d00cf195fe9648ae30f3af4df4269f2c05586e47a1fe28a95a2e6c1
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.0] - 2024-06-13
10
+
11
+ ### Added
12
+
13
+ - Support relations that are using models that have descendants through STI.
14
+
9
15
  ## [0.2.1] - 2024-05-29
10
16
 
11
17
  ### Changed
@@ -31,7 +37,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
31
37
 
32
38
  - 🎉 Initial release. 🎉
33
39
 
34
- [unreleased]: https://github.com/kddnewton/active_record-union_relation/compare/v0.2.1...HEAD
40
+ [unreleased]: https://github.com/kddnewton/active_record-union_relation/compare/v0.3.0...HEAD
41
+ [0.3.0]: https://github.com/kddnewton/active_record-union_relation/compare/v0.2.1...v0.3.0
35
42
  [0.2.1]: https://github.com/kddnewton/active_record-union_relation/compare/v0.2.0...v0.2.1
36
43
  [0.2.0]: https://github.com/kddnewton/active_record-union_relation/compare/v0.1.1...v0.2.0
37
44
  [0.1.1]: https://github.com/kddnewton/active_record-union_relation/compare/v0.1.0...v0.1.1
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  class UnionRelation
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -33,17 +33,68 @@ module ActiveRecord
33
33
  # number of columns, you can put a null in space of a column instead.
34
34
  NULL = Arel.sql("NULL")
35
35
 
36
+ # A model name for a model that is not using single-table inheritance. In
37
+ # this case we use the model name itself as the discriminator and only
38
+ # need one entry in the mappings hash that maps records to the columns
39
+ # that we are pulling from the result.
40
+ class SingleModelName
41
+ attr_reader :name
42
+
43
+ def initialize(name)
44
+ @name = name
45
+ end
46
+
47
+ def each_name
48
+ yield name
49
+ end
50
+
51
+ def to_sql
52
+ Arel.sql("'#{name}'")
53
+ end
54
+ end
55
+
56
+ # A model name for a model that is using single-table inheritance. In this
57
+ # case we use the inheritance column as the discriminator and need to
58
+ # include all of the subclasses in the mappings hash.
59
+ class MultiModelName
60
+ attr_reader :inheritance_column, :names
61
+
62
+ def initialize(inheritance_column, names)
63
+ @inheritance_column = inheritance_column
64
+ @names = names
65
+ end
66
+
67
+ def each_name(&block)
68
+ names.each(&block)
69
+ end
70
+
71
+ def to_sql
72
+ Arel.sql(inheritance_column)
73
+ end
74
+ end
75
+
36
76
  attr_reader :relation, :model_name, :sources
37
77
 
38
78
  def initialize(relation, sources)
39
79
  @relation = relation
40
- @model_name = relation.model.name
80
+
81
+ model = relation.model
82
+ @model_name =
83
+ if model._has_attribute?(model.inheritance_column)
84
+ MultiModelName.new(
85
+ quote_column_name(model.inheritance_column),
86
+ model.descendants.map(&:name)
87
+ )
88
+ else
89
+ SingleModelName.new(model.name)
90
+ end
91
+
41
92
  @sources = sources.map { |source| source ? source.to_s : NULL }
42
93
  end
43
94
 
44
95
  def to_arel(columns, discriminator)
45
96
  relation.select(
46
- Arel.sql("'#{model_name}'").as(quote_column_name(discriminator)),
97
+ model_name.to_sql.as(quote_column_name(discriminator)),
47
98
  *sources
48
99
  .zip(columns)
49
100
  .map do |(source, column)|
@@ -52,10 +103,11 @@ module ActiveRecord
52
103
  ).arel
53
104
  end
54
105
 
55
- def to_mapping(columns)
106
+ def merge_mappings(mappings, columns)
56
107
  # Remove the scope_name/table_name when using table_name.column
57
- sources_without_scope = sources.map { _1.split(".").last }
58
- [model_name, columns.zip(sources_without_scope).to_h]
108
+ mapping =
109
+ columns.zip(sources.map { |source| source.split(".").last }).to_h
110
+ model_name.each_name { |name| mappings[name] = mapping }
59
111
  end
60
112
 
61
113
  private
@@ -95,7 +147,9 @@ module ActiveRecord
95
147
 
96
148
  def subclass_for(model)
97
149
  discriminator = self.discriminator
98
- mappings = subqueries.to_h { |subquery| subquery.to_mapping(columns) }
150
+
151
+ mappings = {}
152
+ subqueries.each { |subquery| subquery.merge_mappings(mappings, columns) }
99
153
 
100
154
  Class.new(model) do
101
155
  # Set the inheritance column and register the discriminator as a string
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-union_relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-29 00:00:00.000000000 Z
11
+ date: 2024-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -99,7 +99,7 @@ licenses:
99
99
  - MIT
100
100
  metadata:
101
101
  bug_tracker_uri: https://github.com/kddnewton/active_record-union_relation/issues
102
- changelog_uri: https://github.com/kddnewton/active_record-union_relation/blob/v0.2.1/CHANGELOG.md
102
+ changelog_uri: https://github.com/kddnewton/active_record-union_relation/blob/v0.3.0/CHANGELOG.md
103
103
  source_code_uri: https://github.com/kddnewton/active_record-union_relation
104
104
  rubygems_mfa_required: 'true'
105
105
  post_install_message: