polymorphic_join 0.1.0 → 0.2.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 +5 -5
- data/.gitignore +1 -0
- data/README.md +13 -5
- data/lib/polymorphic_join/version.rb +1 -1
- data/lib/polymorphic_join.rb +13 -4
- data/polymorphic_join.gemspec +2 -2
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 18aae681c305d7e6a3e84dc25b222504d8ffc3e42f179d1c898df52c7f574167
|
4
|
+
data.tar.gz: c036d18bc05f96af6acd3ac5f131ff000df2d441347f55d1a10505bd6be6a4e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43967b82c57498e69c0bfb6e3db8ac41fe62e72f362ca29a3b04f267da3fdff9024b92a2678f517999cef0a522c39f33410d4db497b506450b412613c83595af
|
7
|
+
data.tar.gz: 922542eea684ee2006b82663d91abbad8fc5ddfa2368c0c9a5d66c05a2efbfdb1e421bb607e70fbb795ebc04061a12d37ed91a5d9327acfb7e8831af52e0781c
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
# PolymorphicJoin
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Rails does not include a polymorphic join by default but this gem would help you to joins your polymorphic relationship with ease.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
11
9
|
```ruby
|
12
|
-
gem 'polymorphic_join'
|
10
|
+
gem 'polymorphic_join', '~> 0.2.0'
|
13
11
|
```
|
14
12
|
|
15
13
|
And then execute:
|
@@ -30,7 +28,8 @@ include PolymorphicJoin
|
|
30
28
|
|
31
29
|
Then you can use the polymorphic join like followings:
|
32
30
|
|
33
|
-
|
31
|
+
Given that I have this model call `Notification`
|
32
|
+
|
34
33
|
```rb
|
35
34
|
class Notification < ApplicationRecord
|
36
35
|
belongs_to :notifiable, polymorphic: true
|
@@ -40,6 +39,15 @@ end
|
|
40
39
|
|
41
40
|
Then I can call:
|
42
41
|
|
42
|
+
```rb
|
43
|
+
Notification
|
44
|
+
.ref_polymorphic(:notifiable)
|
45
|
+
.where('notifiables.common_attribute' => 'test')
|
46
|
+
.order('notifiables.common_attribute ASC')
|
47
|
+
```
|
48
|
+
|
49
|
+
Or if I want to specify only those types that I want to reference to:
|
50
|
+
|
43
51
|
```rb
|
44
52
|
Notification
|
45
53
|
.ref_polymorphic(
|
data/lib/polymorphic_join.rb
CHANGED
@@ -10,6 +10,9 @@ module PolymorphicJoin
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
|
14
|
+
private
|
15
|
+
|
13
16
|
def polymorphic_union_scope(types, *scopes)
|
14
17
|
union = scopes[0]
|
15
18
|
t2 = arel_table
|
@@ -24,11 +27,17 @@ module PolymorphicJoin
|
|
24
27
|
from(t2.create_join(t1, t2.create_on(t1[:id].eq(t2[:id]))))
|
25
28
|
end
|
26
29
|
|
27
|
-
|
30
|
+
def retrieve_all_polymorphic_types(type)
|
31
|
+
pluck("#{type}_type").collect { |x| x.underscore.tableize.to_sym }
|
32
|
+
end
|
28
33
|
|
29
34
|
def add_ref_polymorphic_scope
|
30
|
-
scope :ref_polymorphic, lambda { |type, refs|
|
31
|
-
polymorphic = refs
|
35
|
+
scope :ref_polymorphic, lambda { |type, refs = nil|
|
36
|
+
polymorphic = if refs.nil?
|
37
|
+
retrieve_all_polymorphic_types(type)
|
38
|
+
else
|
39
|
+
refs
|
40
|
+
end
|
32
41
|
columns = polymorphic.first.to_s.classify.constantize.column_names
|
33
42
|
polymorphic.each do |p|
|
34
43
|
columns &= p.to_s.classify.constantize.column_names
|
@@ -43,7 +52,7 @@ module PolymorphicJoin
|
|
43
52
|
end
|
44
53
|
|
45
54
|
table = arel_table
|
46
|
-
joins_statements =
|
55
|
+
joins_statements = polymorphic.map do |join_type|
|
47
56
|
join_table =
|
48
57
|
join_type.to_s.classify.constantize.arel_table.alias(types)
|
49
58
|
arel_table
|
data/polymorphic_join.gemspec
CHANGED
@@ -8,11 +8,11 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.name = 'polymorphic_join'
|
9
9
|
spec.version = PolymorphicJoin::VERSION
|
10
10
|
spec.authors = ['James Huynh']
|
11
|
-
spec.email = ['
|
11
|
+
spec.email = ['jameshuynhsg@gmail.com']
|
12
12
|
|
13
13
|
spec.summary = 'This gem is used to do polymorphic join'
|
14
14
|
spec.description = 'This gem is used to do polymorphic join'
|
15
|
-
spec.homepage = 'https://
|
15
|
+
spec.homepage = 'https://github.com/jameshuynh/polymorphic_join'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
|
18
18
|
if spec.respond_to?(:metadata)
|
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.
|
4
|
+
version: 0.2.0
|
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-04-
|
11
|
+
date: 2019-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
version: '10.0'
|
41
41
|
description: This gem is used to do polymorphic join
|
42
42
|
email:
|
43
|
-
-
|
43
|
+
- jameshuynhsg@gmail.com
|
44
44
|
executables: []
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- lib/polymorphic_join.rb
|
57
57
|
- lib/polymorphic_join/version.rb
|
58
58
|
- polymorphic_join.gemspec
|
59
|
-
homepage: https://
|
59
|
+
homepage: https://github.com/jameshuynh/polymorphic_join
|
60
60
|
licenses:
|
61
61
|
- MIT
|
62
62
|
metadata:
|
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
|
80
|
-
rubygems_version: 2.6.13
|
79
|
+
rubygems_version: 3.0.3
|
81
80
|
signing_key:
|
82
81
|
specification_version: 4
|
83
82
|
summary: This gem is used to do polymorphic join
|