diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 9a3bdb7..e7ce801 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -33,8 +33,7 @@ module CouchbaseOrm class Document include Inspectable include ::ActiveModel::Model - include ::ActiveModel::Dirty - include Changeable # override some methods from ActiveModel::Dirty (keep it included after) + include Changeable include ::ActiveModel::Attributes include ::ActiveModel::Serializers::JSON diff --git a/lib/couchbase-orm/changeable.rb b/lib/couchbase-orm/changeable.rb index ba852eb..712acf6 100644 --- a/lib/couchbase-orm/changeable.rb +++ b/lib/couchbase-orm/changeable.rb @@ -80,7 +80,6 @@ def move_changes def changes_applied move_changes - super end def reset_object! diff --git a/lib/couchbase-orm/types/timestamp.rb b/lib/couchbase-orm/types/timestamp.rb index d3643e8..9552de7 100644 --- a/lib/couchbase-orm/types/timestamp.rb +++ b/lib/couchbase-orm/types/timestamp.rb @@ -3,10 +3,10 @@ module Types class Timestamp < ActiveModel::Type::DateTime def cast(value) return nil if value.nil? - return Time.at(value) if value.is_a?(Integer) || value.is_a?(Float) - return Time.at(value.to_i) if value.is_a?(String) && value =~ /^[0-9]+$/ - return value.utc if value.is_a?(Time) - super(value).utc + return Time.at(value).floor if value.is_a?(Integer) || value.is_a?(Float) + return Time.at(value.to_i).floor if value.is_a?(String) && value =~ /^[0-9]+$/ + return value.utc.floor if value.is_a?(Time) + super(value).utc.floor end def serialize(value) diff --git a/lib/couchbase-orm/utilities/query_helper.rb b/lib/couchbase-orm/utilities/query_helper.rb index 3f1a71c..34b64c8 100644 --- a/lib/couchbase-orm/utilities/query_helper.rb +++ b/lib/couchbase-orm/utilities/query_helper.rb @@ -117,7 +117,7 @@ def build_not_match(key, value) end def serialize_value(key, value_before_type_cast) - value = + value = if value_before_type_cast.is_a?(Array) value_before_type_cast.map do |v| attribute_types[key.to_s].serialize(attribute_types[key.to_s].cast(v)) diff --git a/spec/type_nested_spec.rb b/spec/type_nested_spec.rb index 958fd56..6020e56 100644 --- a/spec/type_nested_spec.rb +++ b/spec/type_nested_spec.rb @@ -42,6 +42,12 @@ class TypeNestedTest < CouchbaseOrm::Base obj.others[1].child = SubTypeTest.new(name: "baz") obj.save! + expect(obj.others[0].name).to eq "foo" + expect(obj.others[0].tags).to eq ["foo", "bar"] + expect(obj.others[1].name).to eq "bar" + expect(obj.others[1].tags).to eq ["bar", "baz"] + expect(obj.others[1].child.name).to eq "baz" + obj = TypeNestedTest.find(obj.id) expect(obj.others[0].name).to eq "foo" expect(obj.others[0].tags).to eq ["foo", "bar"] @@ -116,7 +122,8 @@ class TypeNestedTest < CouchbaseOrm::Base obj.others[1].name = "baz" obj.flags[0] = true - obj.save! + expect { obj.save! }.to_not change { [obj.main.name, obj.others[0].name, obj.others[1].name, obj.flags] } + obj = TypeNestedTest.find(obj.id) expect(obj.main.name).to eq "bar" expect(obj.others[0].name).to eq "bar" diff --git a/spec/type_spec.rb b/spec/type_spec.rb index 6cf2290..e74cba2 100644 --- a/spec/type_spec.rb +++ b/spec/type_spec.rb @@ -4,6 +4,11 @@ require "couchbase-orm/types" class DateTimeWith3Decimal < CouchbaseOrm::Types::DateTime + def cast(value) + result = super(value) + result&.floor(3) + end + def serialize(value) value&.iso8601(3) end