passive_record 0.3.10 → 0.3.11

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
  SHA1:
3
- metadata.gz: 7a864962f49adde921cd337981fea7046b072a91
4
- data.tar.gz: f6dfa54709c92ab2628fbe07a2831f71f714b930
3
+ metadata.gz: 1a62578ddc6b83eea82b4cf49d13debdaa28a981
4
+ data.tar.gz: bb333fee35ac2726caa54e119ac9dd8ee30c1804
5
5
  SHA512:
6
- metadata.gz: 9aba9642fa965b2b5460e5eec9521d426fe3041dfb53c5e234000eebb36d41caba198f88f011a399900dfdea4c6b68f721aa3421873d3a7edc26a3f54ee3ac04
7
- data.tar.gz: 19fb03f821db6c570d24a546960d3b729c0b4e78f29189bee1c9ae820d87d227b74fb765b1eade54c6db602d903653d7683387f741c066253a6c6fb19b6b657e
6
+ metadata.gz: 454ae9685e3ab7a3ced60986178e18c2d30bdc413f5d8ea4ccf7d320e5d731ddf844072186dfdd1335251c0ebcb4eed575c3028ba58b684dfb437951d5b8a86c
7
+ data.tar.gz: 24509793436c201f1f4be7e73d3b85808b360aae77847c4f1f73b211aa3ce8b4b8319cd2abf142a0ecfb0e31b97fa2389d39ca58afcad35708f44f6beed7e226
@@ -25,29 +25,123 @@ module PassiveRecord
25
25
  target_class_name = opts.delete(:class_name) { (parent_name_sym.to_s).split('_').map(&:capitalize).join }
26
26
  association = BelongsToAssociation.new(self, target_class_name, parent_name_sym)
27
27
  associate!(association)
28
+
29
+ define_method(:"#{parent_name_sym}_id") do
30
+ send(parent_name_sym)&.id
31
+ end
32
+
33
+ define_method(parent_name_sym) do
34
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
35
+ association.parent_class.find(relation.parent_model_id)
36
+ end
37
+
38
+ define_method(:"#{parent_name_sym}=") do |new_parent|
39
+ send(:"#{parent_name_sym}_id=", new_parent.id)
40
+ end
41
+
42
+ define_method(:"#{parent_name_sym}_id=") do |new_parent_id|
43
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
44
+ relation.parent_model_id = new_parent_id
45
+ end
28
46
  end
29
47
 
30
48
  def has_one(child_name_sym)
31
49
  child_class_name = (child_name_sym.to_s).split('_').map(&:capitalize).join
32
50
  association = HasOneAssociation.new(self, child_class_name, child_name_sym)
33
51
  associate!(association)
52
+
53
+ define_method(:"#{child_name_sym}_id") do
54
+ send(child_name_sym)&.id
55
+ end
56
+
57
+ define_method(child_name_sym) do
58
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
59
+ relation.lookup
60
+ end
61
+
62
+ define_method(:"create_#{child_name_sym}") do |attrs={}|
63
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
64
+ relation.create(attrs)
65
+ end
66
+
67
+ define_method(:"#{child_name_sym}=") do |new_child|
68
+ send(:"#{child_name_sym}_id=", new_child.id)
69
+ end
70
+
71
+ define_method(:"#{child_name_sym}_id=") do |new_child_id|
72
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
73
+ # detach existing child...
74
+ relation.lookup.send(:"#{relation.parent_model_id_field}=", nil)
75
+
76
+ relation.child_class.
77
+ find(new_child_id).
78
+ update(relation.parent_model_id_field => relation.id)
79
+ end
34
80
  end
35
81
 
36
82
  def has_many(collection_name_sym, opts={})
37
83
  target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join }
38
84
 
85
+ association = nil
39
86
  if opts.key?(:through)
40
87
  through_class_collection_name = opts.delete(:through)
41
88
 
42
89
  through_class_name = (through_class_collection_name.to_s).split('_').map(&:capitalize).join
43
90
  base_association = associations.detect { |assn| assn.child_class_name == through_class_name }
44
91
 
45
- association = HasManyThroughAssociation.new(self, target_class_name, collection_name_sym, through_class_collection_name, base_association)
92
+ association = HasManyThroughAssociation.new(
93
+ self, target_class_name, collection_name_sym, through_class_collection_name, base_association)
46
94
 
47
95
  associate!(association)
96
+
97
+ define_method(:"#{collection_name_sym}=") do |new_collection|
98
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
99
+
100
+ intermediary = relation.intermediary_relation
101
+
102
+ # drop all intermediary relations
103
+ intermediary.where( relation.parent_model_id_field => relation.id ).each do |intermediate|
104
+ intermediate.destroy
105
+ end
106
+
107
+ # add in new ones...
108
+ target_name = relation.association.target_name_symbol.to_s
109
+ new_collection.each do |child|
110
+ intermediary.create(target_name.singularize + "_id" => child.id, relation.parent_model_id_field => relation.id )
111
+ end
112
+ end
48
113
  else
