22
33module Queries
44 class Vacancy
5+ EMPTY_HASH = { } . freeze
6+
57 attr_reader :repo
68
79 def initialize ( repo = VacancyRepository . new )
810 @repo = repo
911 end
1012
11- def all_with_contact ( limit :, page :, search_query :)
12- all_with_contact_relation ( limit : limit , page : page , search_query : search_query ) . to_a
13+ def all_with_contact ( limit :, page :, search_query : nil )
14+ all_with_contact_relation ( limit : limit , page : page , search_query : search_query || EMPTY_HASH ) . to_a
1315 end
1416
15- def pager_for_all_with_contact ( limit :, page :, search_query :)
17+ def pager_for_all_with_contact ( limit :, page :, search_query : nil )
1618 Hanami ::Pagination ::Pager . new (
17- all_with_contact_relation ( limit : limit , page : page , search_query : search_query ) . pager
19+ all_with_contact_relation ( limit : limit , page : page , search_query : search_query || EMPTY_HASH ) . pager
1820 )
1921 end
2022
@@ -26,23 +28,24 @@ def pager_for_all_with_contact(limit:, page:, search_query:)
2628 location : -> ( query , filter_value ) { query . where { location . ilike ( "%#{ filter_value } %" ) } }
2729 } . freeze
2830
29- def new_all_with_contact_relation ( limit :, page :, search_query :)
30- query = repo . aggregate ( :contact )
31- . where ( published : true , archived : false , deleted_at : nil )
31+ def all_with_contact_relation ( limit :, page :, search_query :)
32+ query = base_query
33+
3234 search_query . to_h . each do |key , value |
35+ next if value . nil?
36+
3337 modifier = QUERY_MODIFIERS [ key ]
34- query = modifier . call ( query , value ) if modifier && value
38+ query = modifier . call ( query , value ) if modifier
3539 end
36- query . map_to ( :: Vacancy ) . order { created_at . desc }
37- . per_page ( limit ) . page ( page || 1 )
40+
41+ query . per_page ( limit ) . page ( page || 1 )
3842 end
39-
40- def all_with_contact_relation ( limit : , page : )
43+
44+ def base_query
4145 repo . aggregate ( :contact )
42- . where ( published : true , deleted_at : nil )
46+ . where ( published : true , archived : false , deleted_at : nil )
4347 . where ( 'archived_at > ?' , Date . today )
4448 . map_to ( ::Vacancy ) . order { created_at . desc }
45- . per_page ( limit ) . page ( page || 1 )
4649 end
4750 end
4851end
0 commit comments