Skip to content

Commit

Permalink
use query param
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGorbanev committed Jun 25, 2019
1 parent f2e7cb0 commit 2035247
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
10 changes: 3 additions & 7 deletions apps/web/controllers/vacancies/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Index

expose :vacancies
expose :pager
expose :search_query

params do
optional(:page).filled
Expand All @@ -24,20 +25,15 @@ class Index
end

def call(params)
result = operation.call(search_query: search_query, page: params[:page], remote_query: params[:remote])
@search_query = params[:query] ? search_query_parser.call(params[:query]) : EMPTY_SEARCH_QUERY
result = operation.call(search_query: @search_query, page: params[:page])

case result
when Success
@pager = result.value![:pager]
@vacancies = result.value![:result]
end
end

private

def search_query
params[:query] ? search_query_parser.call(params[:query]) : EMPTY_SEARCH_QUERY
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions apps/web/views/vacancies/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def remote_filter_button(text:, remote_value:)
private

def current_remote_query
params[:remote] if params[:remote] == 'true' || params[:remote] == 'false'
search_query[:remote] if search_query[:remote] == 'true' || search_query[:remote] == 'false'
end

def remote_filter_button_href(remote_value)
routes.root_path(remote_value.nil? ? {} : { remote: remote_value })
routes.root_path(remote_value.nil? ? {} : { query: "remote:#{remote_value}" })
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/vacancies/operations/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class List < ::Libs::Operation

PAGINATION_LIMIT = 10

def call(search_query: {}, page: 1, remote_query: nil) # rubocop:disable Lint/UnusedMethodArgument
remote_available = { 'true' => true, 'false' => false }[remote_query]
def call(search_query: {}, page: 1) # rubocop:disable Lint/UnusedMethodArgument
remote_available = { 'true' => true, 'false' => false }[search_query[:remote]]
pager = vacancy_query.pager_for_all_with_contact(limit: PAGINATION_LIMIT,
page: page || 1,
remote_available: remote_available)
Expand Down
2 changes: 1 addition & 1 deletion spec/vacancies/operations/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

context 'with remote query' do
subject { operation.call(remote_query: remote_query) }
subject { operation.call(search_query: { remote: remote_query }) }

let(:vacancies) { [] }

Expand Down
8 changes: 2 additions & 6 deletions spec/web/controllers/vacancies/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
end

context 'when params includes query param' do
let(:params) { { query: 'remote:true search text', remote: 'remote_query' } }
let(:params) { { query: 'remote:true search text' } }

it { expect(subject).to be_success }

it do
expect(operation).to receive(:call).with(
page: nil,
search_query: { remote: 'true', text: 'search text' },
remote_query: 'remote_query'
)
expect(operation).to receive(:call).with(page: nil, search_query: { remote: 'true', text: 'search text' })
subject
end
end
Expand Down
21 changes: 11 additions & 10 deletions spec/web/views/vacancies/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe Web::Views::Vacancies::Index, type: :view do
let(:params) { Hash[] }
let(:exposures) { Hash[format: :html, flash: {}, vacancies: [], params: params] }
let(:search_query) { Hash[] }
let(:exposures) { Hash[format: :html, flash: {}, params: params, vacancies: [], search_query: search_query] }
let(:template) { Hanami::View::Template.new('apps/web/templates/vacancies/index.html.slim') }
let(:view) { described_class.new(template, exposures) }
let(:rendered) { view.render }
Expand All @@ -20,49 +21,49 @@
expect(rendered).to include without_indentation <<~HTML
<div class="btn-group btn-group-toggle">
<a class="btn btn-light active"><span>Любые</span></a>
<a class="btn btn-light" href="/?remote=true"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?remote=false"><span>В офисе</span></a>
<a class="btn btn-light" href="/?query=remote%3Atrue"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?query=remote%3Afalse"><span>В офисе</span></a>
</div>
HTML
end

context 'when remote is true' do
let(:params) { Hash[remote: 'true'] }
let(:search_query) { Hash[remote: 'true'] }

it do
expect(rendered).to include without_indentation <<~HTML
<div class="btn-group btn-group-toggle">
<a class="btn btn-light" href="/"><span>Любые</span></a>
<a class="btn btn-light active"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?remote=false"><span>В офисе</span></a>
<a class="btn btn-light" href="/?query=remote%3Afalse"><span>В офисе</span></a>
</div>
HTML
end
end

context 'when remote is false' do
let(:params) { Hash[remote: 'false'] }
let(:search_query) { Hash[remote: 'false'] }

it do
expect(rendered).to include without_indentation <<~HTML
<div class="btn-group btn-group-toggle">
<a class="btn btn-light" href="/"><span>Любые</span></a>
<a class="btn btn-light" href="/?remote=true"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?query=remote%3Atrue"><span>Удаленные</span></a>
<a class="btn btn-light active"><span>В офисе</span></a>
</div>
HTML
end
end

context 'when remote is not valid' do
let(:params) { Hash[remote: 'foo'] }
let(:search_query) { Hash[remote: 'foo'] }

it do
expect(rendered).to include without_indentation <<~HTML
<div class="btn-group btn-group-toggle">
<a class="btn btn-light active"><span>Любые</span></a>
<a class="btn btn-light" href="/?remote=true"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?remote=false"><span>В офисе</span></a>
<a class="btn btn-light" href="/?query=remote%3Atrue"><span>Удаленные</span></a>
<a class="btn btn-light" href="/?query=remote%3Afalse"><span>В офисе</span></a>
</div>
HTML
end
Expand Down

0 comments on commit 2035247

Please sign in to comment.