Skip to content

Commit d11b81b

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 a460f38 commit d11b81b

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 =~ /[?=]$/ || @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
@@ -194,10 +204,9 @@ def use_setter_methods(model, params = {})
194204
reserved = [:id, model.class.primary_key, *model.class.association_keys]
195205
model.class.attributes *params.keys.reject { |k| reserved.include?(k) }
196206

197-
setter_method_names = model.class.setter_method_names
198207
params.each_with_object({}) do |(key, value), memo|
199208
setter_method = key.to_s + '='
200-
if setter_method_names.include?(setter_method)
209+
if model.respond_to_without_missing?(setter_method)
201210
model.send(setter_method, value)
202211
else
203212
memo[key.to_sym] = value
@@ -271,15 +280,6 @@ def store_metadata(value = nil)
271280
store_her_data(:metadata, value)
272281
end
273282

274-
# @private
275-
def setter_method_names
276-
@_her_setter_method_names ||= begin
277-
instance_methods.each_with_object(Set.new) do |method, memo|
278-
memo << method.to_s if method.to_s.end_with?('=')
279-
end
280-
end
281-
end
282-
283283
private
284284
# @private
285285
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)