Skip to content

Commit

Permalink
Add specs for serializer json_key overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklandgrebe committed Jan 25, 2017
1 parent e3e5d3a commit c9fb6bf
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 1 deletion.
5 changes: 5 additions & 0 deletions spec/dummy/app/models/attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Attachment < ApplicationRecord
belongs_to :post

validates_presence_of :name
end
2 changes: 2 additions & 0 deletions spec/dummy/app/models/attachments/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Image < Attachment
end
1 change: 1 addition & 0 deletions spec/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Post < ApplicationRecord
belongs_to :user, autosave: true

has_many :attachments
has_many :comments

validates_presence_of :title
Expand Down
9 changes: 9 additions & 0 deletions spec/dummy/app/serializers/api/v1/attachment_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module API
module V1
class AttachmentSerializer < ApplicationSerializer
attributes :name, :created_at, :updated_at

belongs_to :post
end
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/serializers/api/v1/post_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class PostSerializer < ApplicationSerializer

belongs_to :user

has_many :attachments
has_many :comments
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

config.autoload_paths += %W(#{config.root}/app/models/attachments)
end
end
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20170125204237_create_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateAttachments < ActiveRecord::Migration
def change
create_table :attachments do |t|
t.string :type
t.string :name
t.references :post
t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160916024225) do
ActiveRecord::Schema.define(version: 20170125204237) do

create_table "attachments", force: :cascade do |t|
t.string "type"
t.string "name"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "comments", force: :cascade do |t|
t.string "body", null: false
Expand Down
10 changes: 10 additions & 0 deletions spec/dummy/log/development.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateAttachments (20170125204237)
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateAttachments (20170125204237)
 (0.1ms) begin transaction
DEPRECATION WARNING: `#timestamps` was called without specifying an option for `null`. In Rails 5, this behavior will change to `null: false`. You should manually specify `null: true` to prevent the behavior of your existing migrations from changing. (called from block in change at /Users/nicklandgrebe/dev/caprese/spec/dummy/db/migrate/20170125204237_create_attachments.rb:7)
 (0.9ms) CREATE TABLE "attachments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar, "name" varchar, "post_id" integer, "created_at" datetime, "updated_at" datetime) 
SQL (18.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20170125204237"]]
 (1.2ms) commit transaction
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
18 changes: 18 additions & 0 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@
# TODO: Implement and spec only_path option
end

describe 'type' do
before { get "/api/v1/#{resource.class.name.underscore.pluralize}/#{resource.id}" }

subject(:resource) { comments.first }

it 'uses the resource model name' do
expect(json['data']['type']).to eq('comments')
end

context 'when resource is serialized by parent resource model serializer' do
subject(:resource) { create :post, :with_attachments }

it 'uses the parent resource model name' do
expect(json['data']['relationships']['attachments']['data'][0]['type']).to eq('attachments')
end
end
end

context 'when records do not exist' do
let!(:nonexistent_id) { comments.first.id }
before { Comment.destroy_all }
Expand Down
6 changes: 6 additions & 0 deletions spec/support/factories/attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :attachment do
sequence(:name) { |n| "Attachment #{n}" }
post
end
end
10 changes: 10 additions & 0 deletions spec/support/factories/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@
create_list(:comment, evaluator.comment_count, post: post)
end
end

trait :with_attachments do
transient do
attachment_count 1
end

after(:create) do |post, evaluator|
create_list(:attachment, evaluator.attachment_count, post: post)
end
end
end
end

0 comments on commit c9fb6bf

Please sign in to comment.