Class: Interpret::SearchController

Inherits:
BaseController show all
Defined in:
app/controllers/interpret/search_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/interpret/search_controller.rb', line 4

def index
  if request.post?
    opts = {}
    opts[:key] = params[:key] if params[:key].present?
    opts[:value] = params[:value] if params[:value].present?
    redirect_to search_url(opts)
  else
    if params[:key].present? || params[:value].present?
      sanitizer = case ActiveRecord::Base.connection.adapter_name
                  when "SQLite"
                    if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("1.9")
                      lambda {|x| "%#{x}%"}
                    else
                      lambda {|x| "%#{CGI.escape(x)}%"}
                    end
                  else
                    lambda {|x| "%#{CGI.escape(x)}%"}
                  end
      t = Interpret::Translation.arel_table
      search_key = params[:key].present? ? params[:key].split(" ").map{|x| sanitizer.call(x)} : []
      search_value = params[:value].present? ? params[:value].split(" ").map{|x| sanitizer.call(x)} : []
      if search_value.any? && search_key.any?
        @translations = Interpret::Translation.allowed.locale(I18n.locale).where(t[:key].matches_all(search_key).or(t[:value].matches_all(search_value))).order("translations.key ASC")
      elsif search_key.any?
        @translations = Interpret::Translation.allowed.locale(I18n.locale).where(t[:key].matches_all(search_key)).order("translations.key ASC")
      else
        @translations = Interpret::Translation.allowed.locale(I18n.locale).where(t[:value].matches_all(search_value)).order("translations.key ASC")
      end
    end
  end
end