After looking through docs and finding this outdated thread I believe I've found the solution. You can mark attributes as dirty by changing the persistence_state of a DataMapper::Resource
instance.
DirtyState = DataMapper::Resource::PersistenceState::Dirty
quantity_property = ProductQuantity.properties[:quantity]
old_quantity = prod_quantity1.quantity
prod_quantity1.quantity.value = 500
dirty_state = DataMapper::Resource::PersistenceState::Dirty.new(prod_quantity1)
dirty_state.original_attributes[quantity_property] = old_quantity
prod_quantity1.persistence_state = dirty_state
expect(prod_quantity1.persistence_state.is_a? DirtyState).to eql(true)
expect(prod_quantity1.dirty?).to eql(true)
prod_quantity1.save
expect(prod_quantity1.dirty?).to eql(false)
Basically we create a DataMapper::Resource::PersistenceState::Dirty
object and then using the "quantity" property of my ProductQuantity as the key, we set the value as the old object in original_attributes
. Any non-empty map will return true for prod_quantity1.dirty?
.
I've made it into a module below. Just call <resource_instance>.make_dirty(*attributes)
with some attributes names.
require 'data_mapper'
module DataMapper
module Resource
# Make the give attributes dirty
def make_dirty(*attributes)
if attributes.empty?
return
end
unless self.clean?
self.save
end
dirty_state = DataMapper::Resource::PersistenceState::Dirty.new(self)
attributes.each do |attribute|
property = self.class.properties[attribute]
# Any value will do here and return true for self.dirty?, but it expects the old version of this attribute.
dirty_state.original_attributes[property] = nil
self.persistence_state = dirty_state
end
end
end
end
And I've tested it below:
describe DataMapper::Resource do
before(:all) do
DataMapper.auto_migrate!
@prod1 = Product.create(:name => 'Snickers')
end
after(:all) do
DataMapper.auto_migrate!
end
it 'should fail when an attribute is not dirty' do
prod_quantity1 = ProductQuantity.create(:product => @prod1, :quantity => UnitValue.new(300, 'kg'))
prod_quantity1.quantity.value = 400
expect(prod_quantity1.dirty?).to eql(false)
prod_quantity1.save
prod_quantity1 = ProductQuantity.get(prod_quantity1.id)
expect(prod_quantity1.quantity.value).to eql(300)
end
it 'should mark an attribute as dirty' do
prod_quantity1 = ProductQuantity.create(:product => @prod1, :quantity => UnitValue.new(300, 'kg'))
prod_quantity1.quantity.value = 400
expect(prod_quantity1.dirty?).to eql(false)
prod_quantity1.make_dirty(:quantity)
expect(prod_quantity1.dirty?).to eql(true)
prod_quantity1.save
expect(prod_quantity1.quantity.value).to eql(400)
expect(prod_quantity1.dirty?).to eql(false)
end
end