mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #15240 from chancancode/fix_attribute_methods
Fixed serialization for records with an attribute named `format`.
This commit is contained in:
commit
8b36471501
6 changed files with 31 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
|||
* Fixed serialization for records with an attribute named `format`.
|
||||
|
||||
Fixes #15188.
|
||||
|
||||
*Godfrey Chan*
|
||||
|
||||
* When a `group` is set, `sum`, `size`, `average`, `minimum` and `maximum`
|
||||
on a NullRelation should return a Hash.
|
||||
|
||||
|
|
|
@ -66,6 +66,7 @@ module ActiveRecord
|
|||
# Generates all the attribute related methods for columns in the database
|
||||
# accessors, mutators and query methods.
|
||||
def define_attribute_methods # :nodoc:
|
||||
return false if @attribute_methods_generated
|
||||
# Use a mutex; we don't want two thread simultaneously trying to define
|
||||
# attribute methods.
|
||||
generated_attribute_methods.synchronize do
|
||||
|
|
|
@ -286,6 +286,8 @@ module ActiveRecord
|
|||
|
||||
@new_record = false
|
||||
|
||||
self.class.define_attribute_methods
|
||||
|
||||
run_callbacks :find
|
||||
run_callbacks :initialize
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
require "cases/helper"
|
||||
require 'models/contact'
|
||||
require 'models/topic'
|
||||
require 'models/book'
|
||||
|
||||
class SerializationTest < ActiveRecord::TestCase
|
||||
fixtures :books
|
||||
|
||||
FORMATS = [ :xml, :json ]
|
||||
|
||||
def setup
|
||||
|
@ -65,4 +68,20 @@ class SerializationTest < ActiveRecord::TestCase
|
|||
ensure
|
||||
ActiveRecord::Base.include_root_in_json = original_root_in_json
|
||||
end
|
||||
|
||||
def test_read_attribute_for_serialization_with_format_after_init
|
||||
klazz = Class.new(ActiveRecord::Base)
|
||||
klazz.table_name = 'books'
|
||||
|
||||
book = klazz.new(format: 'paperback')
|
||||
assert_equal 'paperback', book.read_attribute_for_serialization(:format)
|
||||
end
|
||||
|
||||
def test_read_attribute_for_serialization_with_format_after_find
|
||||
klazz = Class.new(ActiveRecord::Base)
|
||||
klazz.table_name = 'books'
|
||||
|
||||
book = klazz.find(books(:awdr).id)
|
||||
assert_equal 'paperback', book.read_attribute_for_serialization(:format)
|
||||
end
|
||||
end
|
||||
|
|
2
activerecord/test/fixtures/books.yml
vendored
2
activerecord/test/fixtures/books.yml
vendored
|
@ -2,8 +2,10 @@ awdr:
|
|||
author_id: 1
|
||||
id: 1
|
||||
name: "Agile Web Development with Rails"
|
||||
format: "paperback"
|
||||
|
||||
rfr:
|
||||
author_id: 1
|
||||
id: 2
|
||||
name: "Ruby for Rails"
|
||||
format: "ebook"
|
||||
|
|
|
@ -103,6 +103,7 @@ ActiveRecord::Schema.define do
|
|||
|
||||
create_table :books, force: true do |t|
|
||||
t.integer :author_id
|
||||
t.string :format
|
||||
t.column :name, :string
|
||||
t.column :status, :integer, default: 0
|
||||
t.column :read_status, :integer, default: 0
|
||||
|
|
Loading…
Reference in a new issue