rails_dynamic_associations 0.4.1 → 0.4.2
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 +5 -1
- data/app/models/relation.rb +6 -5
- data/config/initializers/model_associations.rb +4 -5
- data/lib/rails_dynamic_associations/active_record/associations.rb +10 -2
- data/lib/rails_dynamic_associations/active_record/relations.rb +2 -8
- data/lib/rails_dynamic_associations/config.rb +12 -11
- data/lib/rails_dynamic_associations/version.rb +8 -1
- data/lib/rails_dynamic_associations.rb +3 -0
- metadata +88 -79
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f1b548f08bbef239327e6a0ba9246dd44f2efe97719300a315166a87f677ce57
|
|
4
|
+
data.tar.gz: d55cd5fc938ea3afb297ff698d073d6954cf3d5c652754e8f68d6b5bfa20774f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b2389b1f63e69b8ebf2f13a5f7626f28aa46f4c4267e6ccc2b4b8b9419b93df967fc9bd1f57c8de8eecbd61441b9a5fed215cea72cf2e3b3dd86d81dc93b3f3f
|
|
7
|
+
data.tar.gz: 3fcb306ed4985a8b5696b4c810aaede9f08eb5d84b6036b1d8086c556832639901c39aa47857bf417420f99a883967f171bd72c61cb3361096fe8cbc11fb7195
|
data/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Rails Dynamic Associations
|
|
2
2
|
|
|
3
3
|
Define your model associations in the database without changing the schema or models.
|
|
4
4
|
|
|
5
|
+
## DEPRECATED
|
|
6
|
+
|
|
7
|
+
Use [Adjustable Schema](https://github.com/Alexander-Senko/adjustable_schema) instead. It has a similar API, though it's not backward-compatible.
|
|
8
|
+
|
|
5
9
|
## Features
|
|
6
10
|
|
|
7
11
|
* Creates associations for your models when application starts.
|
data/app/models/relation.rb
CHANGED
|
@@ -53,7 +53,7 @@ class Relation < ActiveRecord::Base
|
|
|
53
53
|
scope :named, -> (*names) {
|
|
54
54
|
case names
|
|
55
55
|
when [] # i.e. `named`
|
|
56
|
-
where.
|
|
56
|
+
where.associated :role
|
|
57
57
|
else
|
|
58
58
|
with_roles { named *names }
|
|
59
59
|
end
|
|
@@ -61,15 +61,16 @@ class Relation < ActiveRecord::Base
|
|
|
61
61
|
|
|
62
62
|
def self.with_roles &block
|
|
63
63
|
joins(:role).merge(
|
|
64
|
-
|
|
64
|
+
Role.instance_eval &block
|
|
65
65
|
).uniq
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def self.seed source, target, roles = nil
|
|
69
69
|
(roles.present? ? Role.find_or_create_named(roles) : [ nil ]).map do |role|
|
|
70
|
-
create
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
create role: role do
|
|
71
|
+
_1[:source_type] = source
|
|
72
|
+
_1[:target_type] = target
|
|
73
|
+
end
|
|
73
74
|
end
|
|
74
75
|
end
|
|
75
76
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
ActiveSupport.on_load :model_class do
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
end if self != Relation and Relation.table_exists? # needed for DB migrations & schema initializing
|
|
2
|
+
next if self == Relation
|
|
3
|
+
next unless Relation.table_exists?
|
|
4
|
+
|
|
5
|
+
setup_relations
|
|
7
6
|
end
|
|
@@ -4,11 +4,19 @@ module RailsDynamicAssociations::ActiveRecord
|
|
|
4
4
|
module Associations
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
class_methods do
|
|
8
8
|
include RailsDynamicAssociations::Config
|
|
9
9
|
|
|
10
10
|
protected
|
|
11
11
|
|
|
12
|
+
def setup_relations
|
|
13
|
+
relations.each do |type, relations|
|
|
14
|
+
for relation in relations.select(&:"#{type}_type") do
|
|
15
|
+
setup_relation type, relation.send("#{type}_type").constantize, relation.role
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
12
20
|
def setup_relation type, target = self, role = nil
|
|
13
21
|
define_association type, target
|
|
14
22
|
define_association type, target, role if role
|
|
@@ -30,7 +38,7 @@ module RailsDynamicAssociations::ActiveRecord
|
|
|
30
38
|
next if reflect_on_association association
|
|
31
39
|
|
|
32
40
|
has_many association, role && -> { where role_id: role.id },
|
|
33
|
-
as: association_directions.opposite(type), class_name: 'Relation'
|
|
41
|
+
as: association_directions.opposite(to: type), class_name: 'Relation'
|
|
34
42
|
end
|
|
35
43
|
end
|
|
36
44
|
|
|
@@ -9,21 +9,15 @@ module RailsDynamicAssociations::ActiveRecord
|
|
|
9
9
|
include ClassAndInstanceMethods
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
class_methods do
|
|
13
13
|
include RailsDynamicAssociations::Config
|
|
14
14
|
|
|
15
15
|
def relations
|
|
16
16
|
@relations ||=
|
|
17
17
|
association_directions.to_h do
|
|
18
|
-
[ _1, Relation.abstract.send(association_directions.
|
|
18
|
+
[ _1, Relation.abstract.send(association_directions.shortcuts.opposite[_1], self) ]
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
|
-
|
|
22
|
-
RailsDynamicAssociations::Config.association_directions.opposite_shortcuts.each &-> ((association, method)) do
|
|
23
|
-
define_method "#{association}_relations" do
|
|
24
|
-
Relation.send method, self
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
21
|
end
|
|
28
22
|
|
|
29
23
|
module ClassAndInstanceMethods
|
|
@@ -2,7 +2,16 @@ module RailsDynamicAssociations
|
|
|
2
2
|
module Config
|
|
3
3
|
module Naming
|
|
4
4
|
def shortcuts
|
|
5
|
-
|
|
5
|
+
@shortcuts ||=
|
|
6
|
+
config(:shortcut).tap do |shortcuts|
|
|
7
|
+
def shortcuts.opposite to: nil
|
|
8
|
+
if to
|
|
9
|
+
values.reject { _1 == to }.sole
|
|
10
|
+
else
|
|
11
|
+
transform_values { opposite to: _1 }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
6
15
|
end
|
|
7
16
|
|
|
8
17
|
def selfed
|
|
@@ -17,16 +26,8 @@ module RailsDynamicAssociations
|
|
|
17
26
|
end
|
|
18
27
|
end
|
|
19
28
|
|
|
20
|
-
def opposite
|
|
21
|
-
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def opposite_shortcuts
|
|
25
|
-
shortcuts.each_with_object({}) do |(key, value), hash|
|
|
26
|
-
hash[key] = shortcuts.values.find do |v|
|
|
27
|
-
v != value
|
|
28
|
-
end
|
|
29
|
-
end
|
|
29
|
+
def opposite to:
|
|
30
|
+
reject { _1 == to }.sole
|
|
30
31
|
end
|
|
31
32
|
|
|
32
33
|
private
|
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
module RailsDynamicAssociations
|
|
2
|
-
VERSION = '0.4.
|
|
2
|
+
VERSION = '0.4.2'
|
|
3
|
+
|
|
4
|
+
DEPRECATION_MESSAGE = <<~TEXT
|
|
5
|
+
Rails Dynamic Associations are deprecated.
|
|
6
|
+
|
|
7
|
+
Please migrate to Adjustable Schema: https://github.com/Alexander-Senko/adjustable_schema.
|
|
8
|
+
It has a similar API, but is not backward-compatible.
|
|
9
|
+
TEXT
|
|
3
10
|
end
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
require 'rails_model_load_hook'
|
|
2
2
|
|
|
3
|
+
require 'rails_dynamic_associations/version'
|
|
3
4
|
require 'rails_dynamic_associations/engine'
|
|
4
5
|
require 'core_ext/string'
|
|
5
6
|
|
|
6
7
|
module RailsDynamicAssociations
|
|
7
8
|
autoload :Config, 'rails_dynamic_associations/config'
|
|
8
9
|
autoload :ActiveRecord, 'rails_dynamic_associations/active_record'
|
|
10
|
+
|
|
11
|
+
warn RailsDynamicAssociations::DEPRECATION_MESSAGE, category: :deprecation
|
|
9
12
|
end
|
metadata
CHANGED
|
@@ -1,104 +1,113 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_dynamic_associations
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Alexander Senko
|
|
8
|
-
autorequire:
|
|
7
|
+
- Alexander Senko
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
- !ruby/object:Gem::
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: "7.0"
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ~>
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: "7.0"
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rails_model_load_hook
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "0"
|
|
36
|
+
type: :runtime
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
- !ruby/object:Gem::Dependency
|
|
45
|
+
name: sqlite3
|
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
-
|
|
49
|
+
- ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
type: :development
|
|
53
|
+
prerelease: false
|
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
-
|
|
57
|
+
- ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
description: Define your model associations in the database without changing the schema or models.
|
|
57
61
|
email:
|
|
58
|
-
- Alexander.Senko@gmail.com
|
|
62
|
+
- Alexander.Senko@gmail.com
|
|
59
63
|
executables: []
|
|
60
64
|
extensions: []
|
|
61
65
|
extra_rdoc_files: []
|
|
62
66
|
files:
|
|
63
|
-
- MIT-LICENSE
|
|
64
|
-
- README.md
|
|
65
|
-
- Rakefile
|
|
66
|
-
- app/models/relation.rb
|
|
67
|
-
- app/models/role.rb
|
|
68
|
-
- config/initializers/helpers.rb
|
|
69
|
-
- config/initializers/model_associations.rb
|
|
70
|
-
- db/migrate/01_create_roles.rb
|
|
71
|
-
- db/migrate/02_create_relations.rb
|
|
72
|
-
- lib/core_ext/string.rb
|
|
73
|
-
- lib/rails_dynamic_associations.rb
|
|
74
|
-
- lib/rails_dynamic_associations/active_record.rb
|
|
75
|
-
- lib/rails_dynamic_associations/active_record/associations.rb
|
|
76
|
-
- lib/rails_dynamic_associations/active_record/relations.rb
|
|
77
|
-
- lib/rails_dynamic_associations/config.rb
|
|
78
|
-
- lib/rails_dynamic_associations/engine.rb
|
|
79
|
-
- lib/rails_dynamic_associations/version.rb
|
|
80
|
-
- lib/tasks/rails_dynamic_associations_tasks.rake
|
|
81
|
-
homepage: https://github.com/Alexander-Senko/rails_dynamic_associations
|
|
67
|
+
- MIT-LICENSE
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- app/models/relation.rb
|
|
71
|
+
- app/models/role.rb
|
|
72
|
+
- config/initializers/helpers.rb
|
|
73
|
+
- config/initializers/model_associations.rb
|
|
74
|
+
- db/migrate/01_create_roles.rb
|
|
75
|
+
- db/migrate/02_create_relations.rb
|
|
76
|
+
- lib/core_ext/string.rb
|
|
77
|
+
- lib/rails_dynamic_associations.rb
|
|
78
|
+
- lib/rails_dynamic_associations/active_record.rb
|
|
79
|
+
- lib/rails_dynamic_associations/active_record/associations.rb
|
|
80
|
+
- lib/rails_dynamic_associations/active_record/relations.rb
|
|
81
|
+
- lib/rails_dynamic_associations/config.rb
|
|
82
|
+
- lib/rails_dynamic_associations/engine.rb
|
|
83
|
+
- lib/rails_dynamic_associations/version.rb
|
|
84
|
+
- lib/tasks/rails_dynamic_associations_tasks.rake
|
|
85
|
+
homepage: "https://github.com/Alexander-Senko/rails_dynamic_associations"
|
|
82
86
|
licenses:
|
|
83
|
-
- MIT
|
|
87
|
+
- MIT
|
|
84
88
|
metadata: {}
|
|
85
|
-
post_install_message:
|
|
89
|
+
post_install_message: |
|
|
90
|
+
Rails Dynamic Associations are deprecated.
|
|
91
|
+
|
|
92
|
+
Please migrate to Adjustable Schema: https://github.com/Alexander-Senko/adjustable_schema.
|
|
93
|
+
It has a similar API, but is not backward-compatible.
|
|
86
94
|
rdoc_options: []
|
|
87
95
|
require_paths:
|
|
88
|
-
- lib
|
|
96
|
+
- lib
|
|
89
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
98
|
requirements:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
99
|
+
-
|
|
100
|
+
- ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: "2.7"
|
|
94
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
104
|
requirements:
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
105
|
+
-
|
|
106
|
+
- ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: "0"
|
|
99
109
|
requirements: []
|
|
100
|
-
rubygems_version:
|
|
101
|
-
signing_key:
|
|
110
|
+
rubygems_version: 4.1.0.dev
|
|
102
111
|
specification_version: 4
|
|
103
112
|
summary: DB-driven model associations for Rails.
|
|
104
113
|
test_files: []
|