Module: Redmine::Acts::Searchable::InstanceMethods::ClassMethods
- Defined in:
- lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
Instance Method Summary collapse
-
#search_result_ranks_and_ids(tokens, user = User.current, projects = nil, options = {}) ⇒ Object
Searches the model for the given tokens and user visibility.
-
#search_results(*args) ⇒ Object
Returns search results with same arguments as search_result_ranks_and_ids.
-
#search_results_from_ids(ids) ⇒ Object
Returns search results of given ids.
Instance Method Details
#search_result_ranks_and_ids(tokens, user = User.current, projects = nil, options = {}) ⇒ Object
Searches the model for the given tokens and user visibility. The projects argument can be either nil (will search all projects), a project or an array of projects. Returns an array that contains the rank and id of all results. In current implementation, the rank is the record timestamp converted as an integer.
Valid options:
-
:titles_only - searches tokens in the first searchable column only
-
:all_words - searches results that match all token
-
:
-
:limit - maximum number of results to return
Example:
Issue.search_result_ranks_and_ids("foo")
# => [[1419595329, 69], [1419595622, 123]]
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 147 148 149 150 151 152 153 |
# File 'lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 84 def search_result_ranks_and_ids(tokens, user=User.current, projects=nil, ={}) tokens = [] << tokens unless tokens.is_a?(Array) projects = [] << projects if projects.is_a?(Project) columns = [:columns] columns = columns[0..0] if [:titles_only] r = [] queries = 0 unless [:attachments] == 'only' r = fetch_ranks_and_ids( search_scope(user, projects, ). where(search_tokens_condition(columns, tokens, [:all_words])), [:limit] ) queries += 1 if ![:titles_only] && [:search_custom_fields] searchable_custom_fields = CustomField.where(:type => "#{self.name}CustomField", :searchable => true).to_a if searchable_custom_fields.any? fields_by_visibility = searchable_custom_fields.group_by {|field| field.visibility_by_project_condition([:project_key], user, "#{CustomValue.table_name}.custom_field_id") } clauses = [] fields_by_visibility.each do |visibility, fields| clauses << "(#{CustomValue.table_name}.custom_field_id IN (#{fields.map(&:id).join(',')}) AND (#{visibility}))" end visibility = clauses.join(' OR ') r |= fetch_ranks_and_ids( search_scope(user, projects, ). joins(:custom_values). where(visibility). where(search_tokens_condition(["#{CustomValue.table_name}.value"], tokens, [:all_words])), [:limit] ) queries += 1 end end if ![:titles_only] && [:search_journals] r |= fetch_ranks_and_ids( search_scope(user, projects, ). joins(:journals). where("#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes)})", false). where(search_tokens_condition(["#{Journal.table_name}.notes"], tokens, [:all_words])), [:limit] ) queries += 1 end end if [:search_attachments] && ([:titles_only] ? [:attachments] == 'only' : [:attachments] != '0') r |= fetch_ranks_and_ids( search_scope(user, projects, ). joins(:attachments). where(search_tokens_condition(["#{Attachment.table_name}.filename", "#{Attachment.table_name}.description"], tokens, [:all_words])), [:limit] ) queries += 1 end if queries > 1 r = r.sort.reverse if [:limit] && r.size > [:limit] r = r[0, [:limit]] end end r end |
#search_results(*args) ⇒ Object
Returns search results with same arguments as search_result_ranks_and_ids
210 211 212 213 |
# File 'lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 210 def search_results(*args) ranks_and_ids = search_result_ranks_and_ids(*args) search_results_from_ids(ranks_and_ids.map(&:last)) end |
#search_results_from_ids(ids) ⇒ Object
Returns search results of given ids
205 206 207 |
# File 'lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 205 def search_results_from_ids(ids) where(:id => ids).preload([:preload]).to_a end |