Skip to content

Commit

Permalink
Merge pull request #347 from samvera-labs/create_in_memory_with_id
Browse files Browse the repository at this point in the history
Creating resources with id sets created_at and new_record
  • Loading branch information
Trey Pendragon authored Jan 18, 2018
2 parents d0ec233 + a42708a commit a0f4ffe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/valkyrie/persistence/memory/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def initialize(adapter)
# persistence backend.
def save(resource:)
resource = generate_id(resource) if resource.id.blank?
resource.created_at ||= Time.current
resource.updated_at = Time.current
resource.new_record = false
normalize_dates!(resource)
cache[resource.id] = resource
end
Expand All @@ -41,9 +43,7 @@ def wipe!
private

def generate_id(resource)
resource.new(id: SecureRandom.uuid,
created_at: Time.current,
new_record: false)
resource.new(id: SecureRandom.uuid)
end

def normalize_dates!(resource)
Expand Down
5 changes: 5 additions & 0 deletions lib/valkyrie/specs/shared_specs/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class CustomResource < Valkyrie::Resource
book = persister.save(resource: resource_class.new(id: id))
reloaded = query_service.find_by(id: book.id)
expect(reloaded.id).to eq Valkyrie::ID.new(id)
expect(reloaded).to be_persisted
expect(reloaded.created_at).not_to be_blank
expect(reloaded.updated_at).not_to be_blank
expect(reloaded.created_at).not_to be_kind_of Array
expect(reloaded.updated_at).not_to be_kind_of Array
end

it "can store ::RDF::URIs" do
Expand Down

0 comments on commit a0f4ffe

Please sign in to comment.