Skip to content

Commit

Permalink
Merge pull request #11 from AlchemyCMS/use-full-module-path
Browse files Browse the repository at this point in the history
Use full module paths in serializers
  • Loading branch information
tvdeyen authored Oct 28, 2020
2 parents ad9f9b6 + 4a098d0 commit 8757236
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 129 deletions.
38 changes: 20 additions & 18 deletions app/serializers/alchemy/json_api/element_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# frozen_string_literal: true
module Alchemy::JsonApi
class ElementSerializer
include FastJsonapi::ObjectSerializer
attributes(
:position,
:created_at,
:updated_at,
)
attribute :element_type, &:name
belongs_to :parent_element, record_type: :element, serializer: self
module Alchemy
module JsonApi
class ElementSerializer
include FastJsonapi::ObjectSerializer
attributes(
:position,
:created_at,
:updated_at,
)
attribute :element_type, &:name
belongs_to :parent_element, record_type: :element, serializer: self

belongs_to :page
has_many :essences, polymorphic: true do |element|
element.contents.map(&:essence)
end
belongs_to :page
has_many :essences, polymorphic: true do |element|
element.contents.map(&:essence)
end

has_many :nested_elements, record_type: :element, serializer: self
has_many :nested_elements, record_type: :element, serializer: self

with_options if: ->(_, params) { params[:admin] == true } do
attribute :tag_list
attribute :display_name, &:display_name_with_preview_text
with_options if: ->(_, params) { params[:admin] == true } do
attribute :tag_list
attribute :display_name, &:display_name_with_preview_text
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceBooleanSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceBooleanSerializer
include EssenceSerializer
end
end
end
8 changes: 5 additions & 3 deletions app/serializers/alchemy/json_api/essence_date_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceDateSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceDateSerializer
include EssenceSerializer
end
end
end
36 changes: 19 additions & 17 deletions app/serializers/alchemy/json_api/essence_file_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceFileSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceFileSerializer
include EssenceSerializer

attribute :link_title, &:title
attribute :link_title, &:title

attribute :ingredient, &:attachment_url
attribute :ingredient, &:attachment_url

with_options if: proc { |essence| essence.attachment.present? } do
attribute :attachment_name do |essence|
essence.attachment.name
end
with_options if: proc { |essence| essence.attachment.present? } do
attribute :attachment_name do |essence|
essence.attachment.name
end

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

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

attribute :attachment_file_size do |essence|
essence.attachment.file_size
attribute :attachment_file_size do |essence|
essence.attachment.file_size
end
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/serializers/alchemy/json_api/essence_html_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceHtmlSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceHtmlSerializer
include EssenceSerializer
end
end
end
16 changes: 9 additions & 7 deletions app/serializers/alchemy/json_api/essence_link_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceLinkSerializer
include EssenceSerializer
attributes(
:link_title,
:link_target,
)
module Alchemy
module JsonApi
class EssenceLinkSerializer
include EssenceSerializer
attributes(
:link_title,
:link_target,
)
end
end
end
40 changes: 21 additions & 19 deletions app/serializers/alchemy/json_api/essence_node_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceNodeSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceNodeSerializer
include EssenceSerializer

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

belongs_to :node
belongs_to :node

with_options if: proc { |essence| essence.node.present? } do
attribute :name do |essence|
essence.node.name
end
with_options if: proc { |essence| essence.node.present? } do
attribute :name do |essence|
essence.node.name
end

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

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

attribute :link_nofollow do |essence|
essence.node.nofollow
attribute :link_nofollow do |essence|
essence.node.nofollow
end
end
end
end
Expand Down
28 changes: 15 additions & 13 deletions app/serializers/alchemy/json_api/essence_page_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssencePageSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssencePageSerializer
include EssenceSerializer

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

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

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

has_one :page, record_type: :page, serializer: PageSerializer
has_one :page, record_type: :page, serializer: PageSerializer
end
end
end
56 changes: 29 additions & 27 deletions app/serializers/alchemy/json_api/essence_picture_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssencePictureSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssencePictureSerializer
include EssenceSerializer

attributes(
:title,
:caption,
:link_title,
:link_target,
)
attribute :ingredient, &:picture_url
attribute :alt_text, &:alt_tag
attribute :link_url, &:link
attributes(
:title,
:caption,
:link_title,
:link_target,
)
attribute :ingredient, &:picture_url
attribute :alt_text, &:alt_tag
attribute :link_url, &:link

with_options if: proc { |essence| essence.picture.present? } do
attribute :image_dimensions do |essence|
essence.sizes_from_string(essence.render_size)
end
with_options if: proc { |essence| essence.picture.present? } do
attribute :image_dimensions do |essence|
essence.sizes_from_string(essence.render_size)
end

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

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

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

attribute :image_file_size do |essence|
essence.picture.image_file_size
attribute :image_file_size do |essence|
essence.picture.image_file_size
end
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions app/serializers/alchemy/json_api/essence_richtext_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceRichtextSerializer
include EssenceSerializer
attributes(
:body,
:stripped_body,
)
module Alchemy
module JsonApi
class EssenceRichtextSerializer
include EssenceSerializer
attributes(
:body,
:stripped_body,
)
end
end
end
8 changes: 5 additions & 3 deletions app/serializers/alchemy/json_api/essence_select_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceSelectSerializer
include EssenceSerializer
module Alchemy
module JsonApi
class EssenceSelectSerializer
include EssenceSerializer
end
end
end
20 changes: 11 additions & 9 deletions app/serializers/alchemy/json_api/essence_text_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssenceTextSerializer
include EssenceSerializer
attributes(
:body,
:link_title,
:link_target,
)
attribute :link_url, &:link
module Alchemy
module JsonApi
class EssenceTextSerializer
include EssenceSerializer
attributes(
:body,
:link_title,
:link_target,
)
attribute :link_url, &:link
end
end
end

0 comments on commit 8757236

Please sign in to comment.