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

Add support for Alchemy Ingredients #51

Merged
merged 16 commits into from
Jul 7, 2021
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
1 change: 1 addition & 0 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def page_scope_with_includes
elements: [
:nested_elements,
{ contents: { essence: :ingredient_association } },
{ ingredients: :related_object },
],
},
},
Expand Down
5 changes: 5 additions & 0 deletions app/serializers/alchemy/json_api/element_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class ElementSerializer
element.contents.map(&:essence)
end

has_many :ingredients,
serializer: ->(record) do
"Alchemy::JsonApi::Ingredient#{record.type.demodulize}Serializer".constantize
end

has_many :nested_elements, record_type: :element, serializer: self
end
end
Expand Down
40 changes: 40 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_audio_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientAudioSerializer
include IngredientSerializer

attributes(
:autoplay,
:controls,
:muted,
:loop,
)

attribute :value do |ingredient|
ingredient.attachment&.url
end

with_options if: ->(ingredient) { ingredient.attachment } do
attribute :audio_name do |ingredient|
ingredient.attachment.name
end

attribute :audio_file_name do |ingredient|
ingredient.attachment.file_name
end

attribute :audio_mime_type do |ingredient|
ingredient.attachment.file_mime_type
end

attribute :audio_file_size do |ingredient|
ingredient.attachment.file_size
end
end
end
end
end
11 changes: 11 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_boolean_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientBooleanSerializer
include IngredientSerializer
end
end
end
11 changes: 11 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_datetime_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientDatetimeSerializer
include IngredientSerializer
end
end
end
35 changes: 35 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_file_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientFileSerializer
include IngredientSerializer

attribute :link_title, &:title

attribute :value do |ingredient|
ingredient.attachment&.url
end

with_options if: proc { |ingredient| ingredient.attachment } do
attribute :attachment_name do |ingredient|
ingredient.attachment.name
end

attribute :attachment_file_name do |ingredient|
ingredient.attachment.file_name
end

attribute :attachment_mime_type do |ingredient|
ingredient.attachment.file_mime_type
end

attribute :attachment_file_size do |ingredient|
ingredient.attachment.file_size
end
end
end
end
end
13 changes: 13 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_headline_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientHeadlineSerializer
include IngredientSerializer

attributes :level, :size
end
end
end
11 changes: 11 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_html_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientHtmlSerializer
include IngredientSerializer
end
end
end
17 changes: 17 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_link_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientLinkSerializer
include IngredientSerializer

attributes(
:link_class_name,
:link_target,
:link_title
)
end
end
end
35 changes: 35 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_node_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientNodeSerializer
include IngredientSerializer

attribute :value do |ingredient|
ingredient.node&.name
end

belongs_to :node, record_type: :node, serializer: ::Alchemy::JsonApi::NodeSerializer

with_options if: proc { |ingredient| ingredient.node } do
attribute :name do |ingredient|
ingredient.node.name
end

attribute :link_url do |ingredient|
ingredient.node.url
end

attribute :link_title do |ingredient|
ingredient.node.title
end

attribute :link_nofollow do |ingredient|
ingredient.node.nofollow
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_page_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientPageSerializer
include IngredientSerializer

attribute :value do |ingredient|
ingredient.page&.url_path
end

attribute :page_name do |ingredient|
ingredient.page&.name
end

attribute :page_url do |ingredient|
ingredient.page&.url_path
end

has_one :page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer
end
end
end
60 changes: 60 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_picture_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientPictureSerializer
include IngredientSerializer

attributes(
:title,
:caption,
:link_class_name,
:link_title,
:link_target,
)

attribute :value do |ingredient|
ingredient.picture&.url
end

attribute :alt_text, &:alt_tag
attribute :link_url, &:link

with_options if: proc { |ingredient| ingredient.picture.present? } do
attribute :image_dimensions do |ingredient|
sizes = ingredient.settings[:size]&.split("x", 2)&.map(&:to_i) || [
ingredient.image_file_width,
ingredient.image_file_height,
]

ratio = ingredient.image_file_width.to_f / ingredient.image_file_height
width = sizes[0].zero? ? sizes[1] * ratio : sizes[0]
height = sizes[1].zero? ? sizes[0] / ratio : sizes[1]

{
width: width,
height: height,
}
end

attribute :image_name do |ingredient|
ingredient.picture.name
end

attribute :image_file_name do |ingredient|
ingredient.picture.image_file_name
end

attribute :image_mime_type do |ingredient|
"image/#{ingredient.picture.image_file_format}"
end

attribute :image_file_size do |ingredient|
ingredient.picture.image_file_size
end
end
end
end
end
18 changes: 18 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_richtext_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientRichtextSerializer
include IngredientSerializer

attributes(
:sanitized_body,
:stripped_body,
)

attribute :body, &:value
end
end
end
11 changes: 11 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_select_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientSelectSerializer
include IngredientSerializer
end
end
end
22 changes: 22 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_text_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientTextSerializer
include IngredientSerializer

attributes(
:link,
:link_class_name,
:link_target,
:link_title,
)

# maintain compatibility with EssenceText
attribute :body, &:value
attribute :link_url, &:link
end
end
end
42 changes: 42 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_video_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "alchemy/json_api/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientVideoSerializer
include IngredientSerializer

attributes(
:width,
:height,
:allow_fullscreen,
:autoplay,
:controls,
:preload,
)

attribute :value do |ingredient|
ingredient.attachment&.url
end

with_options if: ->(ingredient) { ingredient.attachment } do
attribute :video_name do |ingredient|
ingredient.attachment.name
end

attribute :video_file_name do |ingredient|
ingredient.attachment.file_name
end

attribute :video_mime_type do |ingredient|
ingredient.attachment.file_mime_type
end

attribute :video_file_size do |ingredient|
ingredient.attachment.file_size
end
end
end
end
end
Loading