1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Restore attribute alias resolution for attribute_{for_inspect,present?}

It is considered as a regression for #34384.
This commit is contained in:
Ryuta Kamizono 2020-06-14 10:34:38 +09:00
parent 3bd14f4653
commit 586acfca60
3 changed files with 5 additions and 3 deletions

View file

@ -305,6 +305,7 @@ module ActiveRecord
# # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]"
def attribute_for_inspect(attr_name)
attr_name = attr_name.to_s
attr_name = self.class.attribute_aliases[attr_name] || attr_name
value = _read_attribute(attr_name)
format_for_inspect(value)
end
@ -326,6 +327,7 @@ module ActiveRecord
# task.attribute_present?(:is_done) # => true
def attribute_present?(attr_name)
attr_name = attr_name.to_s
attr_name = self.class.attribute_aliases[attr_name] || attr_name
value = _read_attribute(attr_name)
!value.nil? && !(value.respond_to?(:empty?) && value.empty?)
end

View file

@ -101,9 +101,7 @@ module ActiveRecord
current_time = current_time_from_proper_timezone
all_timestamp_attributes_in_model.each do |column|
if !attribute_present?(column)
_write_attribute(column, current_time)
end
_write_attribute(column, current_time) unless _read_attribute(column)
end
end

View file

@ -34,6 +34,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
t.title = "The First Topic Now Has A Title With\nNewlines And More Than 50 Characters"
assert_equal '"The First Topic Now Has A Title With\nNewlines And ..."', t.attribute_for_inspect(:title)
assert_equal '"The First Topic Now Has A Title With\nNewlines And ..."', t.attribute_for_inspect(:heading)
end
test "attribute_for_inspect with a date" do
@ -69,6 +70,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
t.written_on = Time.now
t.author_name = ""
assert t.attribute_present?("title")
assert t.attribute_present?("heading")
assert t.attribute_present?("written_on")
assert_not t.attribute_present?("content")
assert_not t.attribute_present?("author_name")