polymorphic_join 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ffbf0f031923d716557359eac5fa4f3f076fd175
4
- data.tar.gz: 311ed02d73c93b98f29f7702f8d66627d71bcb8e
2
+ SHA256:
3
+ metadata.gz: 18aae681c305d7e6a3e84dc25b222504d8ffc3e42f179d1c898df52c7f574167
4
+ data.tar.gz: c036d18bc05f96af6acd3ac5f131ff000df2d441347f55d1a10505bd6be6a4e6
5
5
  SHA512:
6
- metadata.gz: 98901ec9be45e69b884a1cd97f537f53453905351edab66a1808daa3e19b9596fad1ba2a37f28cbdc9fe22737d04872fcaf725302695ecbcf17093c17a1e4ef9
7
- data.tar.gz: 1e35a1bf325ff457a84a88dccce6b99887c77a99e0df9bff53f3613d2ae35a336f6222a911849ec246384f6456e63db1ee5f95cca56012c3348762979c19d569
6
+ metadata.gz: 43967b82c57498e69c0bfb6e3db8ac41fe62e72f362ca29a3b04f267da3fdff9024b92a2678f517999cef0a522c39f33410d4db497b506450b412613c83595af
7
+ data.tar.gz: 922542eea684ee2006b82663d91abbad8fc5ddfa2368c0c9a5d66c05a2efbfdb1e421bb607e70fbb795ebc04061a12d37ed91a5d9327acfb7e8831af52e0781c
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
data/README.md CHANGED
@@ -1,15 +1,13 @@
1
1
  # PolymorphicJoin
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/polymorphic_join`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- Assuming I have this model call `Notification`
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(
@@ -1,3 +1,3 @@
1
1
  module PolymorphicJoin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
- private
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 = refs.map do |join_type|
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
@@ -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 = ['james@rubify.com']
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://rubify.com'
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.1.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-09 00:00:00.000000000 Z
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
- - james@rubify.com
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://rubify.com
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
- rubyforge_project:
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