Skip to content

Commit

Permalink
:merge and :find_or_create also set/update created_at and updated_at …
Browse files Browse the repository at this point in the history
…when defined
  • Loading branch information
subvertallchris committed May 1, 2015
1 parent dab74e0 commit 8c9671b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/neo4j/active_node/persistence.rb
Expand Up @@ -99,13 +99,15 @@ def create!(*args)
def merge(attributes)
neo4j_session.query.merge(n: {self => attributes})
.on_create_set(n: on_create_props)
.on_match_set(n: on_match_props)
.pluck(:n).first
end

def find_or_create(find_attributes, set_attributes = {})
set_attributes_with_id = set_attributes.merge(on_create_props)
on_create_attributes = set_attributes.merge(on_create_props)
on_match_attributes = set_attributes.merge(on_match_props)
neo4j_session.query.merge(n: {self => find_attributes})
.on_create_set(n: set_attributes_with_id).on_match_set(n: set_attributes)
.on_create_set(n: on_create_attributes).on_match_set(n: on_match_attributes)
.pluck(:n).first
end

Expand All @@ -126,7 +128,20 @@ def load_entity(id)
private

def on_create_props
{id_property_name => default_properties[id_property_name].call}
{id_property_name => default_properties[id_property_name].call}.tap do |props|
now = DateTime.now.to_i
set_props_timestamp!('created_at', props, now)
set_props_timestamp!('updated_at', props, now)
end
end

def on_match_props
set_props_timestamp!('updated_at')
end

def set_props_timestamp!(key_name, props = {}, stamp = DateTime.now.to_i)
props[key_name.to_sym] = stamp if attributes_nil_hash.key?(key_name)
props
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/e2e/query_spec.rb
Expand Up @@ -53,6 +53,8 @@ def self.ordered_by_subject
stub_active_node_class('Teacher') do
property :name
property :age
property :created_at
property :updated_at

has_many :both, :lessons

Expand Down Expand Up @@ -185,6 +187,25 @@ def self.ordered_by_subject
Teacher.merge(name: 'Dr. Harold Samuels')
expect(Teacher.count).to eq(1)
end

it 'sets created_at and updated_at' do
teacher = Teacher.merge(name: 'Dr. Harold Samuels')
expect(teacher.created_at).not_to be_nil
expect(teacher.updated_at).not_to be_nil
expect(teacher.created_at).to eq teacher.updated_at
end

context 'on match' do
it 'updates updated_at but not created_at' do
teacher1 = Teacher.merge(name: 'Dr. Harold Samuels')
expect(teacher1.created_at).to eq teacher1.updated_at
expect(DateTime).to receive(:now).at_least(2).times.and_return 1234
teacher2 = Teacher.merge(name: 'Dr. Harold Samuels')
expect(teacher1.uuid).to eq teacher2.uuid
expect(teacher1.created_at).to eq teacher2.created_at
expect(teacher1.created_at).not_to eq teacher2.updated_at
end
end
end

describe '.find_or_create' do
Expand Down Expand Up @@ -218,6 +239,23 @@ def self.ordered_by_subject
expect(teacher1.neo_id).to eq teacher2.neo_id
expect(teacher1.id).to eq teacher2.id
end

it 'sets timestamps on create' do
teacher = Teacher.find_or_create(name: 'Dr. Harold Samuels')
expect(teacher.created_at).not_to be_nil
expect(teacher.updated_at).not_to be_nil
end

it 'changes updated_at on update but not created_at' do
teacher1 = Teacher.find_or_create(name: 'Dr. Harold Samuels')
expect(teacher1.created_at).to eq teacher1.updated_at
expect(DateTime).to receive(:now).at_least(2).times.and_return 1234
teacher2 = Teacher.find_or_create(name: 'Dr. Harold Samuels')
expect(teacher1.uuid).to eq teacher2.uuid
expect(teacher1.created_at).to eq teacher2.created_at
expect(teacher1.updated_at).not_to eq teacher2.updated_at
expect(teacher2.updated_at.to_i).to eq 1234
end
end
end

Expand Down

0 comments on commit 8c9671b

Please sign in to comment.