From e03b4ca05134d6f10aafa38dd2f94313ab0fbcf3 Mon Sep 17 00:00:00 2001 From: Greg Lappen Date: Thu, 29 Oct 2015 13:53:22 -0400 Subject: [PATCH] Implemented attribute_changed_in_place? on models so validates_numericality_of validation will work --- lib/her/model/attributes.rb | 6 ++++++ spec/model/attributes_spec.rb | 6 ++++++ spec/model/validations_spec.rb | 4 +++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/her/model/attributes.rb b/lib/her/model/attributes.rb index edf1facb..1a019a4e 100644 --- a/lib/her/model/attributes.rb +++ b/lib/her/model/attributes.rb @@ -132,6 +132,12 @@ def get_attribute(attribute_name) end alias attribute get_attribute + # Return `true` if the attribute was changed since last read. + # This allows validates_numericality_of to work properly. + def attribute_changed_in_place?(attribute_name) + !changes[attribute_name].nil? + end + # Return the value of the model `primary_key` attribute def id @attributes[self.class.primary_key] diff --git a/spec/model/attributes_spec.rb b/spec/model/attributes_spec.rb index 601795f1..c02d8240 100644 --- a/spec/model/attributes_spec.rb +++ b/spec/model/attributes_spec.rb @@ -65,6 +65,12 @@ @new_user.get_attribute(:unknown_method_for_a_user).should be_nil @new_user.get_attribute(:'life-span').should == '3 years' end + + it "handles attribute_changed_in_place?" do + @new_user = Foo::User.new + @new_user.fullname = 'Schmoo' + @new_user.attribute_changed_in_place?(:fullname).should be_truthy + end end diff --git a/spec/model/validations_spec.rb b/spec/model/validations_spec.rb index 6e7c7ce0..ea87c757 100644 --- a/spec/model/validations_spec.rb +++ b/spec/model/validations_spec.rb @@ -5,9 +5,10 @@ context "validating attributes" do before do spawn_model "Foo::User" do - attributes :fullname, :email + attributes :fullname, :email, :age validates_presence_of :fullname validates_presence_of :email + validates_numericality_of :age end end @@ -18,6 +19,7 @@ user.errors.full_messages.should include("Email can't be blank") user.fullname = "Tobias Fünke" user.email = "tobias@bluthcompany.com" + user.age = 35 user.should be_valid end end