Skip to content

Commit 3c29ba9

Browse files
authored
Adding for_collection to the BaseSerializer. (#884)
* Adding for_collection to the BaseSerializer. This for_collection override takes a Lucky::Paginator argument as an option and changes the output to have extra pagination data added in. * Adding the pagination_serializer to the generated app. * Earthly snapshot updates.
1 parent ae18e4e commit 3c29ba9

File tree

11 files changed

+123
-0
lines changed

11 files changed

+123
-0
lines changed

fixtures/src_template/expected/src/serializers/base_serializer.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ abstract class BaseSerializer
66
new(object, *args, **named_args)
77
end
88
end
9+
10+
def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
11+
{
12+
"items" => collection.map do |object|
13+
new(object, *args, **named_args)
14+
end,
15+
"pagination" => PaginationSerializer.new(pages),
16+
}
17+
end
918
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is the default pagination serializer generated by Lucky.
2+
# Feel free to customize it in any way you like.
3+
class PaginationSerializer < BaseSerializer
4+
def initialize(@pages : Lucky::Paginator)
5+
end
6+
7+
def render
8+
{
9+
next_page: @pages.path_to_next,
10+
previous_page: @pages.path_to_previous,
11+
total_items: @pages.item_count,
12+
total_pages: @pages.total,
13+
}
14+
end
15+
end

fixtures/src_template__api_only/expected/src/serializers/base_serializer.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ abstract class BaseSerializer
66
new(object, *args, **named_args)
77
end
88
end
9+
10+
def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
11+
{
12+
"items" => collection.map do |object|
13+
new(object, *args, **named_args)
14+
end,
15+
"pagination" => PaginationSerializer.new(pages),
16+
}
17+
end
918
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is the default pagination serializer generated by Lucky.
2+
# Feel free to customize it in any way you like.
3+
class PaginationSerializer < BaseSerializer
4+
def initialize(@pages : Lucky::Paginator)
5+
end
6+
7+
def render
8+
{
9+
next_page: @pages.path_to_next,
10+
previous_page: @pages.path_to_previous,
11+
total_items: @pages.item_count,
12+
total_pages: @pages.total,
13+
}
14+
end
15+
end

fixtures/src_template__generate_auth/expected/src/serializers/base_serializer.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ abstract class BaseSerializer
66
new(object, *args, **named_args)
77
end
88
end
9+
10+
def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
11+
{
12+
"items" => collection.map do |object|
13+
new(object, *args, **named_args)
14+
end,
15+
"pagination" => PaginationSerializer.new(pages),
16+
}
17+
end
918
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is the default pagination serializer generated by Lucky.
2+
# Feel free to customize it in any way you like.
3+
class PaginationSerializer < BaseSerializer
4+
def initialize(@pages : Lucky::Paginator)
5+
end
6+
7+
def render
8+
{
9+
next_page: @pages.path_to_next,
10+
previous_page: @pages.path_to_previous,
11+
total_items: @pages.item_count,
12+
total_pages: @pages.total,
13+
}
14+
end
15+
end

fixtures/src_template__sec_tester/expected/src/serializers/base_serializer.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ abstract class BaseSerializer
66
new(object, *args, **named_args)
77
end
88
end
9+
10+
def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
11+
{
12+
"items" => collection.map do |object|
13+
new(object, *args, **named_args)
14+
end,
15+
"pagination" => PaginationSerializer.new(pages),
16+
}
17+
end
918
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is the default pagination serializer generated by Lucky.
2+
# Feel free to customize it in any way you like.
3+
class PaginationSerializer < BaseSerializer
4+
def initialize(@pages : Lucky::Paginator)
5+
end
6+
7+
def render
8+
{
9+
next_page: @pages.path_to_next,
10+
previous_page: @pages.path_to_previous,
11+
total_items: @pages.item_count,
12+
total_pages: @pages.total,
13+
}
14+
end
15+
end

src/lucky_cli/src_template.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ class SrcTemplate
219219
serializers_dir.add_file("error_serializer.cr") do |io|
220220
ECR.embed("#{__DIR__}/../web_app_skeleton/src/serializers/error_serializer.cr.ecr", io)
221221
end
222+
serializers_dir.add_file("pagination_serializer.cr") do |io|
223+
ECR.embed("#{__DIR__}/../web_app_skeleton/src/serializers/pagination_serializer.cr.ecr", io)
224+
end
222225
end
223226
end
224227
end

src/web_app_skeleton/src/serializers/base_serializer.cr.ecr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ abstract class BaseSerializer
66
new(object, *args, **named_args)
77
end
88
end
9+
10+
def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
11+
{
12+
"items" => collection.map do |object|
13+
new(object, *args, **named_args)
14+
end,
15+
"pagination" => PaginationSerializer.new(pages),
16+
}
17+
end
918
end

0 commit comments

Comments
 (0)