Skip to content
Merged
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
5 changes: 5 additions & 0 deletions motion/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ def update_attributes(attrs)
def read_attribute(name)
@data[name]
end

def write_attribute(attr_name, value)
@data[attr_name] = value
@dirty = true
end

# Default to_i implementation returns value of id column, much as
# in Rails.
Expand Down
28 changes: 28 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class ATask
columns :name, :details, :some_day
end

class BTask
include MotionModel::Model
include MotionModel::ArrayModelAdapter
columns :name, :details
def details=(value)
write_attribute(:details, "overridden")
end
end

class TypeCast
include MotionModel::Model
include MotionModel::ArrayModelAdapter
Expand Down Expand Up @@ -254,6 +263,25 @@ class TypeCast
@task.custom_attribute_by_method.should == 'Feed the Cat - Get food, pour out'
end
end

describe 'overloading accessors using write_attribute' do
before do
BTask.delete_all
end

it 'updates the attribute on creation' do
@task = BTask.create :name => 'foo', :details => 'bar'
@task.details.should.equal('overridden')
@task.should.not.be.dirty
end

it 'updates the attribute but does not save a new instance' do
@task = BTask.new :name => 'foo', :details => 'bar'
@task.details.should.equal('overridden')
@task.should.be.dirty
end

end

describe 'protecting timestamps' do
class NoTimestamps
Expand Down