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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73b173018e9af4d94db62bf921d964015577605528495c893e7d905b735d11ab
4
- data.tar.gz: e83792d3d00058247a4f7e7ed9c8676f0166e2cb6ebc8675cbf4c8badd58be76
3
+ metadata.gz: 6228c3f790ef52e03bb5303386c5c706c0a3f35ae79697c790dcd8ab85a6512d
4
+ data.tar.gz: b1c03bf64c5593bd7348709a404ab131dff69ba854ed1ecc26b1743395145aca
5
5
  SHA512:
6
- metadata.gz: 5d1058ca2a6cad412c32367a695641b57e6be67979cd337c2d83e91a994cf5b4048dc5ca3f709eac7f39d1e63632e175a4fb1348467cb885cec90badc8452f48
7
- data.tar.gz: 3180c4951dfbd32e5cc1dd398306b7dec0844b4e82d47495b3b97776ebac3794636bb64cc9c87db5994d9e157e5b4c4fa785e99636e1548773a624d7687904b2
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), model.table_name.underscore)
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, additional|
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, additional)
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, additional)
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.underscore.pluralize.to_sym,
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, additional]
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.3
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-14 00:00:00.000000000 Z
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord