dynamic-active-model 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dynamic-active-model/foreign_key.rb +3 -3
- data/lib/dynamic-active-model/relations.rb +17 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6228c3f790ef52e03bb5303386c5c706c0a3f35ae79697c790dcd8ab85a6512d
|
4
|
+
data.tar.gz: b1c03bf64c5593bd7348709a404ab131dff69ba854ed1ecc26b1743395145aca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6684da72fa55ca7fc969f7de3136df39afabfcc7b4ca5a5c6bd72b22f2e1f4644f15a9e15570d72d84ab4b147d387655e0683defc6fdddf6bc54e3f7fd232885
|
7
|
+
data.tar.gz: 357df514a4846cd8641fba61c1b8bd714c37f91545f79b373d559bd2bff7c47d7af83d4698a0ff05674e4fd3fd478ebf37d108234253340ade401c9a795f409f
|
@@ -21,11 +21,11 @@ module DynamicActiveModel
|
|
21
21
|
def initialize(model)
|
22
22
|
@model = model
|
23
23
|
@keys = {}
|
24
|
-
add(generate_foreign_key(model.table_name)
|
24
|
+
add(generate_foreign_key(model.table_name))
|
25
25
|
end
|
26
26
|
|
27
|
-
def add(key, relationship_name)
|
28
|
-
@keys[key] = relationship_name
|
27
|
+
def add(key, relationship_name = nil)
|
28
|
+
@keys[key] = relationship_name || model.table_name.underscore
|
29
29
|
end
|
30
30
|
|
31
31
|
def generate_foreign_key(table_name)
|
@@ -13,7 +13,7 @@ module DynamicActiveModel
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def add_foreign_key(table_name, foreign_key, relationship_name)
|
16
|
+
def add_foreign_key(table_name, foreign_key, relationship_name = nil)
|
17
17
|
@foreign_keys[table_name].add(foreign_key, relationship_name)
|
18
18
|
end
|
19
19
|
|
@@ -25,10 +25,10 @@ module DynamicActiveModel
|
|
25
25
|
model.column_names.each do |column_name|
|
26
26
|
next unless foreign_key_to_models[column_name.downcase]
|
27
27
|
|
28
|
-
foreign_key_to_models[column_name.downcase].each do |foreign_model, relationship_name
|
28
|
+
foreign_key_to_models[column_name.downcase].each do |foreign_model, relationship_name|
|
29
29
|
next if foreign_model == model
|
30
30
|
|
31
|
-
add_relationships(relationship_name, model, foreign_model, column_name
|
31
|
+
add_relationships(relationship_name, model, foreign_model, column_name)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -36,14 +36,9 @@ module DynamicActiveModel
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
def add_relationships(relationship_name, model, belongs_to_model, foreign_key
|
39
|
+
def add_relationships(relationship_name, model, belongs_to_model, foreign_key)
|
40
40
|
add_belongs_to(relationship_name, model, belongs_to_model, foreign_key)
|
41
|
-
add_has_many(
|
42
|
-
additional ? relationship_name : nil,
|
43
|
-
belongs_to_model,
|
44
|
-
model,
|
45
|
-
foreign_key
|
46
|
-
)
|
41
|
+
add_has_many(relationship_name, belongs_to_model, model, foreign_key)
|
47
42
|
end
|
48
43
|
|
49
44
|
def add_belongs_to(relationship_name, model, belongs_to_model, foreign_key)
|
@@ -56,10 +51,8 @@ module DynamicActiveModel
|
|
56
51
|
end
|
57
52
|
|
58
53
|
def add_has_many(relationship_name, model, has_many_model, foreign_key)
|
59
|
-
relationship_name = relationship_name ? (relationship_name + '_') : ''
|
60
|
-
relationship_name += has_many_model.table_name
|
61
54
|
model.has_many(
|
62
|
-
relationship_name
|
55
|
+
generate_has_many_association_name(relationship_name, model, has_many_model),
|
63
56
|
class_name: has_many_model.name,
|
64
57
|
foreign_key: foreign_key,
|
65
58
|
primary_key: has_many_model.primary_key
|
@@ -68,13 +61,21 @@ module DynamicActiveModel
|
|
68
61
|
|
69
62
|
def create_foreign_key_to_model_map
|
70
63
|
@foreign_keys.values.each_with_object({}) do |foreign_key, hsh|
|
71
|
-
additional = false
|
72
64
|
foreign_key.keys.each do |key, relationship_name|
|
73
65
|
hsh[key.downcase] ||= []
|
74
|
-
hsh[key.downcase] << [foreign_key.model, relationship_name
|
75
|
-
additional = true
|
66
|
+
hsh[key.downcase] << [foreign_key.model, relationship_name]
|
76
67
|
end
|
77
68
|
end
|
78
69
|
end
|
70
|
+
|
71
|
+
def generate_has_many_association_name(relationship_name, model, has_many_model)
|
72
|
+
name =
|
73
|
+
if relationship_name == model.table_name.underscore
|
74
|
+
has_many_model.table_name
|
75
|
+
else
|
76
|
+
relationship_name + '_' + has_many_model.table_name
|
77
|
+
end
|
78
|
+
name.underscore.pluralize.to_sym
|
79
|
+
end
|
79
80
|
end
|
80
81
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic-active-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Youch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|