Skip to content

Commit 2751977

Browse files
committed
Implement attribute_changed_in_place? for dirty tracking
Ref: remi#383 Closes: remi#382 Override ActiveModel::Dirty's attribute_changed_in_place? to use Her's change tracking, enabling validates_numericality_of and other validators that depend on in-place change detection.
1 parent 8277697 commit 2751977

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/her/model.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ module Model
4444
include ActiveModel::Conversion
4545
include ActiveModel::Dirty
4646

47+
# Override ActiveModel::Dirty's attribute_changed_in_place? to use
48+
# Her's change tracking. This allows validates_numericality_of to work.
49+
def attribute_changed_in_place?(attribute_name)
50+
!changes[attribute_name.to_s].nil?
51+
end
52+
4753
# Class methods
4854
included do
4955
# Assign the default API

spec/model/attributes_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
expect(@new_user.get_attribute(:unknown_method_for_a_user)).to be_nil
7575
expect(@new_user.get_attribute(:'life-span')).to eq("3 years")
7676
end
77+
78+
it "handles attribute_changed_in_place?" do
79+
@new_user = Foo::User.new
80+
@new_user.fullname = "Schmoo"
81+
expect(@new_user.attribute_changed_in_place?(:fullname)).to be_truthy
82+
end
7783
end
7884

7985
context "assigning new resource data" do

spec/model/validations_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
context "validating attributes" do
77
before do
88
spawn_model "Foo::User" do
9-
attributes :fullname, :email
9+
attributes :fullname, :email, :age
1010
validates_presence_of :fullname
1111
validates_presence_of :email
12+
validates_numericality_of :age
1213
end
1314
end
1415

@@ -19,6 +20,7 @@
1920
expect(user.errors.full_messages).to include("Email can't be blank")
2021
user.fullname = "Tobias Fünke"
2122
user.email = "tobias@bluthcompany.com"
23+
user.age = 35
2224
expect(user).to be_valid
2325
end
2426
end

0 commit comments

Comments
 (0)