sequel-through 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -1
- data/bin/console +1 -1
- data/lib/sequel/plugins/{cyclical_association_solver.rb → cyclical_through_associations.rb} +6 -6
- data/lib/sequel/plugins/through_associations.rb +10 -9
- data/lib/sequel/through.rb +1 -1
- data/sequel-through.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 052c754323070df67e30e29c408efacdd3fc04da4a973e84551fe0d3ee6ed730
|
4
|
+
data.tar.gz: 1f8ab70b4bb87ed7889e2fd9339c56328e09fbe1f4e7c493cc5fb799955db37e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6264ff5cb4701d009e11e5cfaec4bc8e5d02696f5bd634605b2e3f3eff5aeaae505be0495c806ccd4daf6e489a205dadeca19341fe4bec985d1fbb28e167c09
|
7
|
+
data.tar.gz: 5f749f13768fb60fa0c1dabbc56de70f6adaab0db284cbc3a0ac2988a3b6fb887fa809ae205cbf2dce06d1d7b81267ce14772d7e71c951f747ff8651a4e8a7a3
|
data/README.md
CHANGED
@@ -24,7 +24,40 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
```ruby
|
28
|
+
# Load the plugin
|
29
|
+
Sequel::Model.plugin :cyclical_through_associations
|
30
|
+
|
31
|
+
class User < Sequel::Model
|
32
|
+
one_to_many :user_has_security_groups
|
33
|
+
one_to_many :security_groups, through: :user_has_security_groups
|
34
|
+
one_to_many :privileges, through: :security_groups
|
35
|
+
end
|
36
|
+
class SecurityGroup < Sequel::Model
|
37
|
+
one_to_many :security_group_has_privileges
|
38
|
+
one_to_many :privileges, through: :security_group_has_privileges
|
39
|
+
end
|
40
|
+
class Privilege < Sequel::Model
|
41
|
+
end
|
42
|
+
class UserHasSecurityGroup < Sequel::Model
|
43
|
+
many_to_one :user
|
44
|
+
many_to_one :security_group
|
45
|
+
one_to_many :privileges, through: :security_group
|
46
|
+
end
|
47
|
+
class SecurityGroupHasPrivilege < Sequel::Model
|
48
|
+
many_to_one :security_group
|
49
|
+
many_to_one :privilege
|
50
|
+
end
|
51
|
+
|
52
|
+
# Solve any cyclical dependencies (when :cyclical_through_associations is used)
|
53
|
+
Sequel::Model.solve_cyclical_associations!
|
54
|
+
|
55
|
+
# Use the intermediate associations
|
56
|
+
User.first.security_groups # => [#<SecurityGroup 1>, #<SecurityGroup 2> ...]
|
57
|
+
User.first.privileges # => [#<Privilege A>, #<Privilege B> ...]
|
58
|
+
```
|
59
|
+
|
60
|
+
This plugin adds an optional `:through` key for `*_to_one` and `*_to_many` associations. When `:through` is provided, intermediary associations are traversed and a single `one_through_many` or `many_through_many` association is added to the base model using a JOIN structure calculated from the associations traversed.
|
28
61
|
|
29
62
|
## Development
|
30
63
|
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
|
4
|
+
require_relative "../test/test_helper"
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Sequel
|
2
2
|
module Plugins
|
3
|
-
module
|
3
|
+
module CyclicalThroughAssociations
|
4
4
|
|
5
5
|
# Ensure through associations are loaded
|
6
6
|
def self.apply mod
|
7
|
-
mod.plugin
|
7
|
+
mod.plugin :through_associations
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
@@ -25,7 +25,7 @@ module Sequel
|
|
25
25
|
# Attempt to solve remaining cyclical associations
|
26
26
|
@@_resolver_stack = []
|
27
27
|
stack.each do |klass, assoc_type, name, opts, block|
|
28
|
-
klass.send
|
28
|
+
klass.send assoc_type, name, **opts, &block
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -33,13 +33,13 @@ module Sequel
|
|
33
33
|
# Output errors for any unsolved associations
|
34
34
|
@@_resolving = true
|
35
35
|
@@_resolver_stack.each do |klass, assoc_type, name, opts, block|
|
36
|
-
klass.send
|
36
|
+
klass.send assoc_type, name, **opts, &block
|
37
37
|
end
|
38
38
|
@@_resolving = false
|
39
39
|
|
40
40
|
end
|
41
41
|
|
42
|
-
def associate_through
|
42
|
+
def associate_through type, name, opts, &block
|
43
43
|
begin
|
44
44
|
result = super
|
45
45
|
rescue \
|
@@ -52,7 +52,7 @@ module Sequel
|
|
52
52
|
|
53
53
|
# Otherwise, attempt to resolve later
|
54
54
|
unless result
|
55
|
-
@@_resolver_stack.push
|
55
|
+
@@_resolver_stack.push [self, type, name, opts, block]
|
56
56
|
return
|
57
57
|
end
|
58
58
|
|
@@ -3,10 +3,10 @@ module Sequel
|
|
3
3
|
module ThroughAssociations
|
4
4
|
|
5
5
|
ASSOCIATION_THROUGH_TYPES = {
|
6
|
-
:
|
6
|
+
:one_to_one => :one_through_many,
|
7
|
+
:one_to_many => :many_through_many,
|
7
8
|
:many_to_one => :one_through_many,
|
8
9
|
:many_to_many => :many_through_many
|
9
|
-
# one_to_one
|
10
10
|
# many_to_pg_array
|
11
11
|
# pg_array_to_many
|
12
12
|
}
|
@@ -16,6 +16,7 @@ module Sequel
|
|
16
16
|
|
17
17
|
# Ensure associations are loaded
|
18
18
|
def self.apply mod
|
19
|
+
Sequel.extension :inflector unless "".respond_to?(:pluralize)
|
19
20
|
mod.plugin :many_through_many
|
20
21
|
end
|
21
22
|
|
@@ -37,7 +38,7 @@ module Sequel
|
|
37
38
|
def associate_through type, name, opts, &block
|
38
39
|
raise Error, "#{type} does not support through associations" unless assoc_type = Sequel.synchronize{ASSOCIATION_THROUGH_TYPES[type]}
|
39
40
|
|
40
|
-
result = find_association_path
|
41
|
+
result = find_association_path(**opts, name: name, models: self, from_through: true)
|
41
42
|
|
42
43
|
# Remove the last table if it matches the destination table
|
43
44
|
dest_model = result[:models].pop
|
@@ -51,7 +52,7 @@ module Sequel
|
|
51
52
|
end
|
52
53
|
|
53
54
|
# Create the association
|
54
|
-
if assoc_type.to_s.
|
55
|
+
if assoc_type.to_s.end_with? "_through_many"
|
55
56
|
# *_through_many has a path argument
|
56
57
|
self.send(assoc_type,
|
57
58
|
name,
|
@@ -78,7 +79,7 @@ module Sequel
|
|
78
79
|
end
|
79
80
|
|
80
81
|
# Recurses through associations until a path to the destination is completed
|
81
|
-
def find_association_path opts
|
82
|
+
def find_association_path **opts
|
82
83
|
|
83
84
|
# Initialize arguments
|
84
85
|
[:tables, :keys, :through, :models, :assocs].each do |k|
|
@@ -125,7 +126,7 @@ module Sequel
|
|
125
126
|
opts[:assocs].push assoc
|
126
127
|
|
127
128
|
# Handle *_through_many associations
|
128
|
-
if assoc[:type].to_s.
|
129
|
+
if assoc[:type].to_s.end_with? "_through_many"
|
129
130
|
|
130
131
|
opts[:through].push assoc[:originally_through]
|
131
132
|
opts[:from_through] = true
|
@@ -138,7 +139,7 @@ module Sequel
|
|
138
139
|
return begin
|
139
140
|
model = search.shift
|
140
141
|
raise NoAssociationPath, opts unless model
|
141
|
-
self.find_association_path
|
142
|
+
self.find_association_path(**opts, models: opts[:models] + [model])
|
142
143
|
rescue MissingAssociation
|
143
144
|
# Try the next model in the search path
|
144
145
|
retry
|
@@ -154,7 +155,7 @@ module Sequel
|
|
154
155
|
opts[:through].push assoc[:using] || assoc[:through]
|
155
156
|
opts[:from_through] = true
|
156
157
|
opts[:using] = nil
|
157
|
-
return self.find_association_path
|
158
|
+
return self.find_association_path(**opts)
|
158
159
|
end
|
159
160
|
|
160
161
|
# Otherwise, add the new table to the stack
|
@@ -187,7 +188,7 @@ module Sequel
|
|
187
188
|
# Check for a source association
|
188
189
|
opts[:through].push opts[:using] || opts[:name]
|
189
190
|
opts[:from_through] = false
|
190
|
-
return self.find_association_path
|
191
|
+
return self.find_association_path(**opts)
|
191
192
|
|
192
193
|
end
|
193
194
|
|
data/lib/sequel/through.rb
CHANGED
data/sequel-through.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-through
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenaniah Cerny
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -81,7 +81,7 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- bin/console
|
83
83
|
- bin/setup
|
84
|
-
- lib/sequel/plugins/
|
84
|
+
- lib/sequel/plugins/cyclical_through_associations.rb
|
85
85
|
- lib/sequel/plugins/through_associations.rb
|
86
86
|
- lib/sequel/through.rb
|
87
87
|
- sequel-through.gemspec
|