-
Notifications
You must be signed in to change notification settings - Fork 24
Filters by remote_availibity, location, position_type in query object #39
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
Changes from all commits
cce6147
63708ca
6e795d1
77d8420
3bf4ac2
2f13e85
ec683c3
7963877
df683d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ | |
| .ruby-version | ||
| .env.dev | ||
| .env.development | ||
|
|
||
| .direnv/ | ||
| .envrc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,8 @@ class Index | |
|
|
||
| include Import[ | ||
| 'libs.search_query_parser', | ||
| operation: 'vacancies.operations.list' | ||
| operation: 'vacancies.operations.list', | ||
| search_options_mapper: 'vacancies.mappers.search_options', | ||
| ] | ||
|
|
||
| EMPTY_SEARCH_QUERY = {}.freeze | ||
|
|
@@ -35,7 +36,9 @@ def call(params) | |
| private | ||
|
|
||
| def search_query | ||
| params[:query] ? search_query_parser.call(params[:query]) : EMPTY_SEARCH_QUERY | ||
| query_attributes = params[:query] ? search_query_parser.call(params[:query]) : EMPTY_SEARCH_QUERY | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а еще лучше, я бы сделал так, что квери парсер возвращал бы сразу что надо, вне зависимости от того, что туда передать, что-то в таком духе: query_attributes = search_query_parser.call(params[:query])
query_attributes # => attributes or initial_attributes |
||
| initial_attributes = { remote: nil, position_type: nil, location: nil } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. я бы в константу это вынес |
||
| search_options_mapper.call(initial_attributes.merge(query_attributes)) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,18 +8,35 @@ def initialize(repo = VacancyRepository.new) | |
| @repo = repo | ||
| end | ||
|
|
||
| def all_with_contact(limit:, page:) | ||
| all_with_contact_relation(limit: limit, page: page).to_a | ||
| def all_with_contact(limit:, page:, search_query:) | ||
| all_with_contact_relation(limit: limit, page: page, search_query: search_query).to_a | ||
| end | ||
|
|
||
| def pager_for_all_with_contact(limit:, page:) | ||
| def pager_for_all_with_contact(limit:, page:, search_query:) | ||
| Hanami::Pagination::Pager.new( | ||
| all_with_contact_relation(limit: limit, page: page).pager | ||
| all_with_contact_relation(limit: limit, page: page, search_query: search_query).pager | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| QUERY_MODIFIERS = { | ||
| remote: ->(query, filter_value) { query.where(remote_available: filter_value) }, | ||
| position_type: ->(query, filter_value) { query.where(position_type: filter_value) }, | ||
| location: ->(query, filter_value) { query.where { location.ilike("%#{filter_value}%") } } | ||
| }.freeze | ||
|
|
||
| def new_all_with_contact_relation(limit:, page:, search_query:) | ||
| query = repo.aggregate(:contact) | ||
| .where(published: true, archived: false, deleted_at: nil) | ||
| search_query.to_h.each do |key, value| | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. реально оптимизировать этот кусок? что бы при
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не совсем понял вопрос There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. может быть: if search_query
...
end
search_query.to_h.each do |key, value| |
||
| modifier = QUERY_MODIFIERS[key] | ||
| query = modifier.call(query, value) if modifier && value | ||
| end | ||
| query.map_to(::Vacancy).order { created_at.desc } | ||
| .per_page(limit).page(page || 1) | ||
| end | ||
|
|
||
| def all_with_contact_relation(limit:, page:) | ||
| repo.aggregate(:contact) | ||
| .where(published: true, deleted_at: nil) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Vacancies | ||
| module Entities | ||
| class SearchOptions < Dry::Struct | ||
| attribute :remote, Core::Types::Strict::Bool.optional.default(nil) | ||
| attribute :position_type, Core::Types::VacancyPositionTypes.optional.default(nil) | ||
| attribute :location, Core::Types::Strict::String.optional.default(nil) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Vacancies | ||
| module Mappers | ||
| Container = Module.new do | ||
| extend Transproc::Registry | ||
| import Transproc::HashTransformations | ||
| import Transproc::Coercions | ||
| import Transproc::ClassTransformations | ||
| end | ||
|
|
||
| class SearchOptions < Transproc::Transformer[Container] | ||
| symbolize_keys | ||
| map_value :remote, t(:to_boolean) | ||
| constructor_inject ::Vacancies::Entities::SearchOptions | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Vacancies::Mappers::SearchOptions, type: :mapper do | ||
| subject { described_class.new.call(search_options_hash) } | ||
|
|
||
| let(:search_options_hash) { { remote: nil, position_type: 'other', location: 'New Vasyuki' } } | ||
|
|
||
| it { expect(subject).to be_a(Vacancies::Entities::SearchOptions) } | ||
| it { expect(subject.position_type).to eq(search_options_hash[:position_type]) } | ||
| it { expect(subject.location).to eq(search_options_hash[:location]) } | ||
|
|
||
| context 'when remote is string equaled "true"' do | ||
| let(:search_options_hash) { { remote: 'true', position_type: nil, location: nil } } | ||
|
|
||
| it { expect(subject.remote).to be_truthy } | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не принципиально, но тут лишняя запятая