Class: Backend::DashboardsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/dashboards_controller.rb

Constant Summary collapse

SIMILAR_LETTERS =
[
  %w(C Ç),
  %w(A Á À Â Ä Ǎ Ă Ā Ã Å),
  %w(Æ Ǽ Ǣ),
  %w(E É È Ė Ê Ë Ě Ĕ Ē),
  %w(I Í Ì İ Î Ï Ǐ Ĭ Ī Ĩ),
  %w(O Ó Ò Ô Ö Ǒ Ŏ Ō Õ Ő),
  %w(U Ú Ù Û Ü Ǔ Ŭ Ū Ũ Ű Ů),
  %w(Y Ý  Ŷ Ÿ Ȳ ),
  %w(c ç),
  %w(a á à â ä ǎ ă ā ã å),
  %w(æ ǽ ǣ),
  %w(e é è ė ê ë ě ĕ ē),
  %w(i í ì i î ï ǐ ĭ ī ĩ),
  %w(o ó ò ô ö ǒ ŏ ō õ ő),
  %w(u ú ù û ü ǔ ŭ ū ũ ű ů),
  %w(ý  ŷ ÿ ȳ )
].freeze

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Instance Method Details

#homeObject


39
40
# File 'app/controllers/backend/dashboards_controller.rb', line 39

def home
end

#sandboxObject


42
43
# File 'app/controllers/backend/dashboards_controller.rb', line 42

def sandbox
end

#searchObject

Global search method is put there for now waiting for a better place This action permits to search across all the main data of the application TODO: Clean this!!!


67
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
# File 'app/controllers/backend/dashboards_controller.rb', line 67

def search
  per_page = 10
  page = params[:page].to_i
  page = 1 if page.zero?
  # Create filter
  query = params[:q].to_s.gsub(/[\'\"\(\)\[\]\=\-\|\{\}]+/, ' ').strip.gsub(/[[:space:]]+/, ' ')
  words = query.split(/\s+/)
  SIMILAR_LETTERS.each do |group|
    exp = '(' + group.join('|') + ')'
    query.gsub!(Regexp.new(exp), exp)
  end
  regexps = query.split(/\s+/)

  pertinence = '1'
  if regexps.any?
    # max is the maximal points count that can be obtained for a key word
    # here it's equivalent to find 5 times the sole word.
    max = 4 * 5
    pertinence = 'ROUND(100.0 * CAST((' + regexps.collect do |regexp|
      points = [regexp, "#{regexp}\\\\M", "#{regexp}\\\\M", "\\\\M#{regexp}\\\\M"].collect do |exp|
        # Count occurrences
        "ARRAY_LENGTH(REGEXP_SPLIT_TO_ARRAY(indexer, E'#{exp}', 'i'), 1)-1"
      end.join('+')
      "(CASE WHEN (#{points}) > #{max} THEN #{max} ELSE (#{points}) END)"
    end.join(' * ') + ") AS FLOAT)/#{max**regexps.count}.0)"
  end

  filtered = 'SELECT record_id, record_type, title, indexer, (' + pertinence + ") AS pertinence FROM (#{@@centralizing_query}) AS centralizer GROUP BY record_type, record_id, title, indexer"

  filter = " FROM (#{filtered}) AS filtered"
  filter << ' WHERE filtered.pertinence > 0'

  @search = {}

  # Count results
  query = "SELECT count(filtered.record_id) AS total_count #{filter}"
  @search[:count] = Ekylibre::Record::Base.connection.select_value(query).to_i
  @search[:last_page] = (@search[:count].to_f / per_page).ceil

  # Select results
  query = "SELECT record_id, record_type, title, indexer, pertinence #{filter}"
  query << ' ORDER BY filtered.pertinence DESC, title'
  query << " LIMIT #{per_page}"
  query << " OFFSET #{per_page * (page - 1)}"
  @search[:records] = Ekylibre::Record::Base.connection.select_all(query)

  if @search[:count] == 1
    record = @search[:records].first
    redirect_to controller: record['record_type'].tableize, action: :show, id: record['record_id'].to_i, q: params[:q]
    return
  end

  # @search[:query] = query

  @search[:words] = words

  if @search[:count].zero? && page > 1
    redirect_to(action: :search, q: params[:q], page: 1)
  end
  params[:page] = page
  t3e searched: params[:q]
end