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

feat(alba): Handle inline associations #7

Closed

Conversation

okuramasafumi
Copy link
Contributor

Fix #6

Abstract

This change introduces a new class InlineType for handling inline associations of Alba.
I'd like to know if this approach is good not not. If it's good, I'll add some tests for this case.

Details

There are two things to do:

  • Skip anonymous serializer/resource classes
  • Render inline association

I'll add some notes as a comment later.

Introducing `InlineType` class for handling inline associations.
This commit also adds `require "logger"` to avoid an error with console.
(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? }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a condition to check anonymous serializer.

# 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heredoc is used as a template. This can be a separate file, but it is short enough.

end

def name
properties = SerializerPlugins::Alba.new(serializer: @serializer, config: @config).properties
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of dirty hack, but it's recursive so that inline associations can be deeply nested.

@@ -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) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the next line InlineType causes an error, so we need to reject it here.

@skryukov
Copy link
Owner

@okuramasafumi thanks for your PR! I tried it and noticed that InlineType ignores Typelizer's DSL, i.e.:

class FooSerializer
  include Alba::Resource
  helper Typelizer::DSL

  many :bars do
    typelize_from Bar

    attributes :id, :name
  end
end

So I tried to fix it in #8, would love to hear your thoughts!

@skryukov skryukov closed this Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Alba] associations with block create anonymous classes and typelizer doesn't handle them well
2 participants