-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Without those ActiveModelSerializers can't find a serializer for the site and language relations in the PageSerializer which is used in the PageSelect. It delegates serialization to Rails which takes whatever serializer is responsible for to_json, which in some cases lead to nesting the site or language with it's own root key, which is not what we want.
- Loading branch information
Showing
6 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class LanguageSerializer < ActiveModel::Serializer | ||
attributes :id, | ||
:name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class SiteSerializer < ActiveModel::Serializer | ||
attributes :id, | ||
:name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "API Pages requests", type: :request do | ||
describe "GET /api/pages/:id" do | ||
let(:page) { create(:alchemy_page, :public) } | ||
|
||
subject do | ||
get("/api/pages/#{page.id}") | ||
response | ||
end | ||
|
||
let(:json) { JSON.parse(response.body) } | ||
|
||
context "with unauthorized user" do | ||
it "returns page JSON with site not included" do | ||
is_expected.to have_http_status(200) | ||
expect(json).to_not include("site") | ||
end | ||
|
||
it "returns page JSON with language not included" do | ||
is_expected.to have_http_status(200) | ||
expect(json).to_not include("language") | ||
end | ||
end | ||
|
||
context "with authorized user" do | ||
before do | ||
authorize_user(build(:alchemy_dummy_user, :as_author)) | ||
end | ||
|
||
it "returns page JSON with site included" do | ||
is_expected.to have_http_status(200) | ||
expect(json).to include("site") | ||
expect(json["site"]).to include("id") | ||
expect(json["site"]).to include("name") | ||
end | ||
|
||
it "returns page JSON with language included" do | ||
is_expected.to have_http_status(200) | ||
expect(json).to include("language") | ||
expect(json["language"]).to include("id") | ||
expect(json["language"]).to include("name") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::LanguageSerializer do | ||
subject { described_class.new(language).to_json } | ||
|
||
let(:language) { build_stubbed(:alchemy_language) } | ||
|
||
it "includes all attributes" do | ||
json = JSON.parse(subject) | ||
expect(json).to eq( | ||
"id" => language.id, | ||
"name" => language.name | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::SiteSerializer do | ||
subject { described_class.new(site).to_json } | ||
|
||
let(:site) { build_stubbed(:alchemy_site) } | ||
|
||
it "includes all attributes" do | ||
json = JSON.parse(subject) | ||
expect(json).to eq( | ||
"id" => site.id, | ||
"name" => site.name | ||
) | ||
end | ||
end |