diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 9cdf055a..8df19059 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -6,16 +6,87 @@ ActiveRecord::Base.establish_connection( ) ActiveRecord::Schema.define do - create_table :ar_profiles, :force => true do |t| + create_table :ar_posts, force: true do |t| + t.string :title + t.text :body + t.belongs_to :ar_section, index: true + t.timestamps + end + + create_table :ar_comments, force: true do |t| + t.text :body + t.belongs_to :ar_post, index: true + t.timestamps + end + + create_table :ar_tags, force: true do |t| t.string :name - t.string :description - t.string :comments + end + + create_table :ar_sections, force: true do |t| + t.string :name + end + + create_table :ar_posts_tags, force: true do |t| + t.references :ar_post, :ar_tag, index: true + end + + create_table :ar_comments_tags, force: true do |t| + t.references :ar_comment, :ar_tag, index: true end end -class ARProfile < ActiveRecord::Base +class ARPost < ActiveRecord::Base + has_many :ar_comments, class_name: 'ARComment' + has_and_belongs_to_many :ar_tags, class_name: 'ARTag' + belongs_to :ar_section, class_name: 'ARSection' end -class ARProfileSerializer < ActiveModel::Serializer - attributes :name, :description +class ARComment < ActiveRecord::Base + belongs_to :ar_post, class_name: 'ARPost' + has_and_belongs_to_many :ar_tags, class_name: 'ARTag' +end + +class ARTag < ActiveRecord::Base +end + +class ARSection < ActiveRecord::Base +end + +class ARPostSerializer < ActiveModel::Serializer + attributes :title, :body + + has_many :ar_comments, :ar_tags + has_one :ar_section +end + +class ARCommentSerializer < ActiveModel::Serializer + attributes :body + + has_many :ar_tags +end + +class ARTagSerializer < ActiveModel::Serializer + attributes :name +end + +class ARSectionSerializer < ActiveModel::Serializer + attributes :name +end + +ARPost.create(title: 'New post', + body: 'A body!!!', + ar_section: ARSection.create(name: 'ruby')).tap do |post| + + short_tag = post.ar_tags.create(name: 'short') + whiny_tag = post.ar_tags.create(name: 'whiny') + happy_tag = post.ar_tags.create(name: 'happy') + + post.ar_comments.create(body: 'what a dumb post').tap do |comment| + comment.ar_tags.concat short_tag, whiny_tag + end + + post.ar_comments.create(body: 'i liked it').tap do |comment| + comment.ar_tags.concat short_tag, happy_tag + end end diff --git a/test/integration/active_record/active_record_test.rb b/test/integration/active_record/active_record_test.rb index 65524402..cf914b90 100644 --- a/test/integration/active_record/active_record_test.rb +++ b/test/integration/active_record/active_record_test.rb @@ -5,25 +5,70 @@ module ActiveModel class Serializer class ActiveRecordTest < ActiveModel::TestCase def setup - @profile = ARProfile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - @profile_serializer = ARProfileSerializer.new(@profile) + @post = ARPost.first end - def test_attributes_definition - assert_equal([:name, :description], - @profile_serializer.class._attributes) - end + def test_serialization_embedding_objects + post_serializer = ARPostSerializer.new(@post) - def test_attributes_serialization_using_serializable_hash assert_equal({ - name: 'Name 1', description: 'Description 1' - }, @profile_serializer.serializable_hash) + 'ar_post' => { + title: 'New post', body: 'A body!!!', + ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] }, + { body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }], + ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }], + ar_section: { name: 'ruby' } + } + }, post_serializer.as_json) end - def test_attributes_serialization_using_as_json - assert_equal({ - 'ar_profile' => { name: 'Name 1', description: 'Description 1' } - }, @profile_serializer.as_json) + def test_serialization_embedding_ids + post_serializer = ARPostSerializer.new(@post) + + embed(ARPostSerializer, embed: :ids) do + assert_equal({ + 'ar_post' => { + title: 'New post', body: 'A body!!!', + 'ar_comment_ids' => [1, 2], + 'ar_tag_ids' => [1, 2, 3], + 'ar_section_id' => 1 + } + }, post_serializer.as_json) + end + end + + def test_serialization_embedding_ids_including_in_root + post_serializer = ARPostSerializer.new(@post) + + embed(ARPostSerializer, embed: :ids, include: true) do + assert_equal({ + 'ar_post' => { + title: 'New post', body: 'A body!!!', + 'ar_comment_ids' => [1, 2], + 'ar_tag_ids' => [1, 2, 3], + 'ar_section_id' => 1 + }, + ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] }, + { body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }], + ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }], + ar_section: { name: 'ruby' } + }, post_serializer.as_json) + end + end + + private + + def embed(klass, options = {}) + old_assocs = Hash[ARPostSerializer._associations.to_a.map { |(name, association)| [name, association.dup] }] + + ARPostSerializer._associations.each_value do |association| + association.embed = options[:embed] + association.embed_in_root = options[:include] + end + + yield + ensure + ARPostSerializer._associations = old_assocs end end end