Skip to content

Commit 9a952e6

Browse files
committed
Drop the setter methods cache in favour of respond_to? (see #472)
The cache was not being updated when new attributes were added, causing very strange behaviour. Any previously unseen attributes passed to new with send_only_modified_attributes enabled would be missing from the request parameters. This is because change tracking is only effective for setter methods. Updating the cache turned out to be more expensive than not having a cache at all. Using respond_to? would be fastest but this breaks things as respond_to_missing? returns true for any assignment method. If method(:foo=).source_location returns nil then this may indicate that the method is missing but this is also true for methods in native code. A faster and more reliable approach is using a fiber-local variable to make respond_to_missing? return false while doing these checks. It's a tad ugly but it's the best I can come up with.
1 parent c319c63 commit 9a952e6

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/her/model/attributes.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@ def method_missing(method, *args, &blk)
5555

5656
# @private
5757
def respond_to_missing?(method, include_private = false)
58+
return false if Thread.current[:her_respond_to_missing]
5859
method.to_s =~ /[?=]$/ || @_her_attributes.include?(method) || super
5960
end
6061

62+
def respond_to_without_missing?(method, include_private = false)
63+
Thread.current[:her_respond_to_missing] = true
64+
respond_to?(method, include_private)
65+
ensure
66+
# Normally we would use nil to delete the variable but this is
67+
# slightly more expensive and performance counts here.
68+
Thread.current[:her_respond_to_missing] = false
69+
end
70+
6171
# Assign new attributes to a resource
6272
#
6373
# @example
@@ -202,10 +212,9 @@ def use_setter_methods(model, params = {})
202212
reserved = [:id, model.class.primary_key, *model.class.association_keys]
203213
model.class.attributes *params.keys.reject { |k| reserved.include?(k) }
204214

205-
setter_method_names = model.class.setter_method_names
206215
params.each_with_object({}) do |(key, value), memo|
207216
setter_method = "#{key}="
208-
if setter_method_names.include?(setter_method)
217+
if model.respond_to_without_missing?(setter_method)
209218
model.send setter_method, value
210219
else
211220
memo[key.to_sym] = value
@@ -279,15 +288,6 @@ def store_metadata(value = nil)
279288
store_her_data(:metadata, value)
280289
end
281290

282-
# @private
283-
def setter_method_names
284-
@_her_setter_method_names ||= begin
285-
instance_methods.each_with_object(Set.new) do |method, memo|
286-
memo << method.to_s if method.to_s.end_with?('=')
287-
end
288-
end
289-
end
290-
291291
private
292292
# @private
293293
def store_her_data(name, value)

spec/model/attributes_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ def document=(document)
241241
@user = Foo::User.find(1)
242242
expect(@user.document).to eq("http://example.com")
243243
end
244+
245+
it 'exposes the method to respond_to? and respond_to_without_missing?' do
246+
@user = Foo::User.find(1)
247+
expect(@user.respond_to?(:document=)).to be_truthy
248+
expect(@user.respond_to_without_missing?(:document=)).to be_truthy
249+
end
250+
251+
it 'exposes a non-existent method to respond_to? but not respond_to_without_missing?' do
252+
@user = Foo::User.find(1)
253+
expect(@user.respond_to?(:nonexistent=)).to be_truthy
254+
expect(@user.respond_to_without_missing?(:nonexistent=)).to be_falsey
255+
end
244256
end
245257

246258
context "for predicate method" do

0 commit comments

Comments
 (0)