From c9fb6bfcfe5ad6c3debf38021a14a120f97994fa Mon Sep 17 00:00:00 2001 From: Nick Landgrebe Date: Wed, 25 Jan 2017 15:13:08 -0600 Subject: [PATCH] Add specs for serializer json_key overriding --- spec/dummy/app/models/attachment.rb | 5 +++++ spec/dummy/app/models/attachments/image.rb | 2 ++ spec/dummy/app/models/post.rb | 1 + .../api/v1/attachment_serializer.rb | 9 +++++++++ .../app/serializers/api/v1/post_serializer.rb | 1 + spec/dummy/config/application.rb | 2 ++ .../20170125204237_create_attachments.rb | 10 ++++++++++ spec/dummy/db/schema.rb | 10 +++++++++- spec/dummy/log/development.log | 10 ++++++++++ spec/query_spec.rb | 18 ++++++++++++++++++ spec/support/factories/attachment.rb | 6 ++++++ spec/support/factories/post.rb | 10 ++++++++++ 12 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 spec/dummy/app/models/attachment.rb create mode 100644 spec/dummy/app/models/attachments/image.rb create mode 100644 spec/dummy/app/serializers/api/v1/attachment_serializer.rb create mode 100644 spec/dummy/db/migrate/20170125204237_create_attachments.rb create mode 100644 spec/support/factories/attachment.rb diff --git a/spec/dummy/app/models/attachment.rb b/spec/dummy/app/models/attachment.rb new file mode 100644 index 0000000..a2f5953 --- /dev/null +++ b/spec/dummy/app/models/attachment.rb @@ -0,0 +1,5 @@ +class Attachment < ApplicationRecord + belongs_to :post + + validates_presence_of :name +end diff --git a/spec/dummy/app/models/attachments/image.rb b/spec/dummy/app/models/attachments/image.rb new file mode 100644 index 0000000..014eb61 --- /dev/null +++ b/spec/dummy/app/models/attachments/image.rb @@ -0,0 +1,2 @@ +class Image < Attachment +end diff --git a/spec/dummy/app/models/post.rb b/spec/dummy/app/models/post.rb index 3aad76c..b28e525 100644 --- a/spec/dummy/app/models/post.rb +++ b/spec/dummy/app/models/post.rb @@ -1,6 +1,7 @@ class Post < ApplicationRecord belongs_to :user, autosave: true + has_many :attachments has_many :comments validates_presence_of :title diff --git a/spec/dummy/app/serializers/api/v1/attachment_serializer.rb b/spec/dummy/app/serializers/api/v1/attachment_serializer.rb new file mode 100644 index 0000000..7c80a97 --- /dev/null +++ b/spec/dummy/app/serializers/api/v1/attachment_serializer.rb @@ -0,0 +1,9 @@ +module API + module V1 + class AttachmentSerializer < ApplicationSerializer + attributes :name, :created_at, :updated_at + + belongs_to :post + end + end +end diff --git a/spec/dummy/app/serializers/api/v1/post_serializer.rb b/spec/dummy/app/serializers/api/v1/post_serializer.rb index 6ab0bd1..c1773b1 100644 --- a/spec/dummy/app/serializers/api/v1/post_serializer.rb +++ b/spec/dummy/app/serializers/api/v1/post_serializer.rb @@ -5,6 +5,7 @@ class PostSerializer < ApplicationSerializer belongs_to :user + has_many :attachments has_many :comments end end diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index f9f6c91..c48267f 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -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 diff --git a/spec/dummy/db/migrate/20170125204237_create_attachments.rb b/spec/dummy/db/migrate/20170125204237_create_attachments.rb new file mode 100644 index 0000000..b19e8d0 --- /dev/null +++ b/spec/dummy/db/migrate/20170125204237_create_attachments.rb @@ -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 diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 96461aa..fe7298e 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -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 diff --git a/spec/dummy/log/development.log b/spec/dummy/log/development.log index e69de29..0cecbbf 100644 --- a/spec/dummy/log/development.log +++ b/spec/dummy/log/development.log @@ -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" diff --git a/spec/query_spec.rb b/spec/query_spec.rb index 3ecc84f..a1fc11b 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -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 } diff --git a/spec/support/factories/attachment.rb b/spec/support/factories/attachment.rb new file mode 100644 index 0000000..070e8d6 --- /dev/null +++ b/spec/support/factories/attachment.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :attachment do + sequence(:name) { |n| "Attachment #{n}" } + post + end +end diff --git a/spec/support/factories/post.rb b/spec/support/factories/post.rb index d755980..af078e6 100644 --- a/spec/support/factories/post.rb +++ b/spec/support/factories/post.rb @@ -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