From 320a42bac6777b8e5ffaba0e0a1288874aea5784 Mon Sep 17 00:00:00 2001 From: Pierre Merlin Date: Tue, 27 Dec 2022 18:02:40 +0100 Subject: [PATCH] first implem --- lib/couchbase-orm/base.rb | 5 +++++ spec/base_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 8f77c1a7..3c7b5c9d 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -23,6 +23,7 @@ require 'couchbase-orm/utilities/index' require 'couchbase-orm/utilities/has_many' require 'couchbase-orm/utilities/ensure_unique' +require 'couchbase-orm/utilities/ignored_properties' module CouchbaseOrm @@ -127,6 +128,7 @@ class Base extend EnsureUnique extend HasMany extend Index + extend IgnoredProperties Metadata = Struct.new(:key, :cas) @@ -176,6 +178,9 @@ def find(*ids, quiet: false) } records.compact! ids.length > 1 ? records : records[0] + rescue ActiveModel::UnknownAttributeError => e + unknown_attribute = e.attribute + raise e unless ignored_properties.include? unknown_attribute end def find_by_id(*ids, **options) diff --git a/spec/base_spec.rb b/spec/base_spec.rb index d2c5e685..0dbfc515 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -193,4 +193,29 @@ class TimestampTest < CouchbaseOrm::Base describe CompareTest do it_behaves_like "ActiveModel" end + + describe '.ignored_properties' do + class BaseTestWithIgnoredProperties < CouchbaseOrm::Base + ignored_properties :depracted_property + attribute :name, :string + attribute :job, :string + end + + it 'returns an array of ignored properties' do + expect(BaseTestWithIgnoredProperties.ignored_properties).to eq(['depracted_property']) + end + + it 'ignores the ignored properties on load from db' do + too_much_properties_doc = { + type: BaseTestWithIgnoredProperties.design_document, + name: 'Pierre', + job: 'dev', + depracted_property: 'depracted that could be removed on next save' + } + BaseTestWithIgnoredProperties.bucket.default_collection.upsert 'doc_1', too_much_properties_doc + + base = BaseTestWithIgnoredProperties.find_by_id('doc_1') + BaseTestWithIgnoredProperties.bucket.default_collection.remove 'doc_1' + end + end end