Skip to content

Commit

Permalink
Fix consistency issue between ActiveModel::Conversion and ActiveModel…
Browse files Browse the repository at this point in the history
…::Lint
  • Loading branch information
santib committed Jun 20, 2022
1 parent a466272 commit cab4ccf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
8 changes: 8 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* `ActiveModel::Lint` now tests that `#to_key` returns `nil` when model's `#id` returns `nil`.

This change is done to make `ActiveModel::Conversion` compliant with the ActiveModel API
tested by `ActiveModel::Lint`.

*Santiago Bartesaghi*


* Support infinite ranges for `LengthValidator`s `:in`/`:within` options

```ruby
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def to_model
end

# Returns an Array of all key attributes if any of the attributes is set, whether or not
# the object is persisted. Returns +nil+ if there are no key attributes.
# the object is persisted. Returns +nil+ if there are no key attributes set.
#
# class Person
# include ActiveModel::Conversion
Expand Down
6 changes: 3 additions & 3 deletions activemodel/lib/active_model/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ module Lint
# +self+.
module Tests
# Passes if the object's model responds to <tt>to_key</tt> and if calling
# this method returns +nil+ when the object is not persisted.
# this method returns +nil+ when there are no key attributes set.
# Fails otherwise.
#
# <tt>to_key</tt> returns an Enumerable of all (primary) key attributes
# of the model, and is used to a generate unique DOM id for the object.
def test_to_key
assert_respond_to model, :to_key
def model.persisted?() false end
assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
def model.id() nil end
assert model.to_key.nil?, "to_key should return nil when there are no key attributes set"
end

# Passes if the object's model responds to <tt>to_param</tt> and if
Expand Down
29 changes: 28 additions & 1 deletion activemodel/test/cases/lint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,34 @@ class CompliantModel
extend ActiveModel::Naming
include ActiveModel::Conversion

def persisted?() false end
def persisted?
false
end

def errors
Hash.new([])
end
end

def setup
@model = CompliantModel.new
end
end

class PersistedLintTest < ActiveModel::TestCase
include ActiveModel::Lint::Tests

class CompliantModel
extend ActiveModel::Naming
include ActiveModel::Conversion

def persisted?
true
end

def id
1
end

def errors
Hash.new([])
Expand Down

0 comments on commit cab4ccf

Please sign in to comment.