Class: SearchResultsController

Inherits:
ApplicationController show all
Includes:
DatasetInitialization
Defined in:
app/controllers/search_results_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DatasetInitialization

#datasets_as_json, #init_datasets

Class Method Details

.prepare_basic_variables(controller) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/search_results_controller.rb', line 148

def self.prepare_basic_variables(controller)
  langs = Iqvoc.all_languages.each_with_object({}) do |lang, hsh|
    lang ||= 'none'
    hsh[lang] = I18n.t("languages.#{lang}", default: lang)
  end
  controller.instance_variable_set(:@available_languages, langs)

  # AR requires mapping to existing object attributes so that value is mapped to the unused status attribute to keep the information
  collections = Iqvoc::Collection.base_class.joins(:pref_labels).order('value').select('value AS status, concepts.origin')
  controller.instance_variable_set(:@collections, collections)

  # default search params
  controller.params['t'] = Iqvoc.searchable_class_names.values.first if controller.params['t'].nil?
  controller.params['qt'] = 'contains' if controller.params['qt'].nil?
  controller.params['for'] = 'all' if controller.params['for'].nil?
  controller.params['l'] = langs.keys if controller.params['l'].nil?
  controller.params['include_expired'] = (controller.params['include_expired'] == "true")
end

Instance Method Details

#indexObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/search_results_controller.rb', line 68

def index
  authorize! :read, Concept::Base
  # TODO: requires a dedicated :search permission because this covers more
  # than just concepts

  self.class.prepare_basic_variables(self)

  # Map short params to their log representation
  { t: :type,
    q: :query,
    l: :languages,
    qt: :query_type,
    c: :collection_origin,
    ds: :datasets }.each do |short, long|
    params[long] ||= params[short]
  end

  # Delete parameters which should not be included into generated urls (e.g.
  # in rdf views)
  request.query_parameters.delete('commit')
  request.query_parameters.delete('utf8')

  @datasets = init_datasets

  @remote_result_collections = []

  if params[:query]
    # Deal with language parameter patterns
    languages = []
    # Either "l[]=de&l[]=en" as well as "l=de,en" should be possible
    if params[:languages].respond_to?(:each) && params[:languages].include?('none')
      # Special treatment for the "nil language"
      languages << nil
    elsif params[:languages].respond_to?(:split)
      languages = params[:languages].split(',')
    end

    # Ensure a valid class was selected
    unless klass = Iqvoc.searchable_class_names.detect { |key, value| value == params[:type] }.try(:first)
      raise "'#{params[:type]}' is not a searchable class! Must be one of " + Iqvoc.searchable_class_names.keys.join(', ')
    end
    klass = klass.constantize

    @results = klass.single_query(params.merge({ languages: languages.flatten }))
                    .filter { |search_result| result_allowed?(search_result) }

    if params[:limit] && Iqvoc.unlimited_search_results
      @results = @results.per(params[:limit].to_i)
    end

    if params[:datasets] && datasets = @datasets.select { |a| params[:datasets].include?(a.name) }
      @results = SearchResultCollection.new(@results)
      datasets.each do |dataset|
        results = dataset.search(params)
        if results
          @results = @results + results
        else
          flash.now[:error] ||= []
          flash.now[:error] << t('txt.controllers.search_results.remote_source_error', source: dataset)
        end
      end
      @results = @results.sort { |x, y| x.to_s <=> y.to_s }
    end

    @results = Kaminari.paginate_array(@results)
    @results = @results.page(params[:page])

    respond_to do |format|
      format.html {
        if request.headers['Accept'] == 'text/html; fragment=true'
          render template: 'search_results/_result_list', layout: false
        else
          render :index, layout: with_layout?
        end
      }
      format.any(:ttl, :rdf, :nt)
    end
  end
end