49
114
  association = HasManyAssociation.new(self, target_class_name, collection_name_sym)
50
115
  associate!(association)
116
+
117
+
118
+ define_method(:"#{collection_name_sym}=") do |new_collection|
119
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
120
+
121
+ # detach existing children...
122
+ relation.all.each do |child|
123
+ child.send(:"#{relation.parent_model_id_field}=", nil)
124
+ end
125
+
126
+ # reattach new children
127
+ new_collection.each do |child|
128
+ child.send(:"#{relation.parent_model_id_field}=", relation.id)
129
+ end
130
+ end
131
+ end
132
+
133
+ define_method(collection_name_sym) do
134
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
135
+ relation
136
+ end
137
+
138
+ define_method(:"#{collection_name_sym.to_s.singularize}_ids") do
139
+ send(collection_name_sym).map(&:id)
140
+ end
141
+
142
+ define_method(:"create_#{collection_name_sym.to_s.singularize}") do |attrs={}|
143
+ relation = instance_eval{relata}.detect { |rel| rel.association == association }
144
+ relation.create(attrs)
51
145
  end
52
146
  end
53
147
 
@@ -31,22 +31,6 @@ module PassiveRecord
31
31
  ]
32
32
  end
33
33
 
34
- def respond_to?(meth,*args,&blk)
35
- if find_relation_by_target_name_symbol(meth)
36
- true
37
- else
38
- super(meth,*args,&blk)
39
- end
40
- end
41
-
42
- def method_missing(meth, *args, &blk)
43
- if (matching_relation = find_relation_by_target_name_symbol(meth))
44
- send_relation(matching_relation, meth, *args)
45
- else
46
- super(meth,*args,&blk)
47
- end
48
- end
49
-
50
34
  protected
51
35
  def attribute_names
52
36
  attr_names = instance_variables
@@ -67,67 +51,5 @@ module PassiveRecord
67
51
  assn.to_relation(self)
68
52
  end || []
69
53
  end
70
-
71
- def find_relation_by_target_name_symbol(meth)
72
- relata.detect do |relation|
73
- possible_target_names(relation).include?(meth.to_s)
74
- end
75
- end
76
-
77
- def possible_target_names(relation)
78
- target_name = relation.association.target_name_symbol.to_s
79
- [
80
- target_name,
81
- "#{target_name}=",
82
- "#{target_name}_id",
83
- "#{target_name}_ids",
84
- "#{target_name.singularize}_ids",
85
- "#{target_name}_id=",
86
- "create_#{target_name}",
87
- "create_#{target_name.singularize}"
88
- ]
89
- end
90
-
91
- def send_relation(matching_relation, meth, *args)
92
- target_name = matching_relation.association.target_name_symbol.to_s
93
-
94
- case meth.to_s
95
- when target_name
96
- if matching_relation.singular?
97
- matching_relation.lookup
98
- else
99
- matching_relation
100
- end
101
- when "#{target_name}="
102
- if args.first.is_a?(Array)
103
- if matching_relation.is_a?(Associations::HasManyThroughRelation)
104
- intermediary = matching_relation.intermediary_relation
105
- args.first.each do |child|
106
- intermediary.
107
- where((target_name.singularize + "_id").to_sym => child.id).
108
- first_or_create
109
- end
110
- else
111
- args.first.each do |child|
112
- child.send(matching_relation.parent_model_id_field + "=", id)
113
- end
114
- end
115
- else
116
- matching_relation.parent_model_id = args.first.id
117
- end
118
- when "create_#{target_name}", "create_#{target_name.singularize}"
119
- matching_relation.create(*args)
120
- when "#{target_name}_id"
121
- if matching_relation.is_a?(Associations::HasOneRelation)
122
- matching_relation.lookup&.id
123
- elsif matching_relation.is_a?(Associations::BelongsToRelation)
124
- matching_relation.parent_model_id
125
- end
126
- when "#{target_name}_id="
127
- matching_relation.parent_model_id = args.first
128
- when "#{target_name}_ids", "#{target_name.singularize}_ids"
129
- matching_relation.parent_model.send(target_name).map(&:id)
130
- end
131
- end
132
54
  end
133
55
  end
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.10"
3
+ VERSION = "0.3.11"
4
4
  end
@@ -273,6 +273,9 @@ describe "passive record models" do
273
273
  toy = Toy.create
274
274
  toy.child = child
275
275
  expect(child.toy).to eq(toy)
276
+
277
+ child.toy = Toy.create
278
+ expect(child.toy).not_to eq(toy)
276
279
  end
277
280
  end
278
281
 
@@ -309,7 +312,7 @@ describe "passive record models" do
309
312
 
310
313
  expect(child.id).not_to eq(another_child.id)
311
314
  expect(parent.children.all).to eq([child, another_child])
312
- expect(parent.children_ids).to eq([child.id, another_child.id])
315
+ expect(parent.child_ids).to eq([child.id, another_child.id])
313
316
  end
314
317
  end
315
318
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman