Skip to content

Commit 82167ad

Browse files
Merge pull request #463 from seanpdoyle/deprecate-attributes-write
2 parents 1353080 + 55c7155 commit 82167ad

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

lib/active_resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module ActiveResource
3535

3636
URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::RFC2396_Parser.new
3737

38+
autoload :AttributeSet
3839
autoload :Base
3940
autoload :Callbacks
4041
autoload :Coder
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveResource
4+
class AttributeSet < DelegateClass(Hash) # :nodoc:
5+
MESSAGE = "Writing to the attributes hash is deprecated. Set attributes directly on the instance instead."
6+
7+
deprecate(**[ :[]=, :store, :update, :merge! ].index_with(MESSAGE),
8+
deprecator: ActiveResource.deprecator)
9+
10+
delegate :is_a?, to: :__getobj__
11+
end
12+
end

lib/active_resource/base.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,9 +1291,17 @@ def split_options(options = {})
12911291
end
12921292
end
12931293

1294-
attr_accessor :attributes # :nodoc:
12951294
attr_accessor :prefix_options # :nodoc:
12961295

1296+
def attributes=(value) # :nodoc:
1297+
ActiveResource.deprecator.warn("#attributes= is deprecated. Call #load on the instance instead.")
1298+
@attributes = value
1299+
end
1300+
1301+
def attributes # :nodoc:
1302+
AttributeSet.new(@attributes)
1303+
end
1304+
12971305
# If no schema has been defined for the class (see
12981306
# <tt>ActiveResource::schema=</tt>), the default automatic schema is
12991307
# generated from the current instance's attributes
@@ -1400,7 +1408,7 @@ def id
14001408

14011409
# Sets the <tt>\id</tt> attribute of the resource.
14021410
def id=(id)
1403-
attributes[self.class.primary_key] = id
1411+
@attributes[self.class.primary_key] = id
14041412
end
14051413

14061414
# Test for equality. Resource are equal if and only if +other+ is the same object or
@@ -1454,7 +1462,7 @@ def hash
14541462
# next_invoice.customer # => That Company
14551463
def dup
14561464
self.class.new.tap do |resource|
1457-
resource.attributes = @attributes
1465+
resource.send :instance_variable_set, "@attributes", @attributes
14581466
resource.prefix_options = @prefix_options
14591467
end
14601468
end
@@ -1857,7 +1865,7 @@ def method_missing(method_symbol, *arguments) # :nodoc:
18571865
if method_name =~ /(=|\?)$/
18581866
case $1
18591867
when "="
1860-
attributes[$`] = arguments.first
1868+
@attributes[$`] = arguments.first
18611869
when "?"
18621870
attributes[$`]
18631871
end

test/cases/base_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,4 +1743,38 @@ def test_paths_without_format
17431743
ensure
17441744
ActiveResource::Base.include_format_in_path = true
17451745
end
1746+
1747+
def test_deprecate_attributes_write
1748+
person = Person.find(1)
1749+
1750+
assert_deprecated("#attributes= is deprecated. Call #load on the instance instead.", ActiveResource.deprecator) do
1751+
person.attributes = { "name" => "changed" }
1752+
end
1753+
1754+
assert_equal "changed", person.name
1755+
end
1756+
1757+
def test_deprecate_attributes_store
1758+
[ :[]=, :store ].each do |store|
1759+
person = Person.find(1)
1760+
1761+
assert_deprecated("Writing to the attributes hash is deprecated. Set attributes directly on the instance instead.", ActiveResource.deprecator) do
1762+
person.attributes.send(store, "name", "changed")
1763+
end
1764+
1765+
assert_equal "changed", person.name
1766+
end
1767+
end
1768+
1769+
def test_deprecate_attributes_update
1770+
[ :update, :merge! ].each do |update|
1771+
person = Person.find(1)
1772+
1773+
assert_deprecated("Writing to the attributes hash is deprecated. Set attributes directly on the instance instead.", ActiveResource.deprecator) do
1774+
person.attributes.send(update, "name" => "changed")
1775+
end
1776+
1777+
assert_equal "changed", person.name
1778+
end
1779+
end
17461780
end

0 commit comments

Comments
 (0)