Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow key transforms #64

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ const pages = deserializePages(data)
console.log(pages[0].name) // => Homepage
```

## Key transforms

If you ever want to change how Alchemy serializes attributes you can set

```rb
Alchemy::JsonApi.key_transform = :camel_lower
```

It defaults to `:underscore`.

## Contributing
Contribution directions go here.

Expand Down
9 changes: 9 additions & 0 deletions app/serializers/alchemy/json_api/base_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Alchemy
module JsonApi
class BaseSerializer
include JSONAPI::Serializer

set_key_transform Alchemy::JsonApi.key_transform
end
end
end
4 changes: 1 addition & 3 deletions app/serializers/alchemy/json_api/element_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true
module Alchemy
module JsonApi
class ElementSerializer
include JSONAPI::Serializer

class ElementSerializer < BaseSerializer
attributes(
:name,
:fixed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class EssenceAudioSerializer
class EssenceAudioSerializer < BaseSerializer
include EssenceSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceBooleanSerializer
class EssenceBooleanSerializer < BaseSerializer
include EssenceSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceDateSerializer
class EssenceDateSerializer < BaseSerializer
include EssenceSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceFileSerializer
class EssenceFileSerializer < BaseSerializer
include EssenceSerializer

attribute :link_title, &:title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class EssenceHeadlineSerializer
class EssenceHeadlineSerializer < BaseSerializer
include EssenceSerializer

attributes :level, :size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceHtmlSerializer
class EssenceHtmlSerializer < BaseSerializer
include EssenceSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceLinkSerializer
class EssenceLinkSerializer < BaseSerializer
include EssenceSerializer
attributes(
:link_title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceNodeSerializer
class EssenceNodeSerializer < BaseSerializer
include EssenceSerializer

attribute :ingredient do |essence|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssencePageSerializer
class EssencePageSerializer < BaseSerializer
include EssenceSerializer

attribute :ingredient do |essence|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssencePictureSerializer
class EssencePictureSerializer < BaseSerializer
include EssenceSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceRichtextSerializer
class EssenceRichtextSerializer < BaseSerializer
include EssenceSerializer
attributes(
:body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceSelectSerializer
class EssenceSelectSerializer < BaseSerializer
include EssenceSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Alchemy
module JsonApi
class EssenceTextSerializer
class EssenceTextSerializer < BaseSerializer
include EssenceSerializer
attributes(
:body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class EssenceVideoSerializer
class EssenceVideoSerializer < BaseSerializer
include EssenceSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientAudioSerializer
class IngredientAudioSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientBooleanSerializer
class IngredientBooleanSerializer < BaseSerializer
include IngredientSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientDatetimeSerializer
class IngredientDatetimeSerializer < BaseSerializer
include IngredientSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientFileSerializer
class IngredientFileSerializer < BaseSerializer
include IngredientSerializer

attribute :link_title, &:title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientHeadlineSerializer
class IngredientHeadlineSerializer < BaseSerializer
include IngredientSerializer

attributes :level, :size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientHtmlSerializer
class IngredientHtmlSerializer < BaseSerializer
include IngredientSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientLinkSerializer
class IngredientLinkSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientNodeSerializer
class IngredientNodeSerializer < BaseSerializer
include IngredientSerializer

attribute :value do |ingredient|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientPageSerializer
class IngredientPageSerializer < BaseSerializer
include IngredientSerializer

attribute :value do |ingredient|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientPictureSerializer
class IngredientPictureSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientRichtextSerializer
class IngredientRichtextSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientSelectSerializer
class IngredientSelectSerializer < BaseSerializer
include IngredientSerializer
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientTextSerializer
class IngredientTextSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Alchemy
module JsonApi
class IngredientVideoSerializer
class IngredientVideoSerializer < BaseSerializer
include IngredientSerializer

attributes(
Expand Down
4 changes: 1 addition & 3 deletions app/serializers/alchemy/json_api/language_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true
module Alchemy
module JsonApi
class LanguageSerializer
include JSONAPI::Serializer

class LanguageSerializer < BaseSerializer
attributes(
:name,
:language_code,
Expand Down
4 changes: 1 addition & 3 deletions app/serializers/alchemy/json_api/node_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true
module Alchemy
module JsonApi
class NodeSerializer
include JSONAPI::Serializer

class NodeSerializer < BaseSerializer
attributes :name
attribute :link_url, &:url
attribute :link_title, &:title
Expand Down
4 changes: 1 addition & 3 deletions app/serializers/alchemy/json_api/page_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true
module Alchemy
module JsonApi
class PageSerializer
include JSONAPI::Serializer

class PageSerializer < BaseSerializer
ELEMENT_SERIALIZER = ::Alchemy::JsonApi::ElementSerializer

attributes(
Expand Down
20 changes: 19 additions & 1 deletion lib/alchemy/json_api.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# frozen_string_literal: true

require "fast_jsonapi"
require "alchemy_cms"
require "alchemy/json_api/engine"

module Alchemy
module JsonApi
# Your code goes here...
# Set FastJsonapi key_transform
#
# Supported transformations:
#
# :camel # "some_key" => "SomeKey"
# :camel_lower # "some_key" => "someKey"
# :dash # "some_key" => "some-key"
# :underscore # "some_key" => "some_key"
def self.key_transform=(value)
@_key_transform = value
end

# FastJsonapi key_transform
#
# Default :underscore # "some_key" => "some_key"
def self.key_transform
@_key_transform || :underscore
end
end
end
1 change: 0 additions & 1 deletion lib/alchemy/json_api/essence_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Alchemy
module JsonApi
module EssenceSerializer
def self.included(klass)
klass.include JSONAPI::Serializer
klass.has_one :element, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |essence|
essence.content.element
end
Expand Down
2 changes: 0 additions & 2 deletions lib/alchemy/json_api/ingredient_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Alchemy
module JsonApi
module IngredientSerializer
def self.included(klass)
klass.include JSONAPI::Serializer

klass.has_one :element, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer

klass.attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
describe "attributes" do
subject { serializer.serializable_hash[:data][:attributes] }

it "has the right keys and values" do
it "has the right transformed keys and values" do
transform_method = FastJsonapi::ObjectSerializer::TRANSFORMS_MAPPING[Alchemy::JsonApi.key_transform]
transformed_keys = %w[created_at updated_at].map { |k| k.send(*transform_method).to_sym }

expect(subject).to have_key(:role)
expect(subject).to have_key(:value)
expect(subject).to have_key(:created_at)
expect(subject).to have_key(:updated_at)
transformed_keys.each do |transformed_key|
expect(subject).to have_key(transformed_key)
end
expect(subject[:deprecated]).to be(false)
end

Expand Down
Loading