Skip to content

Commit

Permalink
Merge 6d450fe into 2e31a14
Browse files Browse the repository at this point in the history
  • Loading branch information
arenoir committed Apr 17, 2014
2 parents 2e31a14 + 6d450fe commit a259743
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
create_table :ar_comments_tags, force: true do |t|
t.references :ar_comment, :ar_tag, index: true
end

create_table :ar_tasks, force: true do |t|
t.string :name
end

create_table :ar_lists, force: true do |t|
t.string :name
t.string :listable_type
t.string :listable_id
end

create_table :ar_items, force: true do |t|
t.string :description
t.belongs_to :ar_list, index: true
end

end

class ARPost < ActiveRecord::Base
Expand All @@ -53,6 +69,20 @@ class ARTag < ActiveRecord::Base
class ARSection < ActiveRecord::Base
end

class ARTask < ActiveRecord::Base
has_one :ar_list, as: 'listable', class_name: 'ARList'
end

class ARList < ActiveRecord::Base
belongs_to :listable, polymorphic: true
has_many :ar_items, class_name: 'ARItem'
end

class ARItem < ActiveRecord::Base
belongs_to :ar_list, class_name: 'ARList'
end


class ARPostSerializer < ActiveModel::Serializer
attributes :title, :body

Expand All @@ -74,6 +104,22 @@ class ARSectionSerializer < ActiveModel::Serializer
attributes 'name'
end


class ARItemSerializer < ActiveModel::Serializer
attributes :description
end

class ARListSerializer < ActiveModel::Serializer
attributes :name
has_many :ar_items, serializer: ARItemSerializer
end

class ARTaskSerializer < ActiveModel::Serializer
attributes :name
has_one :ar_list, serializer: ARListSerializer
end


ARPost.create(title: 'New post',
body: 'A body!!!',
ar_section: ARSection.create(name: 'ruby')).tap do |post|
Expand All @@ -90,3 +136,14 @@ class ARSectionSerializer < ActiveModel::Serializer
comment.ar_tags.concat happy_tag, short_tag
end
end


ARList.create(
name: 'list for task',
listable: ARTask.create(name: 'task')
).tap do |list|
list.ar_items.create(description: 'task list item')
end



19 changes: 19 additions & 0 deletions test/integration/active_record/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Serializer
class ActiveRecordTest < Minitest::Test
def setup
@post = ARPost.first

@task = ARTask.first

end

def test_serialization_embedding_objects
Expand Down Expand Up @@ -58,6 +61,22 @@ def test_serialization_embedding_ids_including_in_root
end
end

def test_serialization_polymorphic_has_one_wrapped_in_array
task_serializer = ARTaskSerializer.new(@task)

assert_equal({
'ar_task' => {
name: 'task',
ar_list: [
{ name: 'list for task', ar_items: [
{ description: 'task list item'}
]}
]
}
}, task_serializer.as_json)

end

private

def embed(serializer_class, options = {})
Expand Down
34 changes: 34 additions & 0 deletions test/unit/active_model/array_serializer/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,40 @@ def profile
ensure
UserSerializer._associations[:profile] = @old_association
end


def test_embed_object_in_root_for_has_one_association_with_empty_polymorphic_has_one_relationship
@association = ARTaskSerializer._associations[:ar_list]
@old_association = @association.dup

@association.embed = :ids
@association.embed_in_root = true

@list_association = ARListSerializer._associations[:ar_items]
@old_list_association = @list_association.dup

@list_association.embed = :ids
@list_association.embed_in_root = true


@task1 = ARTask.new({ name: 'Task 1'})
@task2 = ARTask.new({ name: 'Task 2'})

@list = ARList.create(name: 'list', listable: @task1).tap { |l|
l.ar_items.create(description: 'test')
}


@serializer = ArraySerializer.new([@task1, @task2], root: :lists)
assert_equal({
tasks: []
}, @serializer.as_json)
ensure
ARTaskSerializer._associations[:ar_list] = @old_association
ARListSerializer._associations[:ar_items] = @old_list_association
end


end
end
end

0 comments on commit a259743

Please sign in to comment.