rails_dynamic_associations 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +4 -1
- data/app/models/relation.rb +11 -11
- data/lib/core_ext/string.rb +1 -1
- data/lib/rails_dynamic_associations/active_record/associations.rb +21 -10
- data/lib/rails_dynamic_associations/active_record/relations.rb +10 -1
- data/lib/rails_dynamic_associations/config.rb +6 -0
- data/lib/rails_dynamic_associations/engine.rb +4 -0
- data/lib/rails_dynamic_associations/version.rb +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5f6bd7977b55a33be10430aa26ce160f7a2735ac42f787ce1e912a408a937575
|
4
|
+
data.tar.gz: fdf7eb97220f6a909791dfb2b05501b498058205901dfc0711d74a5f7a7fa62b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ccf96404c1fcd13e3f0137f25c606dcaef612f793ed315a7ff20ac4b20320d2f45abfa0b43823d46e27d2b19a06b88cc3341448ec1b86596926a67f52216c5cc
|
7
|
+
data.tar.gz: 8e74754a0bbc56c286003378cd43b134665cd8598f6102f983fcbae31d7e44fb315181a707c263a4802da367d0906397db35a2cb5e197ae9f3c8fea7834b77cb
|
data/README.md
CHANGED
@@ -63,7 +63,7 @@ You will get:
|
|
63
63
|
book.editor_people
|
64
64
|
```
|
65
65
|
|
66
|
-
####
|
66
|
+
#### Special cases
|
67
67
|
|
68
68
|
In case you have set up relations with a `User` model you'll get a slightly different naming:
|
69
69
|
|
@@ -80,6 +80,9 @@ In case you have set up relations with a `User` model you'll get a slightly diff
|
|
80
80
|
book.editors
|
81
81
|
```
|
82
82
|
|
83
|
+
The list of models to be handled this way can be set with `actor_model_names` configuration parameter.
|
84
|
+
It includes `User` by default.
|
85
|
+
|
83
86
|
###### TODO
|
84
87
|
|
85
88
|
* Describe self-referential associations.
|
data/app/models/relation.rb
CHANGED
@@ -9,7 +9,7 @@ class Relation < ActiveRecord::Base
|
|
9
9
|
references(:roles).includes :role
|
10
10
|
}
|
11
11
|
|
12
|
-
association_directions.shortcuts.each &-> (association, method) do
|
12
|
+
association_directions.shortcuts.each &-> ((association, method)) do
|
13
13
|
scope "#{method}_abstract", -> (object = nil) {
|
14
14
|
if object then
|
15
15
|
send("#{method}_abstract").
|
@@ -26,15 +26,17 @@ class Relation < ActiveRecord::Base
|
|
26
26
|
|
27
27
|
scope method, -> (object) {
|
28
28
|
case object
|
29
|
-
when nil
|
30
|
-
|
31
|
-
when
|
29
|
+
when ActiveRecord::Base, nil
|
30
|
+
where association => object
|
31
|
+
when Class
|
32
|
+
where "#{association}_type" => object.ancestors.select { _1 <= object.base_class }.map(&:name)
|
33
|
+
when ActiveRecord::Relation
|
34
|
+
send(method, object.klass)
|
35
|
+
.where "#{association}_id" => object
|
36
|
+
when Symbol
|
32
37
|
send "#{method}_#{object}"
|
33
|
-
when Class then
|
34
|
-
where "#{association}_type" => object.base_class.name
|
35
38
|
else
|
36
|
-
|
37
|
-
"#{association}_id" => object.id
|
39
|
+
raise ArgumentError, "no relations for #{object.inspect}"
|
38
40
|
end
|
39
41
|
}
|
40
42
|
end
|
@@ -50,9 +52,7 @@ class Relation < ActiveRecord::Base
|
|
50
52
|
|
51
53
|
scope :named, -> (*names) {
|
52
54
|
case names
|
53
|
-
when [
|
54
|
-
# all
|
55
|
-
when [] then # i.e. `named`
|
55
|
+
when [] # i.e. `named`
|
56
56
|
where.not role_id: nil
|
57
57
|
else
|
58
58
|
with_roles { named *names }
|
data/lib/core_ext/string.rb
CHANGED
@@ -13,24 +13,31 @@ module RailsDynamicAssociations::ActiveRecord
|
|
13
13
|
define_association type, target
|
14
14
|
define_association type, target, role if role
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
association_directions.recursive
|
17
|
+
.select { |association, method| reflect_on_association association }
|
18
|
+
.reject { |association, method| method_defined? method }
|
19
|
+
.each { |association, method| define_recursive_methods association, method }
|
20
|
+
end
|
21
|
+
|
22
|
+
def actor?
|
23
|
+
actor_models.any? { self <= _1 }
|
19
24
|
end
|
20
25
|
|
21
26
|
private
|
22
27
|
|
23
28
|
def define_relations_association type, target = self, role = nil
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
relations_association_name(type, target, role).tap do |association|
|
30
|
+
next if reflect_on_association association
|
31
|
+
|
32
|
+
has_many association, role && -> { where role_id: role.id },
|
33
|
+
as: association_directions.opposite(type), class_name: 'Relation'
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
37
|
def define_association type, target = self, role = nil
|
33
|
-
|
38
|
+
association_name(type, target, role).tap do |association|
|
39
|
+
next if reflect_on_association association
|
40
|
+
|
34
41
|
has_many association,
|
35
42
|
through: define_relations_association(type, target, role),
|
36
43
|
source: type,
|
@@ -77,6 +84,10 @@ module RailsDynamicAssociations::ActiveRecord
|
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
87
|
+
def relations_association_name type, target = self, role = nil
|
88
|
+
:"#{role ? association_name(type, target, role).to_s.singularize : type}_relations"
|
89
|
+
end
|
90
|
+
|
80
91
|
def association_name type, target = self, role = nil
|
81
92
|
if role then
|
82
93
|
association_name_with_role type, target, role
|
@@ -86,7 +97,7 @@ module RailsDynamicAssociations::ActiveRecord
|
|
86
97
|
end
|
87
98
|
|
88
99
|
def association_name_with_role type, target, role
|
89
|
-
if target == self or target
|
100
|
+
if target == self or target.actor?
|
90
101
|
{
|
91
102
|
source: role.name,
|
92
103
|
target: "#{role.name.passivize}_#{target.name.split('::').reverse.join}",
|
@@ -10,7 +10,16 @@ module RailsDynamicAssociations::ActiveRecord
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
-
RailsDynamicAssociations::Config
|
13
|
+
include RailsDynamicAssociations::Config
|
14
|
+
|
15
|
+
def relations
|
16
|
+
@relations ||=
|
17
|
+
association_directions.to_h do
|
18
|
+
[ _1, Relation.abstract.send(association_directions.opposite_shortcuts[_1], self) ]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
association_directions.opposite_shortcuts.each &-> ((association, method)) do
|
14
23
|
define_method "#{association}_relations" do
|
15
24
|
Relation.send method, self
|
16
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_dynamic_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Senko
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -78,11 +78,11 @@ files:
|
|
78
78
|
- lib/rails_dynamic_associations/engine.rb
|
79
79
|
- lib/rails_dynamic_associations/version.rb
|
80
80
|
- lib/tasks/rails_dynamic_associations_tasks.rake
|
81
|
-
homepage: https://github.com/
|
81
|
+
homepage: https://github.com/Alexander-Senko/rails_dynamic_associations
|
82
82
|
licenses:
|
83
83
|
- MIT
|
84
84
|
metadata: {}
|
85
|
-
post_install_message:
|
85
|
+
post_install_message:
|
86
86
|
rdoc_options: []
|
87
87
|
require_paths:
|
88
88
|
- lib
|
@@ -90,16 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
requirements:
|
91
91
|
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version: '
|
93
|
+
version: '2.7'
|
94
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
95
|
requirements:
|
96
96
|
- - ">="
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
|
101
|
-
|
102
|
-
signing_key:
|
100
|
+
rubygems_version: 3.4.0.dev
|
101
|
+
signing_key:
|
103
102
|
specification_version: 4
|
104
103
|
summary: DB-driven model associations for Rails.
|
105
104
|
test_files: []
|