Module: Blacklight::SearchContext

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
app/controllers/concerns/blacklight/search_context.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_to_search_history(search) ⇒ Object (protected)

Add a search to the in-session search history list



93
94
95
96
97
98
99
100
101
# File 'app/controllers/concerns/blacklight/search_context.rb', line 93

def add_to_search_history search
  session[:history] ||= []

  session[:history].unshift(search.id)

  if session[:history].length > blacklight_config.search_history_window
    session[:history] = session[:history].slice(0, blacklight_config.search_history_window )
  end
end

#agent_is_crawler?Boolean (protected)

Determine if the current request is coming from an anonymous bot or search crawler

Returns:

  • (Boolean)


74
75
76
77
78
# File 'app/controllers/concerns/blacklight/search_context.rb', line 74

def agent_is_crawler?
  crawler_proc = blacklight_config.crawler_detector
  return false if crawler_proc.nil? || current_user.present?
  crawler_proc.call(request)
end

#blacklisted_search_session_paramsObject (protected)

A list of query parameters that should not be persisted for a search



104
105
106
# File 'app/controllers/concerns/blacklight/search_context.rb', line 104

def blacklisted_search_session_params
  [:commit, :counter, :total, :search_id, :page, :per_page]
end

#current_search_sessionObject (protected)

The current search session



31
32
33
# File 'app/controllers/concerns/blacklight/search_context.rb', line 31

def current_search_session
  @current_search_session ||= find_search_session
end

#find_or_initialize_search_session_from_params(params) ⇒ Object (protected)



80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/concerns/blacklight/search_context.rb', line 80

def find_or_initialize_search_session_from_params params
  params_copy = params.reject { |k,v| blacklisted_search_session_params.include?(k.to_sym) or v.blank? }

  return if params_copy.reject { |k,v| [:action, :controller].include? k.to_sym }.blank?

  saved_search = searches_from_history.find { |x| x.query_params == params_copy }

  saved_search ||= Search.create(query_params: params_copy).tap do |s|
    add_to_search_history(s)
  end
end

#find_search_sessionObject (protected)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/blacklight/search_context.rb', line 40

def find_search_session
  if agent_is_crawler?
    nil
  elsif params[:search_context].present?
    find_or_initialize_search_session_from_params JSON.parse(params[:search_context])
  elsif params[:search_id].present?
    begin
      # TODO: check the search id signature.
      searches_from_history.find(params[:search_id])
    rescue ActiveRecord::RecordNotFound
      nil
    end
  elsif start_new_search_session?
    find_or_initialize_search_session_from_params search_state.to_h
  elsif search_session['id']
    begin
      searches_from_history.find(search_session['id'])
    rescue ActiveRecord::RecordNotFound
      nil
    end
  end
end

#search_sessionObject (protected)

sets up the session hash if it doesn't already exist



23
24
25
26
27
28
# File 'app/controllers/concerns/blacklight/search_context.rb', line 23

def search_session
  session[:search] ||= {}
  # Need to call the getter again. The value is mutated
  # https://github.com/rails/rails/issues/23884
  session[:search]
end

#set_current_search_sessionObject (protected)

Persist the current search session id to the user's session



36
37
38
# File 'app/controllers/concerns/blacklight/search_context.rb', line 36

def set_current_search_session
  search_session['id'] = current_search_session.id if current_search_session
end

#setup_next_and_previous_documentsObject (protected)

calls setup_previous_document then setup_next_document. used in the show action for single view pagination.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/blacklight/search_context.rb', line 110

def setup_next_and_previous_documents
  if search_session['counter'] and current_search_session
    index = search_session['counter'].to_i - 1
    response, documents = get_previous_and_next_documents_for_search index, search_state.reset(current_search_session.query_params).to_hash

    search_session['total'] = response.total
    @search_context_response = response
    @previous_document = documents.first
    @next_document = documents.last
  end
rescue Blacklight::Exceptions::InvalidRequest => e
  logger.warn "Unable to setup next and previous documents: #{e}"
end

#start_new_search_session?Boolean (protected)

If the current action should start a new search session, this should be set to true

Returns:

  • (Boolean)


66
67
68
# File 'app/controllers/concerns/blacklight/search_context.rb', line 66

def start_new_search_session?
  false
end