Skip to content

Commit

Permalink
Merge pull request #13 from Shopify/prepare-ar-new-records
Browse files Browse the repository at this point in the history
ActiveRecordCoder: handle `new_record` flag
  • Loading branch information
casperisfine authored Aug 1, 2022
2 parents 81e4071 + dd1ee51 commit c81cdde
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Unreleased

* Make ActiveRecordCoder handle to read payloads that will be generated by Paquito 0.7.0 (#12).
DO NOT upgrade to Paquito 0.7.0+ without first fully rolling out Paquito 0.6.2.

# 0.6.1

* No data

# 0.6.0

* No data

# 0.5.0

* No data

# 0.4.0

* No data

# 0.3.1

* No data

# 0.3.0

* No data

# 0.2.1

* No data

# 0.2.0

* No data

# 0.1.0

* No data
10 changes: 8 additions & 2 deletions lib/paquito/active_record_coder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Paquito
class ActiveRecordCoder
EMPTY_HASH = {}.freeze

class << self
def dump(record)
instances = InstanceTracker.new
Expand Down Expand Up @@ -94,13 +96,17 @@ def attributes_for_database(record)
attributes
end

def deserialize_record(class_name, attributes_from_database)
def deserialize_record(class_name, attributes_from_database, new_record = false)
begin
klass = Object.const_get(class_name)
rescue NameError
raise ClassMissingError, "undefined class: #{class_name}"
end
klass.instantiate(attributes_from_database)

# Ideally we'd like to call `klass.instantiate`, however it doesn't allow to pass
# wether the record was persisted or not.
attributes = klass.attributes_builder.build_from_database(attributes_from_database, EMPTY_HASH)
klass.allocate.init_with_attributes(attributes, new_record)
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/activerecord/active_record_coder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def setup
assert_equal shop.products.first, recovered_value.products.first
end

test "encodes wether the record was persisted or not" do
shop = Shop.find_by!(name: "Snow Devil")
assert_predicate shop, :persisted?
recovered_shop = @codec.load(@codec.dump(shop))
assert_predicate recovered_shop, :persisted?

new_shop = @codec.load([0, ["Shop", {}, true]])
refute_predicate new_shop, :persisted?

skip("Will pass on Paquito 0.7.0")
recovered_shop = @codec.load(@codec.dump(new_shop))
refute_predicate recovered_shop, :persisted?
end

test "format is stable across payload commits" do
shop = Shop.preload(:products).find_by!(name: "Snow Devil")

Expand Down

0 comments on commit c81cdde

Please sign in to comment.