Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #reload and mark model as persisted after reloading #567

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
* [#567](https://github.com/Dynamoid/dynamoid/pull/567) Fix `#reload` and mark reloaded model as persisted
### Added
* [#536](https://github.com/Dynamoid/dynamoid/pull/536) Modernization
* Support for Ruby 3.1
Expand Down
3 changes: 3 additions & 0 deletions lib/dynamoid/loadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def reload
end

self.attributes = self.class.find(hash_key, **options).attributes

@associations.values.each(&:reset)
@new_record = false

self
end
end
Expand Down
35 changes: 0 additions & 35 deletions spec/dynamoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,6 @@ def city=(value)
expect(address.errors.full_messages).to be_empty
end

context '.reload' do
let(:address) { Address.create }
let(:message) { Message.create(text: 'Nice, supporting datetime range!', time: Time.now.to_datetime) }
let(:tweet) { tweet = Tweet.create(tweet_id: 'x', group: 'abc') }

it 'reflects persisted changes' do
address.update_attributes(city: 'Chicago')
expect(address.reload.city).to eq 'Chicago'
end

it 'uses a :consistent_read' do
expect(Tweet).to receive(:find).with(tweet.hash_key, range_key: tweet.range_value, consistent_read: true).and_return(tweet)
tweet.reload
end

it 'works with range key' do
expect(tweet.reload.group).to eq 'abc'
end

it 'uses dumped value of sort key to load document' do
klass = new_class do
range :activated_on, :date
field :name
end

obj = klass.create!(activated_on: Date.today, name: 'Old value')
obj2 = klass.where(id: obj.id, activated_on: obj.activated_on).first
obj2.update_attributes(name: 'New value')

expect { obj.reload }.to change {
obj.name
}.from('Old value').to('New value')
end
end

it 'has default table options' do
address = Address.create

Expand Down
71 changes: 71 additions & 0 deletions spec/dynamoid/loadable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require 'spec_helper'

describe Dynamoid::Loadable do

context '.reload' do
let(:address) { Address.create }
let(:message) { Message.create(text: 'Nice, supporting datetime range!', time: Time.now.to_datetime) }
let(:tweet) { tweet = Tweet.create(tweet_id: 'x', group: 'abc') }

it 'reflects persisted changes' do
klass = new_class do
field :city
end

address = klass.create(city: 'Miami')
copy = klass.find(address.id)

address.update_attributes(city: 'Chicago')
expect(copy.reload.city).to eq 'Chicago'
end

it 'reads with strong consistency' do
klass = new_class do
field :message
end

tweet = klass.create

expect(klass).to receive(:find).with(tweet.id, consistent_read: true).and_return(tweet)
tweet.reload
end

it 'works with range key' do
klass = new_class do
field :message
range :group
end

tweet = klass.create(group: 'tech')
expect(tweet.reload.group).to eq 'tech'
end

it 'uses dumped value of sort key to load document' do
klass = new_class do
range :activated_on, :date
field :name
end

obj = klass.create!(activated_on: Date.today, name: 'Old value')
obj2 = klass.where(id: obj.id, activated_on: obj.activated_on).first
obj2.update_attributes(name: 'New value')

expect { obj.reload }.to change { obj.name }.from('Old value').to('New value')
end

# https://github.com/Dynamoid/dynamoid/issues/564
it 'marks model as persisted if not saved model is already persisted and successfuly reloaded' do
klass = new_class do
field :message
end

object = klass.create(message: 'a')
copy = klass.new(id: object.id)

expect { copy.reload }.to change { copy.new_record? }.from(true).to(false)
expect(copy.message).to eq 'a'
end
end
end