active_node 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,16 @@ module ActiveNode::Associations::Builder
16
16
  CODE
17
17
  end
18
18
 
19
+ def define_readers
20
+ super
21
+
22
+ mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
23
+ def #{name}_id
24
+ association(:#{name}).id_reader
25
+ end
26
+ CODE
27
+ end
28
+
19
29
  def define_writers
20
30
  super
21
31
 
@@ -10,7 +10,11 @@ module ActiveNode
10
10
  end
11
11
 
12
12
  def ids_reader
13
- [target.try(:id)].compact
13
+ [id_reader].compact
14
+ end
15
+
16
+ def id_reader
17
+ target.try :id
14
18
  end
15
19
 
16
20
  def id_writer(id)
@@ -7,6 +7,11 @@ module ActiveNode
7
7
  end
8
8
 
9
9
  module ClassMethods
10
+ def timestamps
11
+ attribute :created_at, type: String
12
+ attribute :updated_at, type: String
13
+ end
14
+
10
15
  def find ids
11
16
  ids.is_a?(Enumerable) ? ids.map { |id| find(id) } : new_instance(Neography::Node.load(ids))
12
17
  end
@@ -32,27 +37,25 @@ module ActiveNode
32
37
  end
33
38
 
34
39
  def filterClass(nodes, klass)
35
- wrap(nodes.select{|node| klass.nil? || node.type == klass.name.underscore}, klass)
40
+ wrap(nodes.select { |node| klass.nil? || node.type == klass.name.underscore }, klass)
36
41
  end
37
42
 
38
43
  private
39
44
  def new_instance node
40
- node && new(node)
45
+ new(node) if node.try(:type) == type
41
46
  end
42
47
  end
43
48
 
44
49
  attr_reader :node
45
- #protected :node
46
-
47
50
  delegate :neo_id, to: :node, allow_nil: true
51
+
48
52
  def id
49
53
  neo_id && neo_id.to_i
50
54
  end
55
+
51
56
  alias :to_param :id
52
57
  alias :persisted? :id
53
58
 
54
- alias :[] :send
55
-
56
59
  def initialize object={}
57
60
  hash=object
58
61
  @node, hash = object, object.send(:table) if object.is_a? Neography::Node
@@ -100,7 +103,7 @@ module ActiveNode
100
103
  end
101
104
 
102
105
  def nullify_blanks! attrs
103
- attrs.each { |k, v| attrs[k]=nil if attrs[k].blank? }
106
+ attrs.each { |k, v| attrs[k]=nil if v.blank? }
104
107
  end
105
108
 
106
109
  def create_or_update
@@ -109,10 +112,13 @@ module ActiveNode
109
112
  end
110
113
 
111
114
  def write
115
+ now = Time.now.utc.iso8601(3)
116
+ try :updated_at=, now
112
117
  if @node
113
- nullify_blanks!(attributes).each { |k, v| @node[k]=v }
118
+ nullify_blanks!(self.attributes).each { |k, v| @node[k]=v }
114
119
  else
115
- @node = Neography::Node.create nullify_blanks!(attributes).merge(type: self.class.type)
120
+ try :created_at=, now
121
+ @node = Neography::Node.create nullify_blanks!(self.attributes).merge!(type: self.class.type)
116
122
  end
117
123
  end
118
124
  end
@@ -133,7 +133,7 @@ module ActiveNode
133
133
 
134
134
  private
135
135
  def derive_class_name
136
- name.to_s.camelize
136
+ (direction == :outgoing ? type : name).to_s.camelize
137
137
  end
138
138
 
139
139
  def derive_type
@@ -1,3 +1,3 @@
1
1
  module ActiveNode
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -100,6 +100,14 @@ describe ActiveNode::Associations do
100
100
  father.children.should == [child]
101
101
  end
102
102
 
103
+ it 'can read has_one relation by id' do
104
+ father = Person.create!
105
+ child = Person.create!
106
+ child.father = father
107
+ child.save
108
+ child.father_id.should == father.id
109
+ end
110
+
103
111
  it "can access new association without being saved" do
104
112
  father = Person.create!
105
113
  child = Person.new
@@ -25,6 +25,23 @@ describe ActiveNode::Persistence do
25
25
  person.destroy!.should be_true
26
26
  Person.all.count.should == 2
27
27
  end
28
+
29
+ it 'should record timestamp' do
30
+ now = Time.now
31
+ person = Person.create!
32
+ person.created_at.should_not be_nil
33
+ person = Person.find(person.id)
34
+ person.created_at.should_not be_nil
35
+ person.updated_at.should_not be_nil
36
+ allow(Time).to receive(:now) {now + 1.second}
37
+ person.name = 'abc'
38
+ person.save
39
+ (person.created_at < person.updated_at).should be_true
40
+ end
41
+
42
+ it 'should not record timestamp if not specified' do
43
+ Client.create!(name: 'abc').respond_to?(:created_at).should be_false
44
+ end
28
45
  end
29
46
 
30
47
  describe "#create!" do
@@ -36,6 +53,10 @@ describe ActiveNode::Persistence do
36
53
  person = Person.create!(multi: [1, 2, 3])
37
54
  Person.find(person.id).multi.should == [1, 2, 3]
38
55
  end
56
+
57
+ it 'should not find an object with id of a different model' do
58
+ Client.find(Person.create!.id).should be_nil
59
+ end
39
60
  end
40
61
 
41
62
  describe "#attributes" do
@@ -1,7 +1,10 @@
1
1
  class Person < ActiveNode::Base
2
2
  attribute :name, type: String
3
3
  attribute :multi
4
+ timestamps
5
+
4
6
  has_many :people
5
7
  has_many :children, class_name: "Person"
8
+ #has_many :sons, class_name: "Person"
6
9
  has_one :father, type: :child, direction: :incoming, class_name: "Person"
7
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_node
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-24 00:00:00.000000000 Z
12
+ date: 2013-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_attr