active_node 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_node/persistence.rb +10 -2
- data/lib/active_node/reflection.rb +50 -46
- data/lib/active_node/version.rb +1 -1
- data/spec/models/client.rb +1 -1
- metadata +1 -1
@@ -31,6 +31,10 @@ module ActiveNode
|
|
31
31
|
klass && klass < ActiveNode::Base && klass || default_klass
|
32
32
|
end
|
33
33
|
|
34
|
+
def filterClass(nodes, klass)
|
35
|
+
wrap(nodes.select{|node| klass.nil? || node.type == klass.name.underscore}, klass)
|
36
|
+
end
|
37
|
+
|
34
38
|
private
|
35
39
|
def new_instance node
|
36
40
|
node && new(node)
|
@@ -76,14 +80,18 @@ module ActiveNode
|
|
76
80
|
end
|
77
81
|
|
78
82
|
def incoming(types=nil, klass=nil)
|
79
|
-
|
83
|
+
related(:incoming, types, klass)
|
80
84
|
end
|
81
85
|
|
82
86
|
def outgoing(types=nil, klass=nil)
|
83
|
-
|
87
|
+
related(:outgoing, types, klass)
|
84
88
|
end
|
85
89
|
|
86
90
|
private
|
91
|
+
def related(direction, types, klass)
|
92
|
+
node && self.class.filterClass(node.send(direction, types), klass)
|
93
|
+
end
|
94
|
+
|
87
95
|
def destroy_associations include_associations
|
88
96
|
rels = node.rels
|
89
97
|
return false unless rels.empty? || include_associations
|
@@ -19,9 +19,9 @@ module ActiveNode
|
|
19
19
|
module ClassMethods
|
20
20
|
def create_reflection(macro, name, options, model)
|
21
21
|
case macro
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
when :has_many, :has_one
|
23
|
+
klass = options[:through] ? ThroughReflection : AssociationReflection
|
24
|
+
reflection = klass.new(macro, name, options, model)
|
25
25
|
end
|
26
26
|
|
27
27
|
self.reflections = self.reflections.merge(name => reflection)
|
@@ -91,9 +91,9 @@ module ActiveNode
|
|
91
91
|
|
92
92
|
|
93
93
|
def initialize(macro, name, options, model)
|
94
|
-
@macro
|
95
|
-
@name
|
96
|
-
@options
|
94
|
+
@macro = macro
|
95
|
+
@name = name
|
96
|
+
@options = options
|
97
97
|
@model = model
|
98
98
|
end
|
99
99
|
|
@@ -118,44 +118,48 @@ module ActiveNode
|
|
118
118
|
end
|
119
119
|
|
120
120
|
def type
|
121
|
-
@type ||= (options[:type] ||
|
121
|
+
@type ||= (options[:type] || derive_type)
|
122
122
|
end
|
123
123
|
|
124
124
|
# Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +model+ attribute,
|
125
125
|
# and +other_aggregation+ has an options hash assigned to it.
|
126
126
|
def ==(other_aggregation)
|
127
127
|
super ||
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
128
|
+
other_aggregation.kind_of?(self.class) &&
|
129
|
+
name == other_aggregation.name &&
|
130
|
+
other_aggregation.options &&
|
131
|
+
model == other_aggregation.model
|
132
132
|
end
|
133
133
|
|
134
134
|
private
|
135
|
-
|
136
|
-
|
137
|
-
|
135
|
+
def derive_class_name
|
136
|
+
name.to_s.camelize
|
137
|
+
end
|
138
|
+
|
139
|
+
def derive_type
|
140
|
+
direction == :outgoing ? name.to_s.singularize : @model.name.underscore
|
141
|
+
end
|
138
142
|
end
|
139
143
|
|
140
144
|
|
141
145
|
# Holds all the meta-data about an association as it was specified in the
|
142
146
|
# Active Record class.
|
143
147
|
class AssociationReflection < MacroReflection #:nodoc:
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
148
|
+
# Returns the target association's class.
|
149
|
+
#
|
150
|
+
# class Author < ActiveRecord::Base
|
151
|
+
# has_many :books
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# Author.reflect_on_association(:books).klass
|
155
|
+
# # => Book
|
156
|
+
#
|
157
|
+
# <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
|
158
|
+
# a new association object. Use +build_association+ or +create_association+
|
159
|
+
# instead. This allows plugins to hook into association object creation.
|
160
|
+
#def klass
|
161
|
+
# @klass ||= model.send(:compute_type, class_name)
|
162
|
+
#end
|
159
163
|
|
160
164
|
def initialize(*args)
|
161
165
|
super
|
@@ -204,27 +208,27 @@ module ActiveNode
|
|
204
208
|
|
205
209
|
def association_class
|
206
210
|
case macro
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
211
|
+
when :has_many
|
212
|
+
if options[:through]
|
213
|
+
Associations::HasManyThroughAssociation
|
214
|
+
else
|
215
|
+
Associations::HasManyAssociation
|
216
|
+
end
|
217
|
+
when :has_one
|
218
|
+
if options[:through]
|
219
|
+
Associations::HasOneThroughAssociation
|
220
|
+
else
|
221
|
+
Associations::HasOneAssociation
|
222
|
+
end
|
219
223
|
end
|
220
224
|
end
|
221
225
|
|
222
226
|
private
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
227
|
+
def derive_class_name
|
228
|
+
class_name = name.to_s.camelize
|
229
|
+
class_name = class_name.singularize if collection?
|
230
|
+
class_name
|
231
|
+
end
|
228
232
|
end
|
229
233
|
|
230
234
|
# Holds all the meta-data about a :through association as it was specified
|
data/lib/active_node/version.rb
CHANGED
data/spec/models/client.rb
CHANGED