active_node 0.0.2.alpha → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/README.md +4 -0
- data/active_node.gemspec +1 -1
- data/lib/active_node/associations.rb +8 -0
- data/lib/active_node/associations/association.rb +27 -0
- data/lib/active_node/associations/builder/association.rb +1 -1
- data/lib/active_node/associations/builder/has_many.rb +0 -4
- data/lib/active_node/associations/builder/has_one.rb +7 -0
- data/lib/active_node/associations/builder/singular_association.rb +19 -0
- data/lib/active_node/associations/collection_association.rb +2 -25
- data/lib/active_node/associations/has_one_association.rb +7 -0
- data/lib/active_node/associations/singular_association.rb +17 -0
- data/lib/active_node/version.rb +2 -2
- data/spec/functional/associations_spec.rb +13 -0
- data/spec/models/client.rb +0 -1
- data/spec/models/person.rb +1 -1
- metadata +26 -5
data/.travis.yml
CHANGED
data/README.md
ADDED
data/active_node.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.add_dependency "activemodel"
|
25
25
|
s.add_development_dependency "rspec", ">= 2.11"
|
26
26
|
s.add_development_dependency "net-http-spy", "0.2.1"
|
27
|
-
|
27
|
+
s.add_development_dependency "rake", ">= 0.8.7"
|
28
28
|
s.add_development_dependency "coveralls"
|
29
29
|
|
30
30
|
end
|
@@ -8,13 +8,17 @@ module ActiveNode
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
autoload :Association, 'active_node/associations/association'
|
11
|
+
autoload :SingularAssociation, 'active_node/associations/singular_association'
|
11
12
|
autoload :CollectionAssociation, 'active_node/associations/collection_association'
|
13
|
+
autoload :HasOneAssociation, 'active_node/associations/has_one_association'
|
12
14
|
autoload :HasManyAssociation, 'active_node/associations/has_many_association'
|
13
15
|
|
14
16
|
module Builder #:nodoc:
|
15
17
|
autoload :Association, 'active_node/associations/builder/association'
|
18
|
+
autoload :SingularAssociation, 'active_node/associations/builder/singular_association'
|
16
19
|
autoload :CollectionAssociation, 'active_node/associations/builder/collection_association'
|
17
20
|
|
21
|
+
autoload :HasOne, 'active_node/associations/builder/has_one'
|
18
22
|
autoload :HasMany, 'active_node/associations/builder/has_many'
|
19
23
|
end
|
20
24
|
|
@@ -55,6 +59,10 @@ module ActiveNode
|
|
55
59
|
def has_many(name, options = {})
|
56
60
|
Builder::HasMany.build(self, name, options)
|
57
61
|
end
|
62
|
+
|
63
|
+
def has_one(name, options = {})
|
64
|
+
Builder::HasOne.build(self, name, options)
|
65
|
+
end
|
58
66
|
end
|
59
67
|
end
|
60
68
|
end
|
@@ -39,6 +39,33 @@ module ActiveNode
|
|
39
39
|
def klass
|
40
40
|
reflection.klass
|
41
41
|
end
|
42
|
+
|
43
|
+
def load_target
|
44
|
+
owner.send(reflection.direction, reflection.type, reflection.klass)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Implements the reader method, e.g. foo.items for Foo.has_many :items
|
48
|
+
def reader(force_reload = false)
|
49
|
+
@target ||= load_target
|
50
|
+
end
|
51
|
+
|
52
|
+
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
|
53
|
+
def writer(records)
|
54
|
+
@dirty = true
|
55
|
+
@target = records
|
56
|
+
end
|
57
|
+
|
58
|
+
def save
|
59
|
+
return unless @dirty
|
60
|
+
#delete all relations missing in new target
|
61
|
+
owner.node.rels(reflection.type).send(reflection.direction).each do |rel|
|
62
|
+
rel.del unless ids_reader.include? rel.other_node(owner.node).neo_id.to_i
|
63
|
+
end
|
64
|
+
original_target = owner.node.send(reflection.direction, reflection.type)
|
65
|
+
original_target_ids = original_target.map(&:neo_id).map(&:to_i)
|
66
|
+
#add relations missing in old target
|
67
|
+
target_each { |n| original_target << n.node unless original_target_ids.include? n.id }
|
68
|
+
end
|
42
69
|
end
|
43
70
|
end
|
44
71
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveNode::Associations::Builder
|
2
|
+
class SingularAssociation < Association #:nodoc:
|
3
|
+
def define_constructors
|
4
|
+
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
5
|
+
def build_#{name}(*args, &block)
|
6
|
+
association(:#{name}).build(*args, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_#{name}(*args, &block)
|
10
|
+
association(:#{name}).create(*args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_#{name}!(*args, &block)
|
14
|
+
association(:#{name}).create!(*args, &block)
|
15
|
+
end
|
16
|
+
CODE
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -26,17 +26,6 @@ module ActiveNode
|
|
26
26
|
# +load_target+ and the +loaded+ flag are your friends.
|
27
27
|
class CollectionAssociation < Association #:nodoc:
|
28
28
|
|
29
|
-
# Implements the reader method, e.g. foo.items for Foo.has_many :items
|
30
|
-
def reader(force_reload = false)
|
31
|
-
@target ||= load_target
|
32
|
-
end
|
33
|
-
|
34
|
-
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
|
35
|
-
def writer(records)
|
36
|
-
@dirty = true
|
37
|
-
@target = records
|
38
|
-
end
|
39
|
-
|
40
29
|
# Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
|
41
30
|
def ids_reader
|
42
31
|
reader.map(&:id)
|
@@ -47,20 +36,8 @@ module ActiveNode
|
|
47
36
|
writer klass.find(ids.reject(&:blank?).map!(&:to_i))
|
48
37
|
end
|
49
38
|
|
50
|
-
def
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
def save
|
55
|
-
return unless @dirty
|
56
|
-
#delete all relations missing in new target
|
57
|
-
owner.node.rels(reflection.type).send(reflection.direction).each do |rel|
|
58
|
-
rel.del unless ids_reader.include? rel.other_node(owner.node).neo_id.to_i
|
59
|
-
end
|
60
|
-
original_target = owner.node.send(reflection.direction, reflection.type)
|
61
|
-
original_target_ids = original_target.map(&:neo_id).map(&:to_i)
|
62
|
-
#add relations missing in old target
|
63
|
-
@target.each { |n| original_target << n.node unless original_target_ids.include? n.id }
|
39
|
+
def target_each
|
40
|
+
target.each {|n| yield n}
|
64
41
|
end
|
65
42
|
|
66
43
|
def reset
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveNode
|
2
|
+
module Associations
|
3
|
+
class SingularAssociation < Association #:nodoc:
|
4
|
+
def load_target
|
5
|
+
super.first
|
6
|
+
end
|
7
|
+
|
8
|
+
def target_each
|
9
|
+
yield target
|
10
|
+
end
|
11
|
+
|
12
|
+
def ids_reader
|
13
|
+
[target.try(:id)].compact
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/active_node/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module ActiveNode
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
@@ -73,5 +73,18 @@ describe ActiveNode::Associations do
|
|
73
73
|
id = Person.create!(children: [Person.create!, Person.create!]).id
|
74
74
|
Person.find(id).children.size.should == 2
|
75
75
|
end
|
76
|
+
|
77
|
+
it 'can set has_one relation' do
|
78
|
+
father = Person.create!
|
79
|
+
child = Person.create!
|
80
|
+
child.father = father
|
81
|
+
child.save
|
82
|
+
father.children.should == [child]
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'can handle has_one reverse relationship' do
|
86
|
+
father = Person.create!(children: [Person.create!])
|
87
|
+
father.children.first.father.should == father
|
88
|
+
end
|
76
89
|
end
|
77
90
|
end
|
data/spec/models/client.rb
CHANGED
data/spec/models/person.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_node
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Heinrich Klobuczek
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active_attr
|
@@ -107,6 +107,22 @@ dependencies:
|
|
107
107
|
- - '='
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: 0.2.1
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rake
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.8.7
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.8.7
|
110
126
|
- !ruby/object:Gem::Dependency
|
111
127
|
name: coveralls
|
112
128
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,6 +150,7 @@ files:
|
|
134
150
|
- .travis.yml
|
135
151
|
- Gemfile
|
136
152
|
- LICENSE
|
153
|
+
- README.md
|
137
154
|
- Rakefile
|
138
155
|
- active_node.gemspec
|
139
156
|
- lib/active_node.rb
|
@@ -142,8 +159,12 @@ files:
|
|
142
159
|
- lib/active_node/associations/builder/association.rb
|
143
160
|
- lib/active_node/associations/builder/collection_association.rb
|
144
161
|
- lib/active_node/associations/builder/has_many.rb
|
162
|
+
- lib/active_node/associations/builder/has_one.rb
|
163
|
+
- lib/active_node/associations/builder/singular_association.rb
|
145
164
|
- lib/active_node/associations/collection_association.rb
|
146
165
|
- lib/active_node/associations/has_many_association.rb
|
166
|
+
- lib/active_node/associations/has_one_association.rb
|
167
|
+
- lib/active_node/associations/singular_association.rb
|
147
168
|
- lib/active_node/base.rb
|
148
169
|
- lib/active_node/callbacks.rb
|
149
170
|
- lib/active_node/core.rb
|
@@ -174,9 +195,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
196
|
none: false
|
176
197
|
requirements:
|
177
|
-
- - ! '
|
198
|
+
- - ! '>='
|
178
199
|
- !ruby/object:Gem::Version
|
179
|
-
version:
|
200
|
+
version: '0'
|
180
201
|
requirements: []
|
181
202
|
rubyforge_project: active_node
|
182
203
|
rubygems_version: 1.8.25
|