Skip to content

Implemented attribute_changed_in_place? on models so validates_numericality_of validation will work #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions lib/her/model/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions spec/model/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion spec/model/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,6 +19,7 @@
user.errors.full_messages.should include("Email can't be blank")
user.fullname = "Tobias Fünke"
user.email = "[email protected]"
user.age = 35
user.should be_valid
end
end
Expand Down