mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Merge pull request #1 from george/proper_root_node_for_compound_model_names
Grape::ActiveModelSerializers for models with compound names
This commit is contained in:
commit
e98149adb5
4 changed files with 41 additions and 4 deletions
|
|
@ -48,7 +48,7 @@ module Grape
|
||||||
|
|
||||||
if options[:root] != false && serializer.root != false
|
if options[:root] != false && serializer.root != false
|
||||||
# the serializer for an Array is ActiveModel::ArraySerializer
|
# the serializer for an Array is ActiveModel::ArraySerializer
|
||||||
options[:root] ||= serializer.root || resource.first.class.name.downcase.pluralize
|
options[:root] ||= serializer.root || resource.first.class.name.underscore.pluralize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,12 @@ ActiveRecord::Schema.define(version: 20130403105356) do
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "blog_posts", force: true do |t|
|
||||||
|
t.string "title"
|
||||||
|
t.string "body"
|
||||||
|
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -53,6 +53,27 @@ describe Grape::ActiveModelSerializers do
|
||||||
last_response.body.should == "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
|
last_response.body.should == "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "for models with compound names" do
|
||||||
|
it "should generate the proper 'root' node for individual objects" do
|
||||||
|
subject.get("/home") do
|
||||||
|
BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/home"
|
||||||
|
last_response.body.should == "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should generate the proper 'root' node for serialized arrays of objects" do
|
||||||
|
subject.get("/home") do
|
||||||
|
blog_post = BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
|
||||||
|
[blog_post, blog_post]
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/home"
|
||||||
|
last_response.body.should == "{\"blog_posts\":[{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"},{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}]}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# [User2Serializer, 'user2', :user2].each do |serializer|
|
# [User2Serializer, 'user2', :user2].each do |serializer|
|
||||||
# it "should render using serializer (#{serializer})" do
|
# it "should render using serializer (#{serializer})" do
|
||||||
# subject.get("/home", serializer: serializer) do
|
# subject.get("/home", serializer: serializer) do
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,11 @@ end
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
attributes :first_name, :last_name
|
attributes :first_name, :last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class BlogPost < ActiveRecord::Base
|
||||||
|
attr_accessor :title, :body
|
||||||
|
end
|
||||||
|
|
||||||
|
class BlogPostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :title, :body
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue