aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/attributes.rb
diff options
context:
space:
mode:
authorDylan Thacker-Smith <Dylan.Smith@shopify.com>2018-06-22 11:54:31 -0400
committerJeremy Daer <jeremydaer@gmail.com>2018-10-12 09:50:10 -0700
commit99c87ad2474d5c5b6e52ceac34c3cf9f9cb57f9f (patch)
treef1000530afb4b52af586e5486db5df50b52b9e81 /activemodel/lib/active_model/attributes.rb
parentee95bed3e6e16eadd1940d9c27953236f1649c08 (diff)
downloadrails-99c87ad2474d5c5b6e52ceac34c3cf9f9cb57f9f.tar.gz
rails-99c87ad2474d5c5b6e52ceac34c3cf9f9cb57f9f.tar.bz2
rails-99c87ad2474d5c5b6e52ceac34c3cf9f9cb57f9f.zip
Improve model attribute accessor method names for backtraces
Ruby uses the original method name, so will show the __temp__ method name in the backtrace. However, in the common case the method name is compatible with the `def` keyword, so we can avoid the __temp__ method name in that case to improve the name shown in backtraces or TracePoint#method_id.
Diffstat (limited to 'activemodel/lib/active_model/attributes.rb')
-rw-r--r--activemodel/lib/active_model/attributes.rb32
1 files changed, 10 insertions, 22 deletions
diff --git a/activemodel/lib/active_model/attributes.rb b/activemodel/lib/active_model/attributes.rb
index 41fe5168f3..c3a446098c 100644
--- a/activemodel/lib/active_model/attributes.rb
+++ b/activemodel/lib/active_model/attributes.rb
@@ -29,17 +29,16 @@ module ActiveModel
private
def define_method_attribute=(name)
- safe_name = name.unpack1("h*")
- ActiveModel::AttributeMethods::AttrNames.set_name_cache safe_name, name
-
- generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
- def __temp__#{safe_name}=(value)
- name = ::ActiveModel::AttributeMethods::AttrNames::ATTR_#{safe_name}
- write_attribute(name, value)
- end
- alias_method #{(name + '=').inspect}, :__temp__#{safe_name}=
- undef_method :__temp__#{safe_name}=
- STR
+ ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
+ generated_attribute_methods, name, writer: true,
+ ) do |temp_method_name, attr_name_expr|
+ generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{temp_method_name}(value)
+ name = #{attr_name_expr}
+ write_attribute(name, value)
+ end
+ RUBY
+ end
end
NO_DEFAULT_PROVIDED = Object.new # :nodoc:
@@ -97,15 +96,4 @@ module ActiveModel
write_attribute(attribute_name, value)
end
end
-
- module AttributeMethods #:nodoc:
- AttrNames = Module.new {
- def self.set_name_cache(name, value)
- const_name = "ATTR_#{name}"
- unless const_defined? const_name
- const_set const_name, -value
- end
- end
- }
- end
end