adjustable_schema 0.6.0 → 0.7.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 +42 -6
- data/lib/adjustable_schema/active_record/association/naming.rb +9 -4
- data/lib/adjustable_schema/active_record/association.rb +29 -3
- data/lib/adjustable_schema/config.rb +2 -2
- data/lib/adjustable_schema/engine.rb +6 -6
- data/lib/adjustable_schema/version.rb +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: 8f905a89a0f7ab6c7c5c1f1feaa92a8fdb90f5e58003b164fed5ac55096fa258
|
|
4
|
+
data.tar.gz: cf1c34dd8cce3497e64635ab1c59f079e257961979e55e9d31b30ae1aefeb4ff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 892d3cc2cdfa8d6cd7d2405289e1417a54be8c8767fefa557208a2d7880d346e4c362adc1a5dc911d2091be1b172a15f88e3060c0c06b29b135a61efb773f5bb
|
|
7
|
+
data.tar.gz: 145e68868903178eb0a6a82ed224912f657cdb7004f91df9cf70bec4c9c32241b1cd216515ee913369e6914f865007214d1a7aad5b5604664b73408974f5a323
|
data/README.md
CHANGED
|
@@ -56,10 +56,12 @@ book.editor_people
|
|
|
56
56
|
|
|
57
57
|
#### Special cases
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
##### "Actor-like" models
|
|
60
|
+
|
|
61
|
+
In case you have set up relationships with `User` model you'll get a slightly different naming:
|
|
60
62
|
|
|
61
63
|
``` ruby
|
|
62
|
-
AdjustableSchema::Relationship.seed! User => Book, %w[author editor]
|
|
64
|
+
AdjustableSchema::Relationship.seed! User => Book, roles: %w[author editor]
|
|
63
65
|
```
|
|
64
66
|
|
|
65
67
|
``` ruby
|
|
@@ -71,11 +73,42 @@ book.editors
|
|
|
71
73
|
The list of models to be handled this way can be set with `actor_model_names` configuration parameter.
|
|
72
74
|
It includes `User` by default.
|
|
73
75
|
|
|
74
|
-
|
|
76
|
+
##### Self-referencing models
|
|
77
|
+
|
|
78
|
+
You may want to set up self-targeted relationships:
|
|
79
|
+
|
|
80
|
+
``` ruby
|
|
81
|
+
AdjustableSchema::Relationship.seed! Person, roles: %w[friend]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
In this case you'll get these associations:
|
|
85
|
+
|
|
86
|
+
``` ruby
|
|
87
|
+
person.parents
|
|
88
|
+
person.children # for all the children
|
|
89
|
+
person.people # for "roleless" children, not friends
|
|
90
|
+
person.friends
|
|
91
|
+
person.friended_people
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
If you prefer a different naming over `parents` & `children`, you can configure it like this:
|
|
75
95
|
|
|
76
|
-
|
|
96
|
+
```ruby
|
|
97
|
+
AdjustableSchema::Engine.configure do
|
|
98
|
+
config.names[:associations][:source][:self] = :effect
|
|
99
|
+
config.names[:associations][:target][:self] = :cause
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Thus, for the self-referenced `Event`s, you'll get:
|
|
104
|
+
|
|
105
|
+
``` ruby
|
|
106
|
+
event.causes
|
|
107
|
+
event.effects
|
|
108
|
+
```
|
|
77
109
|
|
|
78
110
|
## Installation
|
|
111
|
+
|
|
79
112
|
Add this line to your application's Gemfile:
|
|
80
113
|
|
|
81
114
|
```ruby
|
|
@@ -83,13 +116,15 @@ gem "adjustable_schema"
|
|
|
83
116
|
```
|
|
84
117
|
|
|
85
118
|
And then execute:
|
|
119
|
+
|
|
86
120
|
```bash
|
|
87
|
-
|
|
121
|
+
bundle
|
|
88
122
|
```
|
|
89
123
|
|
|
90
124
|
Or install it yourself as:
|
|
125
|
+
|
|
91
126
|
```bash
|
|
92
|
-
|
|
127
|
+
gem install adjustable_schema
|
|
93
128
|
```
|
|
94
129
|
|
|
95
130
|
## Contributing
|
|
@@ -101,4 +136,5 @@ $ gem install adjustable_schema
|
|
|
101
136
|
5. Create new Pull Request
|
|
102
137
|
|
|
103
138
|
## License
|
|
139
|
+
|
|
104
140
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -9,7 +9,9 @@ module AdjustableSchema
|
|
|
9
9
|
module Inflections
|
|
10
10
|
refine String do
|
|
11
11
|
def passivize
|
|
12
|
-
|
|
12
|
+
self
|
|
13
|
+
.sub(/(author)$/, '\\2ed')
|
|
14
|
+
.sub(/(e*|ed|ing|[eo]r|ant|(t)ion)$/, '\\2ed')
|
|
13
15
|
end
|
|
14
16
|
end
|
|
15
17
|
end
|
|
@@ -36,7 +38,7 @@ module AdjustableSchema
|
|
|
36
38
|
private
|
|
37
39
|
|
|
38
40
|
memoize def name_with_role
|
|
39
|
-
if
|
|
41
|
+
if loop?
|
|
40
42
|
{
|
|
41
43
|
source: role.name,
|
|
42
44
|
target: "#{role.name.passivize}_#{target_name}",
|
|
@@ -50,13 +52,16 @@ module AdjustableSchema
|
|
|
50
52
|
end
|
|
51
53
|
|
|
52
54
|
memoize def name_without_role
|
|
53
|
-
if
|
|
55
|
+
if loop?
|
|
54
56
|
Config.association_directions
|
|
55
|
-
.
|
|
57
|
+
.self[direction]
|
|
56
58
|
else
|
|
57
59
|
target_name
|
|
58
60
|
end
|
|
59
61
|
end
|
|
62
|
+
|
|
63
|
+
def name_for_any = :"#{name.to_s.singularize.passivize}"
|
|
64
|
+
def name_for_none = :"#{name.to_s.singularize}less"
|
|
60
65
|
end
|
|
61
66
|
end
|
|
62
67
|
end
|
|
@@ -15,19 +15,22 @@ module AdjustableSchema
|
|
|
15
15
|
class_name: target.name
|
|
16
16
|
}) do
|
|
17
17
|
include Scopes
|
|
18
|
-
include Scopes::Recursive if association.
|
|
18
|
+
include Scopes::Recursive if association.loop?
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
define_scopes
|
|
22
|
+
define_methods
|
|
23
|
+
|
|
21
24
|
unless role
|
|
22
25
|
has_many target_name.tableize.to_sym, -> { roleless }, **options if
|
|
23
|
-
|
|
26
|
+
loop?
|
|
24
27
|
|
|
25
28
|
define_role_methods
|
|
26
29
|
end
|
|
27
30
|
end
|
|
28
31
|
end
|
|
29
32
|
|
|
30
|
-
def
|
|
33
|
+
def loop? = target == owner
|
|
31
34
|
|
|
32
35
|
private
|
|
33
36
|
|
|
@@ -40,6 +43,29 @@ module AdjustableSchema
|
|
|
40
43
|
end
|
|
41
44
|
end
|
|
42
45
|
|
|
46
|
+
def define_scopes
|
|
47
|
+
name = relationships_name
|
|
48
|
+
|
|
49
|
+
{
|
|
50
|
+
name_for_any => -> { where.associated name },
|
|
51
|
+
name_for_none => -> { where.missing name },
|
|
52
|
+
}
|
|
53
|
+
.reject { owner.singleton_class.method_defined? _1 }
|
|
54
|
+
.each { owner.scope _1, _2 }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def define_methods
|
|
58
|
+
name = self.name
|
|
59
|
+
|
|
60
|
+
{
|
|
61
|
+
name_for_any => -> { send(name).any? },
|
|
62
|
+
name_for_none => -> { send(name).none? },
|
|
63
|
+
}
|
|
64
|
+
.transform_keys {"#{_1}?" }
|
|
65
|
+
.reject { owner.method_defined? _1 }
|
|
66
|
+
.each { owner.define_method _1, &_2 }
|
|
67
|
+
end
|
|
68
|
+
|
|
43
69
|
def define_role_methods
|
|
44
70
|
name = self.name
|
|
45
71
|
|
|
@@ -19,11 +19,11 @@ module AdjustableSchema
|
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
def
|
|
22
|
+
def self = config :self
|
|
23
23
|
|
|
24
24
|
def recursive
|
|
25
25
|
config.values.to_h do
|
|
26
|
-
[ _1[:
|
|
26
|
+
[ _1[:self].to_s.pluralize.to_sym, _1[:recursive].to_sym ]
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
@@ -7,14 +7,14 @@ module AdjustableSchema
|
|
|
7
7
|
config.names = {
|
|
8
8
|
associations: {
|
|
9
9
|
source: {
|
|
10
|
-
shortcut:
|
|
11
|
-
|
|
12
|
-
recursive:
|
|
10
|
+
shortcut: :of,
|
|
11
|
+
self: :child,
|
|
12
|
+
recursive: :descendants,
|
|
13
13
|
},
|
|
14
14
|
target: {
|
|
15
|
-
shortcut:
|
|
16
|
-
|
|
17
|
-
recursive:
|
|
15
|
+
shortcut: :to,
|
|
16
|
+
self: :parent,
|
|
17
|
+
recursive: :ancestors,
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
20
|
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: adjustable_schema
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alexander Senko
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-02-
|
|
11
|
+
date: 2024-02-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -111,7 +111,7 @@ licenses:
|
|
|
111
111
|
metadata:
|
|
112
112
|
homepage_uri: https://github.com/Alexander-Senko/adjustable_schema
|
|
113
113
|
source_code_uri: https://github.com/Alexander-Senko/adjustable_schema
|
|
114
|
-
changelog_uri: https://github.com/Alexander-Senko/adjustable_schema/blob/v0.
|
|
114
|
+
changelog_uri: https://github.com/Alexander-Senko/adjustable_schema/blob/v0.7.0/CHANGELOG.md
|
|
115
115
|
post_install_message:
|
|
116
116
|
rdoc_options: []
|
|
117
117
|
require_paths:
|