<% end %>
</p>
+<fieldset class="collapsible collapsed">
+ <legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
+ <div id="options-content" style="display:none;">
+ <p>
+ <label><%= radio_button_tag 'attachments', '0', @search_attachments == '0' %> <%= l(:label_search_attachments_no) %></label>
+ <label><%= radio_button_tag 'attachments', '1', @search_attachments == '1' %> <%= l(:label_search_attachments_yes) %></label>
+ <label><%= radio_button_tag 'attachments', 'only', @search_attachments == 'only' %> <%= l(:label_search_attachments_only) %></label>
+ </p>
+ </div>
+</fieldset>
+<%= hidden_field_tag 'options', '', :id => 'show-options' %>
+
+</div>
<p><%= submit_tag l(:button_submit) %></p>
<% end %>
-</div>
<% if @results %>
<div id="search-results-counts">
$("#search-form").submit();
}
});
+
+$("#search-form").submit(function(){
+ $("#show-options").val($("#options-content").is(":visible") ? '1' : '0');
+});
+
+<% if params[:options] == '1' %>
+toggleFieldset($("#options-content"));
+<% end %>
<% end %>
# Should we search additional associations on this model ?
searchable_options[:search_custom_fields] = reflect_on_association(:custom_values).present?
+ searchable_options[:search_attachments] = reflect_on_association(:attachments).present?
searchable_options[:search_journals] = reflect_on_association(:journals).present?
send :include, Redmine::Acts::Searchable::InstanceMethods
# 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:
columns = searchable_options[:columns]
columns = columns[0..0] if options[:titles_only]
- token_clauses = columns.collect {|column| "(#{search_token_match_statement(column)})"}
- sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
- tokens_conditions = [sql, * (tokens.collect {|w| "%#{w}%"} * token_clauses.size).sort]
+ r = []
+ queries = 0
- r = fetch_ranks_and_ids(search_scope(user, projects).where(tokens_conditions), options[:limit])
- sort_and_limit_results = false
-
- if !options[:titles_only] && searchable_options[:search_custom_fields]
- searchable_custom_fields = CustomField.where(:type => "#{self.name}CustomField", :searchable => true).to_a
+ unless options[:attachments] == 'only'
+ r = fetch_ranks_and_ids(
+ search_scope(user, projects).
+ where(search_tokens_condition(columns, tokens, options[:all_words])),
+ options[:limit]
+ )
+ queries += 1
- if searchable_custom_fields.any?
- fields_by_visibility = searchable_custom_fields.group_by {|field|
- field.visibility_by_project_condition(searchable_options[: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 ')
+ if !options[:titles_only] && searchable_options[: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(searchable_options[: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 ')
- sql = ([search_token_match_statement("#{CustomValue.table_name}.value")] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
- tokens_conditions = [sql, * tokens.collect {|w| "%#{w}%"}.sort]
+ r |= fetch_ranks_and_ids(
+ search_scope(user, projects).
+ joins(:custom_values).
+ where(visibility).
+ where(search_tokens_condition(["#{CustomValue.table_name}.value"], tokens, options[:all_words])),
+ options[:limit]
+ )
+ queries += 1
+ end
+ end
+ if !options[:titles_only] && searchable_options[:search_journals]
r |= fetch_ranks_and_ids(
search_scope(user, projects).
- joins(:custom_values).
- where(visibility).
- where(tokens_conditions),
+ 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, options[:all_words])),
options[:limit]
)
-
- sort_and_limit_results = true
+ queries += 1
end
end
- if !options[:titles_only] && searchable_options[:search_journals]
- sql = ([search_token_match_statement("#{Journal.table_name}.notes")] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
- tokens_conditions = [sql, * tokens.collect {|w| "%#{w}%"}.sort]
-
+ if searchable_options[:search_attachments] && (options[:titles_only] ? options[:attachments] == 'only' : options[:attachments] != '0')
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(tokens_conditions),
+ joins(:attachments).
+ where(search_tokens_condition(["#{Attachment.table_name}.filename", "#{Attachment.table_name}.description"], tokens, options[:all_words])),
options[:limit]
)
-
- sort_and_limit_results = true
+ queries += 1
end
- if sort_and_limit_results
+ if queries > 1
r = r.sort.reverse
if options[:limit] && r.size > options[:limit]
r = r[0, options[:limit]]
r
end
+ def search_tokens_condition(columns, tokens, all_words)
+ token_clauses = columns.map {|column| "(#{search_token_match_statement(column)})"}
+ sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(all_words ? ' AND ' : ' OR ')
+ [sql, * (tokens.collect {|w| "%#{w}%"} * token_clauses.size).sort]
+ end
+ private :search_tokens_condition
+
def search_token_match_statement(column, value='?')
case connection.adapter_name
when /postgresql/i
assert results.include?(Issue.find(7))
end
+ def test_search_without_attachments
+ issue = Issue.generate! :subject => 'search_attachments'
+ attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
+
+ get :index, :id => 1, :q => 'search_attachments', :attachments => '0'
+ results = assigns(:results)
+ assert_equal 1, results.size
+ assert_equal issue, results.first
+ end
+
+ def test_search_attachments_only
+ issue = Issue.generate! :subject => 'search_attachments'
+ attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
+
+ get :index, :id => 1, :q => 'search_attachments', :attachments => 'only'
+ results = assigns(:results)
+ assert_equal 1, results.size
+ assert_equal attachment.container, results.first
+ end
+
+ def test_search_with_attachments
+ Issue.generate! :subject => 'search_attachments'
+ Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
+
+ get :index, :id => 1, :q => 'search_attachments', :attachments => '1'
+ results = assigns(:results)
+ assert_equal 2, results.size
+ end
+
def test_search_all_words
# 'all words' is on by default
get :index, :id => 1, :q => 'recipe updating saving', :all_words => '1'