diff --git a/README.md b/README.md index 1a79755..24277ad 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/serializers/alchemy/json_api/base_serializer.rb b/app/serializers/alchemy/json_api/base_serializer.rb new file mode 100644 index 0000000..18cae81 --- /dev/null +++ b/app/serializers/alchemy/json_api/base_serializer.rb @@ -0,0 +1,9 @@ +module Alchemy + module JsonApi + class BaseSerializer + include JSONAPI::Serializer + + set_key_transform Alchemy::JsonApi.key_transform + end + end +end diff --git a/app/serializers/alchemy/json_api/element_serializer.rb b/app/serializers/alchemy/json_api/element_serializer.rb index 6b6d4e8..50fb8ad 100644 --- a/app/serializers/alchemy/json_api/element_serializer.rb +++ b/app/serializers/alchemy/json_api/element_serializer.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true module Alchemy module JsonApi - class ElementSerializer - include JSONAPI::Serializer - + class ElementSerializer < BaseSerializer attributes( :name, :fixed, diff --git a/app/serializers/alchemy/json_api/essence_audio_serializer.rb b/app/serializers/alchemy/json_api/essence_audio_serializer.rb index 3567aaf..7aab287 100644 --- a/app/serializers/alchemy/json_api/essence_audio_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_audio_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class EssenceAudioSerializer + class EssenceAudioSerializer < BaseSerializer include EssenceSerializer attributes( diff --git a/app/serializers/alchemy/json_api/essence_boolean_serializer.rb b/app/serializers/alchemy/json_api/essence_boolean_serializer.rb index beb721c..13a74b3 100644 --- a/app/serializers/alchemy/json_api/essence_boolean_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_boolean_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceBooleanSerializer + class EssenceBooleanSerializer < BaseSerializer include EssenceSerializer end end diff --git a/app/serializers/alchemy/json_api/essence_date_serializer.rb b/app/serializers/alchemy/json_api/essence_date_serializer.rb index fca7e0c..1f7aa5f 100644 --- a/app/serializers/alchemy/json_api/essence_date_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_date_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceDateSerializer + class EssenceDateSerializer < BaseSerializer include EssenceSerializer end end diff --git a/app/serializers/alchemy/json_api/essence_file_serializer.rb b/app/serializers/alchemy/json_api/essence_file_serializer.rb index 5a5dc13..bee5b81 100644 --- a/app/serializers/alchemy/json_api/essence_file_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_file_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceFileSerializer + class EssenceFileSerializer < BaseSerializer include EssenceSerializer attribute :link_title, &:title diff --git a/app/serializers/alchemy/json_api/essence_headline_serializer.rb b/app/serializers/alchemy/json_api/essence_headline_serializer.rb index 57ee26c..878d5f4 100644 --- a/app/serializers/alchemy/json_api/essence_headline_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_headline_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class EssenceHeadlineSerializer + class EssenceHeadlineSerializer < BaseSerializer include EssenceSerializer attributes :level, :size diff --git a/app/serializers/alchemy/json_api/essence_html_serializer.rb b/app/serializers/alchemy/json_api/essence_html_serializer.rb index d7ea43f..4eba86e 100644 --- a/app/serializers/alchemy/json_api/essence_html_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_html_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceHtmlSerializer + class EssenceHtmlSerializer < BaseSerializer include EssenceSerializer end end diff --git a/app/serializers/alchemy/json_api/essence_link_serializer.rb b/app/serializers/alchemy/json_api/essence_link_serializer.rb index 96f937e..2d00fab 100644 --- a/app/serializers/alchemy/json_api/essence_link_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_link_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceLinkSerializer + class EssenceLinkSerializer < BaseSerializer include EssenceSerializer attributes( :link_title, diff --git a/app/serializers/alchemy/json_api/essence_node_serializer.rb b/app/serializers/alchemy/json_api/essence_node_serializer.rb index 3cceb9b..2c5756d 100644 --- a/app/serializers/alchemy/json_api/essence_node_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_node_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceNodeSerializer + class EssenceNodeSerializer < BaseSerializer include EssenceSerializer attribute :ingredient do |essence| diff --git a/app/serializers/alchemy/json_api/essence_page_serializer.rb b/app/serializers/alchemy/json_api/essence_page_serializer.rb index 006741c..de2cafd 100644 --- a/app/serializers/alchemy/json_api/essence_page_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_page_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssencePageSerializer + class EssencePageSerializer < BaseSerializer include EssenceSerializer attribute :ingredient do |essence| diff --git a/app/serializers/alchemy/json_api/essence_picture_serializer.rb b/app/serializers/alchemy/json_api/essence_picture_serializer.rb index 5f26d76..a39d9c3 100644 --- a/app/serializers/alchemy/json_api/essence_picture_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_picture_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssencePictureSerializer + class EssencePictureSerializer < BaseSerializer include EssenceSerializer attributes( diff --git a/app/serializers/alchemy/json_api/essence_richtext_serializer.rb b/app/serializers/alchemy/json_api/essence_richtext_serializer.rb index 8df4329..902ee07 100644 --- a/app/serializers/alchemy/json_api/essence_richtext_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_richtext_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceRichtextSerializer + class EssenceRichtextSerializer < BaseSerializer include EssenceSerializer attributes( :body, diff --git a/app/serializers/alchemy/json_api/essence_select_serializer.rb b/app/serializers/alchemy/json_api/essence_select_serializer.rb index 608210c..d58d608 100644 --- a/app/serializers/alchemy/json_api/essence_select_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_select_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceSelectSerializer + class EssenceSelectSerializer < BaseSerializer include EssenceSerializer end end diff --git a/app/serializers/alchemy/json_api/essence_text_serializer.rb b/app/serializers/alchemy/json_api/essence_text_serializer.rb index 5564785..2ad4f79 100644 --- a/app/serializers/alchemy/json_api/essence_text_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_text_serializer.rb @@ -3,7 +3,7 @@ module Alchemy module JsonApi - class EssenceTextSerializer + class EssenceTextSerializer < BaseSerializer include EssenceSerializer attributes( :body, diff --git a/app/serializers/alchemy/json_api/essence_video_serializer.rb b/app/serializers/alchemy/json_api/essence_video_serializer.rb index fa16747..13fc803 100644 --- a/app/serializers/alchemy/json_api/essence_video_serializer.rb +++ b/app/serializers/alchemy/json_api/essence_video_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class EssenceVideoSerializer + class EssenceVideoSerializer < BaseSerializer include EssenceSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_audio_serializer.rb b/app/serializers/alchemy/json_api/ingredient_audio_serializer.rb index aa6c199..526863a 100644 --- a/app/serializers/alchemy/json_api/ingredient_audio_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_audio_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientAudioSerializer + class IngredientAudioSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb b/app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb index dd529e4..83589a8 100644 --- a/app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientBooleanSerializer + class IngredientBooleanSerializer < BaseSerializer include IngredientSerializer end end diff --git a/app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb b/app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb index 12eaa99..dbd69a4 100644 --- a/app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientDatetimeSerializer + class IngredientDatetimeSerializer < BaseSerializer include IngredientSerializer end end diff --git a/app/serializers/alchemy/json_api/ingredient_file_serializer.rb b/app/serializers/alchemy/json_api/ingredient_file_serializer.rb index a8a6faa..b49b52e 100644 --- a/app/serializers/alchemy/json_api/ingredient_file_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_file_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientFileSerializer + class IngredientFileSerializer < BaseSerializer include IngredientSerializer attribute :link_title, &:title diff --git a/app/serializers/alchemy/json_api/ingredient_headline_serializer.rb b/app/serializers/alchemy/json_api/ingredient_headline_serializer.rb index b6c0676..cd33a77 100644 --- a/app/serializers/alchemy/json_api/ingredient_headline_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_headline_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientHeadlineSerializer + class IngredientHeadlineSerializer < BaseSerializer include IngredientSerializer attributes :level, :size diff --git a/app/serializers/alchemy/json_api/ingredient_html_serializer.rb b/app/serializers/alchemy/json_api/ingredient_html_serializer.rb index 8b3e36b..cf1d766 100644 --- a/app/serializers/alchemy/json_api/ingredient_html_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_html_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientHtmlSerializer + class IngredientHtmlSerializer < BaseSerializer include IngredientSerializer end end diff --git a/app/serializers/alchemy/json_api/ingredient_link_serializer.rb b/app/serializers/alchemy/json_api/ingredient_link_serializer.rb index 62eb456..889adb8 100644 --- a/app/serializers/alchemy/json_api/ingredient_link_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_link_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientLinkSerializer + class IngredientLinkSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_node_serializer.rb b/app/serializers/alchemy/json_api/ingredient_node_serializer.rb index b8af76a..95630c1 100644 --- a/app/serializers/alchemy/json_api/ingredient_node_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_node_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientNodeSerializer + class IngredientNodeSerializer < BaseSerializer include IngredientSerializer attribute :value do |ingredient| diff --git a/app/serializers/alchemy/json_api/ingredient_page_serializer.rb b/app/serializers/alchemy/json_api/ingredient_page_serializer.rb index 4788dfe..0fe97f8 100644 --- a/app/serializers/alchemy/json_api/ingredient_page_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_page_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientPageSerializer + class IngredientPageSerializer < BaseSerializer include IngredientSerializer attribute :value do |ingredient| diff --git a/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb b/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb index 5906db1..88fea00 100644 --- a/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientPictureSerializer + class IngredientPictureSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb b/app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb index 59bcb9f..09de6ce 100644 --- a/app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientRichtextSerializer + class IngredientRichtextSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_select_serializer.rb b/app/serializers/alchemy/json_api/ingredient_select_serializer.rb index 2262343..0874d65 100644 --- a/app/serializers/alchemy/json_api/ingredient_select_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_select_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientSelectSerializer + class IngredientSelectSerializer < BaseSerializer include IngredientSerializer end end diff --git a/app/serializers/alchemy/json_api/ingredient_text_serializer.rb b/app/serializers/alchemy/json_api/ingredient_text_serializer.rb index ec1b18a..0bd068c 100644 --- a/app/serializers/alchemy/json_api/ingredient_text_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_text_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientTextSerializer + class IngredientTextSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/ingredient_video_serializer.rb b/app/serializers/alchemy/json_api/ingredient_video_serializer.rb index 1fb403c..a0ec68f 100644 --- a/app/serializers/alchemy/json_api/ingredient_video_serializer.rb +++ b/app/serializers/alchemy/json_api/ingredient_video_serializer.rb @@ -4,7 +4,7 @@ module Alchemy module JsonApi - class IngredientVideoSerializer + class IngredientVideoSerializer < BaseSerializer include IngredientSerializer attributes( diff --git a/app/serializers/alchemy/json_api/language_serializer.rb b/app/serializers/alchemy/json_api/language_serializer.rb index a6e0d96..b6456cd 100644 --- a/app/serializers/alchemy/json_api/language_serializer.rb +++ b/app/serializers/alchemy/json_api/language_serializer.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true module Alchemy module JsonApi - class LanguageSerializer - include JSONAPI::Serializer - + class LanguageSerializer < BaseSerializer attributes( :name, :language_code, diff --git a/app/serializers/alchemy/json_api/node_serializer.rb b/app/serializers/alchemy/json_api/node_serializer.rb index 2ef265b..c6ed2b0 100644 --- a/app/serializers/alchemy/json_api/node_serializer.rb +++ b/app/serializers/alchemy/json_api/node_serializer.rb @@ -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 diff --git a/app/serializers/alchemy/json_api/page_serializer.rb b/app/serializers/alchemy/json_api/page_serializer.rb index 4849b7d..824517e 100644 --- a/app/serializers/alchemy/json_api/page_serializer.rb +++ b/app/serializers/alchemy/json_api/page_serializer.rb @@ -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( diff --git a/lib/alchemy/json_api.rb b/lib/alchemy/json_api.rb index b6da561..b951893 100644 --- a/lib/alchemy/json_api.rb +++ b/lib/alchemy/json_api.rb @@ -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 diff --git a/lib/alchemy/json_api/essence_serializer.rb b/lib/alchemy/json_api/essence_serializer.rb index 08a1e58..ea3b72e 100644 --- a/lib/alchemy/json_api/essence_serializer.rb +++ b/lib/alchemy/json_api/essence_serializer.rb @@ -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 diff --git a/lib/alchemy/json_api/ingredient_serializer.rb b/lib/alchemy/json_api/ingredient_serializer.rb index 2cfa60c..f630eb4 100644 --- a/lib/alchemy/json_api/ingredient_serializer.rb +++ b/lib/alchemy/json_api/ingredient_serializer.rb @@ -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( diff --git a/lib/alchemy/json_api/test_support/ingredient_serializer_behaviour.rb b/lib/alchemy/json_api/test_support/ingredient_serializer_behaviour.rb index b29901e..547477c 100644 --- a/lib/alchemy/json_api/test_support/ingredient_serializer_behaviour.rb +++ b/lib/alchemy/json_api/test_support/ingredient_serializer_behaviour.rb @@ -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 diff --git a/spec/serializers/alchemy/json_api/base_serializer_spec.rb b/spec/serializers/alchemy/json_api/base_serializer_spec.rb new file mode 100644 index 0000000..e45890d --- /dev/null +++ b/spec/serializers/alchemy/json_api/base_serializer_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Alchemy::JsonApi::BaseSerializer do + it { expect(described_class).to be < JSONAPI::Serializer } + + it "sets key_transform" do + expect(described_class).to receive(:set_key_transform).with(:underscore) + load Alchemy::JsonApi::Engine.root.join("app/serializers/alchemy/json_api/base_serializer.rb") + end +end