polymorphic_join 0.2.1 → 0.2.2
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 +5 -5
- data/README.md +18 -1
- data/lib/polymorphic_join/version.rb +1 -1
- data/lib/polymorphic_join.rb +88 -40
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b2e3c19213a3b9fbe21bfa6d1ea920f31c9f5def
|
4
|
+
data.tar.gz: 5bf97916ff403fc3a719da1711efdce1bd2adc1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd699d99211940f5f9fc54e350b33b69bd98a60ea880a84034717dcd24b2634327d8c7f5c7113d20f995958f4c91633187f0c6ad2ede6d4531d7a0d721d931ce
|
7
|
+
data.tar.gz: 2be3693618f439c047a23f578c205d21becd4f2cc2bcc694a263a428cffb7ab0780bdae58a8d641df022fe01fc115187f6c4dcb49adf59d18e59fe2c3b232833
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Rails does not include a polymorphic join by default but this gem would help you
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'polymorphic_join', '~> 0.2.
|
10
|
+
gem 'polymorphic_join', '~> 0.2.1'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -57,6 +57,23 @@ Notification
|
|
57
57
|
.order('notifiables.common_attribute ASC')
|
58
58
|
```
|
59
59
|
|
60
|
+
Or if you want to map back certain column
|
61
|
+
|
62
|
+
```rb
|
63
|
+
Notification
|
64
|
+
.ref_polymorphic(
|
65
|
+
:notifiable,
|
66
|
+
[
|
67
|
+
:comments,
|
68
|
+
{
|
69
|
+
uploads: { title: 'content' }
|
70
|
+
}
|
71
|
+
]
|
72
|
+
)
|
73
|
+
.where('notifiables.common_attribute' => 'test')
|
74
|
+
.order('notifiables.common_attribute ASC')
|
75
|
+
```
|
76
|
+
|
60
77
|
## Development
|
61
78
|
|
62
79
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/polymorphic_join.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# rubocop:disable
|
1
|
+
# rubocop:disable MethodLength, AbcSize
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'polymorphic_join/version'
|
@@ -10,8 +10,73 @@ module PolymorphicJoin
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
def query_ref_polymorphic(type, refs)
|
14
|
+
polymorphic = if refs.nil?
|
15
|
+
{
|
16
|
+
refs: retrieve_all_polymorphic_types(type),
|
17
|
+
map_columns: {}
|
18
|
+
}
|
19
|
+
else
|
20
|
+
process_input_refs(refs)
|
21
|
+
end
|
13
22
|
|
14
|
-
|
23
|
+
columns =
|
24
|
+
polymorphic[:refs]
|
25
|
+
.first
|
26
|
+
.to_s
|
27
|
+
.classify
|
28
|
+
.constantize
|
29
|
+
.column_names
|
30
|
+
|
31
|
+
polymorphic[:refs].each do |p|
|
32
|
+
columns &= p.to_s.classify.constantize.column_names
|
33
|
+
end
|
34
|
+
|
35
|
+
polymorphic[:map_columns].values.each do |value|
|
36
|
+
columns += value.values
|
37
|
+
end
|
38
|
+
|
39
|
+
columns.uniq!
|
40
|
+
|
41
|
+
selectable_columns = {}
|
42
|
+
types = type.to_s.pluralize
|
43
|
+
polymorphic[:refs].each do |p|
|
44
|
+
map_columns = polymorphic[:map_columns][p] || {}
|
45
|
+
selectable_columns[p] = []
|
46
|
+
|
47
|
+
p.to_s.classify.constantize.column_names.each do |column_name|
|
48
|
+
if (alias_name = map_columns[column_name.to_sym]).present?
|
49
|
+
selectable_columns[p] <<
|
50
|
+
"#{types}.#{column_name} AS #{alias_name}"
|
51
|
+
elsif columns.index(column_name)
|
52
|
+
selectable_columns[p] <<
|
53
|
+
"#{types}.#{column_name}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
table = arel_table
|
59
|
+
joins_statements = polymorphic[:refs].map do |join_type|
|
60
|
+
join_table =
|
61
|
+
join_type.to_s.classify.constantize.arel_table.alias(types)
|
62
|
+
arel_table
|
63
|
+
.project(*(selectable_columns[join_type]))
|
64
|
+
.join(
|
65
|
+
join_table, Arel::Nodes::InnerJoin
|
66
|
+
)
|
67
|
+
.on(
|
68
|
+
table["#{type}_type".to_sym]
|
69
|
+
.eq(join_type.to_s.classify)
|
70
|
+
.and(
|
71
|
+
table["#{type}_id".to_sym].eq(join_table[:id])
|
72
|
+
)
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
polymorphic_union_scope(types, *joins_statements)
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
15
80
|
|
16
81
|
def polymorphic_union_scope(types, *scopes)
|
17
82
|
union = scopes[0]
|
@@ -28,52 +93,35 @@ module PolymorphicJoin
|
|
28
93
|
end
|
29
94
|
|
30
95
|
def retrieve_all_polymorphic_types(type)
|
31
|
-
distinct.pluck("#{type}_type").collect do |x|
|
96
|
+
distinct.pluck("#{type}_type").collect do |x|
|
32
97
|
x.underscore.tableize.to_sym
|
33
98
|
end
|
34
99
|
end
|
35
100
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
refs
|
42
|
-
end
|
43
|
-
columns = polymorphic.first.to_s.classify.constantize.column_names
|
44
|
-
polymorphic.each do |p|
|
45
|
-
columns &= p.to_s.classify.constantize.column_names
|
46
|
-
end
|
47
|
-
|
48
|
-
types = type.to_s.pluralize
|
49
|
-
|
50
|
-
columns -= column_names
|
51
|
-
selectable_columns = []
|
52
|
-
columns.each do |column|
|
53
|
-
selectable_columns << "#{types}.#{column}"
|
54
|
-
end
|
101
|
+
def process_input_refs(refs)
|
102
|
+
poly_refs = []
|
103
|
+
refs.each do |ref|
|
104
|
+
poly_refs << (ref.is_a?(Symbol) ? ref : ref.keys.first)
|
105
|
+
end
|
55
106
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
.on(
|
66
|
-
table["#{type}_type".to_sym]
|
67
|
-
.eq(join_type.to_s.classify)
|
68
|
-
.and(
|
69
|
-
table["#{type}_id".to_sym].eq(join_table[:id])
|
70
|
-
)
|
71
|
-
)
|
107
|
+
puts refs.inspect
|
108
|
+
{
|
109
|
+
refs: poly_refs,
|
110
|
+
map_columns: refs.each_with_object({}) do |el, hash|
|
111
|
+
if el.is_a?(Symbol)
|
112
|
+
hash[el] = {}
|
113
|
+
else
|
114
|
+
hash[el.keys.first] = el[el.keys.first]
|
115
|
+
end
|
72
116
|
end
|
117
|
+
}
|
118
|
+
end
|
73
119
|
|
74
|
-
|
120
|
+
def add_ref_polymorphic_scope
|
121
|
+
scope :ref_polymorphic, lambda { |type, refs = nil|
|
122
|
+
query_ref_polymorphic(type, refs)
|
75
123
|
}
|
76
124
|
end
|
77
125
|
end
|
78
126
|
end
|
79
|
-
# rubocop:
|
127
|
+
# rubocop:disable all
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_join
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Huynh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,7 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.6.13
|
80
81
|
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: This gem is used to do polymorphic join
|