File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ * Add ` ActiveModel::Conversion.param_delimiter ` to configure delimiter being used in ` to_param `
2
+
3
+ * Nikita Vasilevsky*
4
+
1
5
* ` undefine_attribute_methods ` undefines alias attribute methods along with attribute methods.
2
6
3
7
* Nikita Vasilevsky*
Original file line number Diff line number Diff line change @@ -24,6 +24,14 @@ module ActiveModel
24
24
module Conversion
25
25
extend ActiveSupport ::Concern
26
26
27
+ included do
28
+ ##
29
+ # :singleton-method:
30
+ #
31
+ # Accepts a string that will be used as a delimiter of object's key values in the `to_param` method.
32
+ class_attribute :param_delimiter , instance_reader : false , default : "-"
33
+ end
34
+
27
35
# If your object is already designed to implement all of the \Active \Model
28
36
# you can use the default <tt>:to_model</tt> implementation, which simply
29
37
# returns +self+.
@@ -80,7 +88,7 @@ def to_key
80
88
# person = Person.new(1)
81
89
# person.to_param # => "1"
82
90
def to_param
83
- ( persisted? && key = to_key ) ? key . join ( "-" ) : nil
91
+ ( persisted? && key = to_key ) ? key . join ( self . class . param_delimiter ) : nil
84
92
end
85
93
86
94
# Returns a +string+ identifying the path associated with the object.
Original file line number Diff line number Diff line change @@ -53,4 +53,25 @@ def persisted?
53
53
test "to_partial_path handles namespaced models" do
54
54
assert_equal "helicopter/comanches/comanche" , Helicopter ::Comanche . new . to_partial_path
55
55
end
56
+
57
+ test "#to_param_delimiter allows redefining the delimiter used in #to_param" do
58
+ old_delimiter = Contact . param_delimiter
59
+ Contact . param_delimiter = "_"
60
+ assert_equal ( "abc_xyz" , Contact . new ( id : [ "abc" , "xyz" ] ) . to_param )
61
+ ensure
62
+ Contact . param_delimiter = old_delimiter
63
+ end
64
+
65
+ test "#to_param_delimiter is defined per class" do
66
+ old_contact_delimiter = Contact . param_delimiter
67
+ custom_contract = Class . new ( Contact )
68
+
69
+ Contact . param_delimiter = "_"
70
+ custom_contract . param_delimiter = ";"
71
+
72
+ assert_equal ( "abc_xyz" , Contact . new ( id : [ "abc" , "xyz" ] ) . to_param )
73
+ assert_equal ( "abc;xyz" , custom_contract . new ( id : [ "abc" , "xyz" ] ) . to_param )
74
+ ensure
75
+ Contact . param_delimiter = old_contact_delimiter
76
+ end
56
77
end
You can’t perform that action at this time.
0 commit comments