Skip to content

Remove ActiveRecord::Dirty usage #12

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

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/couchbase-orm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/couchbase-orm/changeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def move_changes

def changes_applied
move_changes
super
end

def reset_object!
Expand Down Expand Up @@ -367,8 +366,10 @@ def create_dirty_methods(name, meth)

def create_setters(name)
define_method("#{name}=") do |new_attribute_value|
type = self.class.attribute_types[name.to_s]
casted_value = type.cast new_attribute_value
previous_value = attributes[name.to_s]
ret = super(new_attribute_value)
ret = super(casted_value)
if previous_value != attributes[name.to_s]
changed_attributes.merge!(Hash[name, [previous_value, attributes[name.to_s]]])
end
Expand Down
19 changes: 13 additions & 6 deletions lib/couchbase-orm/types/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ module CouchbaseOrm
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 nil if value.nil?

value = if value.is_a?(Integer) || value.is_a?(Float)
Time.at(value)
elsif value.is_a?(String) && value =~ /^[0-9]+$/
Time.at(value.to_i)
elsif value.is_a?(Time)
value.utc
else
value
end
super(value)
end

def serialize(value)
value&.to_i
end
Expand Down
2 changes: 1 addition & 1 deletion lib/couchbase-orm/utilities/query_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 8 additions & 1 deletion spec/type_nested_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions spec/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
require "couchbase-orm/types"

class DateTimeWith3Decimal < CouchbaseOrm::Types::DateTime
def serialize(value)
value&.iso8601(3)
end
def initialize
super
@precision=3
end
end

ActiveModel::Type.register(:datetime3decimal, DateTimeWith3Decimal)
Expand All @@ -17,7 +18,7 @@ class TypeTest < CouchbaseOrm::Base
attribute :size, :float
attribute :renewal_date, :date
attribute :subscribed_at, :datetime
attribute :some_time, :timestamp
attribute :some_time, :timestamp, precision: 0
attribute :precision3_time, :datetime3decimal
attribute :precision6_time, :datetime, precision: 6

Expand Down
Loading