Skip to content

Commit f37a768

Browse files
authored
Merge pull request #12 from Couchbase-Ecosystem/fix_save_return_for_nested
fix: Remove ActiveRecord::Dirty to preserve nested document changes on save
2 parents 2e15451 + cf142bd commit f37a768

6 files changed

Lines changed: 19 additions & 9 deletions

File tree

lib/couchbase-orm/base.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ module CouchbaseOrm
3333
class Document
3434
include Inspectable
3535
include ::ActiveModel::Model
36-
include ::ActiveModel::Dirty
37-
include Changeable # override some methods from ActiveModel::Dirty (keep it included after)
36+
include Changeable
3837
include ::ActiveModel::Attributes
3938
include ::ActiveModel::Serializers::JSON
4039

lib/couchbase-orm/changeable.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def move_changes
8080

8181
def changes_applied
8282
move_changes
83-
super
8483
end
8584

8685
def reset_object!

lib/couchbase-orm/types/timestamp.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module Types
33
class Timestamp < ActiveModel::Type::DateTime
44
def cast(value)
55
return nil if value.nil?
6-
return Time.at(value) if value.is_a?(Integer) || value.is_a?(Float)
7-
return Time.at(value.to_i) if value.is_a?(String) && value =~ /^[0-9]+$/
8-
return value.utc if value.is_a?(Time)
9-
super(value).utc
6+
return Time.at(value).floor if value.is_a?(Integer) || value.is_a?(Float)
7+
return Time.at(value.to_i).floor if value.is_a?(String) && value =~ /^[0-9]+$/
8+
return value.utc.floor if value.is_a?(Time)
9+
super(value).utc.floor
1010
end
1111

1212
def serialize(value)

lib/couchbase-orm/utilities/query_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def build_not_match(key, value)
117117
end
118118

119119
def serialize_value(key, value_before_type_cast)
120-
value =
120+
value =
121121
if value_before_type_cast.is_a?(Array)
122122
value_before_type_cast.map do |v|
123123
attribute_types[key.to_s].serialize(attribute_types[key.to_s].cast(v))

spec/type_nested_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class TypeNestedTest < CouchbaseOrm::Base
4242
obj.others[1].child = SubTypeTest.new(name: "baz")
4343
obj.save!
4444

45+
expect(obj.others[0].name).to eq "foo"
46+
expect(obj.others[0].tags).to eq ["foo", "bar"]
47+
expect(obj.others[1].name).to eq "bar"
48+
expect(obj.others[1].tags).to eq ["bar", "baz"]
49+
expect(obj.others[1].child.name).to eq "baz"
50+
4551
obj = TypeNestedTest.find(obj.id)
4652
expect(obj.others[0].name).to eq "foo"
4753
expect(obj.others[0].tags).to eq ["foo", "bar"]
@@ -116,7 +122,8 @@ class TypeNestedTest < CouchbaseOrm::Base
116122
obj.others[1].name = "baz"
117123
obj.flags[0] = true
118124

119-
obj.save!
125+
expect { obj.save! }.to_not change { [obj.main.name, obj.others[0].name, obj.others[1].name, obj.flags] }
126+
120127
obj = TypeNestedTest.find(obj.id)
121128
expect(obj.main.name).to eq "bar"
122129
expect(obj.others[0].name).to eq "bar"

spec/type_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
require "couchbase-orm/types"
55

66
class DateTimeWith3Decimal < CouchbaseOrm::Types::DateTime
7+
def cast(value)
8+
result = super(value)
9+
result&.floor(3)
10+
end
11+
712
def serialize(value)
813
value&.iso8601(3)
914
end

0 commit comments

Comments
 (0)