Open davydovanton opened 6 years ago
After small talk with Luca we decided to use #map_to
method from ROM relation.
Something like:
post.where(published: true).map_to(Paginated(Post)).to_a
@davydovanton how did you use the #map_to
method to solve the problem?
Are you still using the all_for_page
helper with this or have you replaced it?
hey, we can easily create a method which will add pagination attributes to entity Paginated(Post)
. Like we have with Types::Collection(Project)
type in entity's attributes
block.
Now I don't use pagination in my projects that's why all_for_page
still valid
@davydovanton thank you for your reply.
I ended up rewriting all_for_page
so it expects entities
and a pager
back from the repository
def all_for_page(relation, conditions: {})
relation.all_with_pager_by(conditions.merge(per_page: limit, page: (params[:page] || 1)))
end
And in the repository
def all_with_pager_by(conditions)
per_page = conditions.delete(:per_page) || 10
page = conditions.delete(:page) || 1
relation = composite.where(conditions).per_page(per_page).page(page)
[relation.to_a, Hanami::Pagination::Pager.new(relation.pager)]
end
This allowed me to use DI with the controller with the repository, and also had the nicety of not returning a relation to the controller
We have a problem now.
The first case
For example, we have the action like this one:
And we want to test a action with using DI:
Now we have a problem, because
#all_for_page
method does some relation logic in action. And in this case we have a trouble with usingdouble
object with DI.The second case
For example, we have the action like this one:
In this case we need to return relation from interactor object and it's look like a problem. Because action works with relation instead of data.