• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / orangelight / 12d3996e-cc1b-4e7a-9b03-b44931e9d68d

15 Apr 2025 05:16PM UTC coverage: 95.334% (+0.07%) from 95.269%
12d3996e-cc1b-4e7a-9b03-b44931e9d68d

push

circleci

web-flow
Refactor guided_search_fields into ViewComponent (#4873)

* Refactor guided_search_fields into ViewComponent

This allows us to follow new BL patterns, put helper code and html closer together, opens possibility of contributing guided search back to BL

Closes #4872

Clean up some Reek complaints, ignore others

* Remove flaky test

* Remove unneeded test helper

40 of 41 new or added lines in 1 file covered. (97.56%)

6028 of 6323 relevant lines covered (95.33%)

1522.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/app/components/orangelight/guided_search_fields_component.rb
1
# frozen_string_literal: true
2

3
# Fill in default from existing search, if present
4
# -- if you are using same search fields for basic
5
# search and advanced, will even fill in properly if existing
6
# search used basic search on same field present in advanced.
7
class Orangelight::GuidedSearchFieldsComponent < Blacklight::Component
3✔
8
  def label_tag_default_for(key)
3✔
9
    params_key = params[key]
210✔
10
    if params_key.present?
210✔
NEW
11
      params_key
×
12
    elsif params['search_field'] == key || guided_context(key)
210✔
13
      params['q']
6✔
14
    else
15
      param_for_field key
204✔
16
    end
17
  end
18

19
  # :reek:FeatureEnvy
20
  def advanced_key_value
3✔
21
    advanced_search_fields.map do |field|
208✔
22
      [field[1][:label], field[0]]
2,080✔
23
    end
24
  end
25

26
  # carries over original search field and original guided search fields if user switches to guided search from regular search
27
  def guided_field(field_num, default_val)
3✔
28
    search_field = params[:search_field]
208✔
29
    search_field_for_advanced_search = advanced_search_fields[search_field]
208✔
30
    return search_field_for_advanced_search.key || default_val if first_search_field_selector?(field_num) && no_advanced_search_fields_specified? && search_field && search_field_for_advanced_search
208✔
31
    params[field_num] || param_for_field(field_num) || default_val
204✔
32
  end
33

34
  # carries over original search query if user switches to guided search from regular search
35
  def guided_context(key)
3✔
36
    search_field = params[:search_field]
210✔
37
    first_search_field?(key) &&
210✔
38
      search_field && advanced_search_fields[search_field]
39
  end
40

41
  # carries over guided search operations if user switches back to guided search from regular search
42
  def guided_radio(op_num, operator)
3✔
43
    op_num_from_params = params[op_num]
414✔
44
    if op_num_from_params
414✔
45
      op_num_from_params == operator
24✔
46
    else
47
      operator == 'AND'
390✔
48
    end
49
  end
50

51
  # private
52

53
  # :reek:NilCheck
54
  def advanced_search_fields
3✔
55
    blacklight_config.search_fields.select do |_key, value|
423✔
56
      include_in_advanced_search = value.include_in_advanced_search
7,191✔
57
      include_in_advanced_search || include_in_advanced_search.nil?
7,191✔
58
    end
59
  end
60

61
  # :reek:UtilityFunction
62
  def first_search_field_selector?(key)
3✔
63
    %i[clause_0_field].include? key
208✔
64
  end
65

66
  def no_advanced_search_fields_specified?
3✔
67
    %i[clause_0_field clause_1_field clause_2_field].none? do |key|
70✔
68
      params[key].present?
210✔
69
    end
70
  end
71

72
  # :reek:FeatureEnvy
73
  def param_for_field(field_identifier)
3✔
74
    field_identifier_string = field_identifier.to_s
408✔
75
    return unless field_identifier_string.start_with? 'clause'
408✔
76
    components = field_identifier_string.split('_')
408✔
77
    params.dig(*components)
408✔
78
  end
79

80
  # :reek:UtilityFunction
81
  def first_search_field?(key)
3✔
82
    %i[q1 clause_0_query].include? key
210✔
83
  end
84

85
  delegate :blacklight_config, to: :helpers
3✔
86
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc