Skip to content

Commit

Permalink
Support typing method def in Alba
Browse files Browse the repository at this point in the history
* support typing method def in alba serlizers and correct key transfers for typelize on attribute defination syntax

* removed of nil and empty check, not needed

* added test cases for typing method definition and supporting key transforms for any custom definition

* remove debugger from gemfile

* updated snapshots from merge request

* changed path to use column_name instead of creating a new value
  • Loading branch information
patvice authored Feb 28, 2025
1 parent a556a68 commit 81cee0d
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/typelizer/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def global_type?(type)
def infer_types(props, hash_name = :_typelizer_attributes)
props.map do |prop|
if serializer.respond_to?(hash_name)
dsl_type = serializer.public_send(hash_name)[prop.name.to_sym]
dsl_type = serializer.public_send(hash_name)[prop.column_name.to_sym]
if dsl_type&.any?
next Property.new(prop.to_h.merge(dsl_type)).tap do |property|
property.comment ||= model_plugin.comment_for(property) if config.comments && property.comment != false
Expand Down
14 changes: 11 additions & 3 deletions lib/typelizer/serializer_plugins/alba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ def methods_to_typelize
:association, :one, :has_one,
:many, :has_many,
:attributes, :attribute,
:method_added,
:nested_attribute, :nested,
:meta
]
end

def typelize_method_transform(method:, name:, binding:, type:, attrs:)
return {name => [type, attrs.merge(multi: true)]} if [:many, :has_many].include?(method)
if method == :method_added && binding.local_variable_defined?(:method_name)
name = binding.local_variable_get(:method_name)
end

if [:many, :has_many].include?(method)
return {name => [type, attrs.merge(multi: true)]}
end

super
end
Expand Down Expand Up @@ -57,6 +64,7 @@ def meta_fields

def build_property(name, attr, **options)
column_name = name

if has_transform_key?(serializer)
name = fetch_key(serializer, name)
end
Expand All @@ -79,7 +87,7 @@ def build_property(name, attr, **options)
optional: false,
nullable: false,
multi: false,
column_name: nil,
column_name: column_name,
**options
)
when ::Alba::Association
Expand Down Expand Up @@ -112,7 +120,7 @@ def build_property(name, attr, **options)
optional: false,
nullable: false,
multi: false,
column_name: nil,
column_name: column_name,
**options
)
when ::Alba::ConditionalAttribute
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaMeta.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 43ee134e2e6f80e20688ca9bd209bfd0
// Typelizer digest fd9833ed4d30d052cff4f60e74676f9d
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaPost.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 798c35620a38313261940ac1cec08eee
// Typelizer digest 4dde0782bfee3cfaf1892d0e6cd0a51c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaUser} from '@/types'
Expand Down
6 changes: 5 additions & 1 deletion spec/__snapshots__/AlbaTransformKeys.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 58b5baecac4e1bb9ef54d2f4e64e3842
// Typelizer digest 17f7142c0d5a26d58694d5f93f6023fb
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaUser, AlbaPost} from '@/types'
Expand All @@ -8,9 +8,13 @@ type AlbaTransformKeys = {
id: string | null;
username: string;
active: boolean;
name: string;
/** This is sir name from the name */
sirName: string;
invitor: AlbaUser;
posts: Array<AlbaPost>;
latestPost: AlbaPost;
firstName: string;
createdAt: string;
}

Expand Down
6 changes: 5 additions & 1 deletion spec/__snapshots__/AlbaUser.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 99aa66b35b474b7af72aac978dde891e
// Typelizer digest 125c239fb273fd8cad480f2e7130aa17
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'
Expand All @@ -8,9 +8,13 @@ type AlbaUser = {
id: string | null;
username: string;
active: boolean;
name: string;
/** This is sir name from the name */
sir_name: string;
invitor: AlbaUser;
posts: Array<AlbaPost>;
latest_post: AlbaPost;
first_name: string;
}

export default AlbaUser;
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest be423353230851a0934123cc561c43e9
// Typelizer digest 910c1f41e8a80fe491c1a17897e6164f
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 6ea6be91fb1c96b6e98010baa319f80f
// Typelizer digest 8b5319b45aebff4a6732c8f0306115c8
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'
Expand Down
6 changes: 5 additions & 1 deletion spec/__snapshots__/AlbaUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest f0c977ce367b6dfd2a97aafaeeba1ca1
// Typelizer digest 649e6d69b5297dc030ed276c0617f83e
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaUser, AlbaPost} from '@/types'
Expand All @@ -8,9 +8,13 @@ type AlbaUserSerializerFoo = {
id?: number | null;
username: string;
active: boolean;
name: string;
/** This is sir name from the name */
sir_name: string;
invitor: AlbaUser;
posts: Array<AlbaPost>;
latest_post: AlbaPost;
first_name: string;
created_at: string;
}

Expand Down
12 changes: 11 additions & 1 deletion spec/app/app/serializers/alba/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Alba
class UserSerializer < BaseSerializer
typelize_from ::User
attributes :id, :username, :active
attributes :id, :username, :active, :name, :sir_name

has_one :invitor, resource: UserSerializer

Expand All @@ -10,6 +10,16 @@ class UserSerializer < BaseSerializer

typelize id: [:string, nullable: true]

typelize :string, comment: "This is sir name from the name"
def sir_name(object)
object.username.split(" ").last
end

typelize :string
attribute :first_name do |object|
object.username.split(" ").first
end

class FooSerializer < UserSerializer
typelize_from ::User
attributes :created_at
Expand Down
1 change: 1 addition & 0 deletions spec/app/db/migrate/20240707052900_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreateUsers < ActiveRecord::Migration[7.1]
def change
create_table :users do |t|
t.string :name, null: false
t.string :username, null: false
t.boolean :active, null: false, default: false
t.references :invitor, foreign_key: {to_table: "users"}
Expand Down
3 changes: 2 additions & 1 deletion spec/app/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81cee0d

Please sign in to comment.