Skip to content

Commit

Permalink
feat(alba): Handle inline associations
Browse files Browse the repository at this point in the history
Introducing `InlineType` class for handling inline associations.
This commit also adds `require "logger"` to avoid an error with console.
  • Loading branch information
okuramasafumi authored and skryukov committed Sep 27, 2024
1 parent 5284071 commit 13d603e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/typelizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

require_relative "typelizer/railtie" if defined?(Rails)

require "logger"

module Typelizer
class << self
def enabled?
Expand Down
6 changes: 5 additions & 1 deletion lib/typelizer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class Config < Struct.new(
class << self
def instance
@instance ||= new(
serializer_name_mapper: ->(serializer) { serializer.name.ends_with?("Serializer") ? serializer.name.delete_suffix("Serializer") : serializer.name.delete_suffix("Resource") },
serializer_name_mapper: ->(serializer) do
return "" if serializer.name.nil?

serializer.name.ends_with?("Serializer") ? serializer.name&.delete_suffix("Serializer") : serializer.name&.delete_suffix("Resource")
end,
serializer_model_mapper: ->(serializer) do
base_class = serializer_name_mapper.call(serializer)
Object.const_get(base_class) if Object.const_defined?(base_class)
Expand Down
5 changes: 3 additions & 2 deletions lib/typelizer/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def target_serializers
raise ArgumentError, "Please ensure all your serializers include Typelizer::DSL." if base_classes.none?
end

(base_classes + base_classes.flat_map(&:descendants)).uniq.sort_by(&:name)
.reject { |serializer| Typelizer.reject_class.call(serializer: serializer) }
(base_classes + base_classes.flat_map(&:descendants)).uniq
.reject { |serializer| Typelizer.reject_class.call(serializer: serializer) || serializer.name.nil? }
.sort_by(&:name)
end

def read_serializers(files = nil)
Expand Down
22 changes: 22 additions & 0 deletions lib/typelizer/inline_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Typelizer
# InlineType is mainly used with Alba plugin to represent inline associations.
# `name `method` is the same interface as `Interface` class.
class InlineType
TEMPLATE = <<~ERB.strip
{
<%- properties.each do |property| %>
<%= property %>;
<% end %>
}
ERB
def initialize(serializer:, config:)
@serializer = serializer
@config = config
end

def name
properties = SerializerPlugins::Alba.new(serializer: @serializer, config: @config).properties
ERB.new(TEMPLATE, trim_mode: "-").result_with_hash(properties: properties)
end
end
end
1 change: 1 addition & 0 deletions lib/typelizer/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def imports
.filter_map { |interface| interface.name if interface.name != name }

custom_type_imports = attribute_types
.reject { |type| type.is_a?(InlineType) }
.flat_map { |type| extract_typescript_types(type.to_s) }
.uniq
.reject { |type| global_type?(type) }
Expand Down
4 changes: 3 additions & 1 deletion lib/typelizer/serializer_plugins/alba.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "base"
require_relative "../inline_type"

module Typelizer
module SerializerPlugins
Expand Down Expand Up @@ -74,9 +75,10 @@ def build_property(name, attr, **options)
)
when ::Alba::Association
resource = attr.instance_variable_get(:@resource)
type = (resource.is_a?(Class) && !resource.name.nil?) ? Interface.new(serializer: resource) : InlineType.new(serializer: resource, config: {})
Property.new(
name: name,
type: Interface.new(serializer: resource),
type: type,
optional: false,
nullable: false,
multi: false, # we override this in typelize_method_transform
Expand Down

0 comments on commit 13d603e

Please sign in to comment.