Skip to content

Commit b26338e

Browse files
committed
Better tests for AV::RecordIdentifier
This commit intends to clarify the scope of ActionView::RecordIdentifier methods `dom_id` and `dom_class`. Most of the current documentation comes from da257eb (7 years ago) when the decoupling of ActionView, ActiveRecord and ActiveModel was not a concern. Since then, steps have been taken to reach such decoupling. Therefore I think it's important to show that ActionView::RecordIdentifier **does not strictly depend on the ActiveRecord API**: any class `Post` implementing `post.to_key` and `post.model_name.param_key` will work. This commit adds a test to prove that ActionView::RecordIdentifier methods can also be used on objects that do not subclass ActiveRecord::Base.
1 parent 8259d79 commit b26338e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

actionview/test/lib/controller/fake_models.rb

+12
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,15 @@ def each
170170

171171
class Car < Struct.new(:color)
172172
end
173+
174+
class Plane
175+
attr_reader :to_key
176+
177+
def model_name
178+
OpenStruct.new param_key: 'airplane'
179+
end
180+
181+
def save
182+
@to_key = [1]
183+
end
184+
end

actionview/test/template/record_identifier_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,46 @@ def test_dom_class_as_singleton_method
4646
assert_equal @singular, ActionView::RecordIdentifier.dom_class(@record)
4747
end
4848
end
49+
50+
class RecordIdentifierWithoutActiveModelTest < ActiveSupport::TestCase
51+
include ActionView::RecordIdentifier
52+
53+
def setup
54+
@record = Plane.new
55+
end
56+
57+
def test_dom_id_with_new_record
58+
assert_equal "new_airplane", dom_id(@record)
59+
end
60+
61+
def test_dom_id_with_new_record_and_prefix
62+
assert_equal "custom_prefix_airplane", dom_id(@record, :custom_prefix)
63+
end
64+
65+
def test_dom_id_with_saved_record
66+
@record.save
67+
assert_equal "airplane_1", dom_id(@record)
68+
end
69+
70+
def test_dom_id_with_prefix
71+
@record.save
72+
assert_equal "edit_airplane_1", dom_id(@record, :edit)
73+
end
74+
75+
def test_dom_class
76+
assert_equal 'airplane', dom_class(@record)
77+
end
78+
79+
def test_dom_class_with_prefix
80+
assert_equal "custom_prefix_airplane", dom_class(@record, :custom_prefix)
81+
end
82+
83+
def test_dom_id_as_singleton_method
84+
@record.save
85+
assert_equal "airplane_1", ActionView::RecordIdentifier.dom_id(@record)
86+
end
87+
88+
def test_dom_class_as_singleton_method
89+
assert_equal 'airplane', ActionView::RecordIdentifier.dom_class(@record)
90+
end
91+
end

0 commit comments

Comments
 (0)