Skip to content

Commit

Permalink
Extract some view logic into the presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Dec 20, 2021
1 parent cf3d8ce commit a78c69f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% end %>

<!-- no results profile if missing is selected -->
<% if @facet_field.missing_selected? %>
<% unless @facet_field.missing_selected? %>
<!-- you can hide this if you want, but it has to be on page if you want
JS slider and calculated facets to show up, JS sniffs it. -->
<div class="profile">
Expand Down
23 changes: 21 additions & 2 deletions app/presenters/blacklight_range_limit/facet_field_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,23 @@ def selected_range
search_state.filter(key).values.first
end

def missing
stats_for_field.fetch('missing', 0)
def selected_range_facet_item
return unless selected_range

Blacklight::Solr::Response::Facets::FacetItem.new(value: selected_range, hits: response.total)
end

def missing_facet_item
return unless missing.positive?

Blacklight::Solr::Response::Facets::FacetItem.new(
value: Blacklight::SearchState::FilterField::MISSING,
hits: missing
)
end

def missing_selected?
selected_range == Blacklight::SearchState::FilterField::MISSING
end

def range_config
Expand All @@ -45,6 +60,10 @@ def range_config

private

def missing
stats_for_field.fetch('missing', 0)
end

def stats_for_field
response.dig('stats', 'stats_fields', facet_field.field) || {}
end
Expand Down
12 changes: 10 additions & 2 deletions spec/components/range_facet_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
in_modal?: false,
label: 'My facet field',
selected_range: nil,
selected_range_facet_item: nil,
missing_facet_item: nil,
missing_selected?: false,
min: nil,
max: nil,
search_state: Blacklight::SearchState.new({}, nil),
range_config: {},
missing: 0,
modal_path: nil,
facet_field: facet_config,
**facet_field_params
Expand Down Expand Up @@ -93,7 +95,13 @@
end

context 'with missing documents' do
let(:facet_field_params) { { missing: 50 } }
let(:facet_field_params) { { missing_facet_item: facet_item } }
let(:facet_item) do
Blacklight::Solr::Response::Facets::FacetItem.new(
value: Blacklight::SearchState::FilterField::MISSING,
hits: 50
)
end

it 'renders a facet value for the documents that are missing the field data' do
expected_facet_query_param = Regexp.new(Regexp.escape({ f: { '-key': ['[* TO *]'] } }.to_param))
Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/facet_field_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@
end
end

describe '#missing' do
describe '#missing_facet_item' do
let(:some_field_stats) do
{
missing: 5
}
end

it 'extracts the missing stat' do
expect(presenter.missing).to eq 5
expect(presenter.missing_facet_item.hits).to eq 5
end
end

Expand Down

0 comments on commit a78c69f

Please sign in to comment.