rails_dynamic_associations 0.3.1 → 0.4.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 +4 -4
- data/README.md +4 -1
- data/app/models/relation.rb +10 -10
- data/lib/core_ext/string.rb +1 -1
- data/lib/rails_dynamic_associations/active_record/associations.rb +5 -1
- 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 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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
@@ -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
@@ -19,6 +19,10 @@ module RailsDynamicAssociations::ActiveRecord
|
|
19
19
|
.each { |association, method| define_recursive_methods association, method }
|
20
20
|
end
|
21
21
|
|
22
|
+
def actor?
|
23
|
+
actor_models.any? { self <= _1 }
|
24
|
+
end
|
25
|
+
|
22
26
|
private
|
23
27
|
|
24
28
|
def define_relations_association type, target = self, role = nil
|
@@ -93,7 +97,7 @@ module RailsDynamicAssociations::ActiveRecord
|
|
93
97
|
end
|
94
98
|
|
95
99
|
def association_name_with_role type, target, role
|
96
|
-
if target == self or target
|
100
|
+
if target == self or target.actor?
|
97
101
|
{
|
98
102
|
source: role.name,
|
99
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,15 +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
|
-
rubygems_version: 3.
|
101
|
-
signing_key:
|
100
|
+
rubygems_version: 3.4.0.dev
|
101
|
+
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: DB-driven model associations for Rails.
|
104
104
|
test_files: []
|