From 95322521d995bebb305b01136e65dc0af14026b4 Mon Sep 17 00:00:00 2001 From: Kieran Hayes Date: Wed, 16 Nov 2016 10:41:56 +0100 Subject: [PATCH] Redsun 2.0 --- Gemfile | 9 +- README.md | 4 +- app/controllers/redsun_search_controller.rb | 90 +-- app/helpers/redsun_search_helper.rb | 26 +- .../redsun_search/_class_name_facet.html.erb | 9 +- app/views/redsun_search/_date_facet.html.erb | 56 +- app/views/redsun_search/_facet.html.erb | 55 +- app/views/redsun_search/_results.html.erb | 26 +- app/views/redsun_search/index.html.erb | 277 +++++--- .../results/_attachment_result.html.erb | 11 + .../results/_issue_result.html.erb | 11 +- .../results/_journal_result.html.erb | 11 +- .../results/_wiki_page_result.html.erb | 2 +- app/views/settings/_redsun_settings.html.erb | 6 + config/locales/de.yml | 25 +- config/locales/en.yml | 43 +- config/schema.xml | 277 ++++++++ config/solrconfig.xml | 657 ++++++++++++++++++ init.rb | 12 +- lib/redmine_redsun/attachment_patch.rb | 92 +++ lib/redmine_redsun/journal_patch.rb | 7 + lib/tasks/redsun.rake | 8 +- 22 files changed, 1490 insertions(+), 224 deletions(-) create mode 100644 app/views/redsun_search/results/_attachment_result.html.erb create mode 100644 config/schema.xml create mode 100644 config/solrconfig.xml create mode 100644 lib/redmine_redsun/attachment_patch.rb diff --git a/Gemfile b/Gemfile index 5d85500..fd64a31 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,10 @@ -gem 'sunspot_rails', '2.2.2' -gem 'rsolr', '~> 1.0.7' -gem 'will_paginate', '3.0.7' +gem 'sunspot_rails', '2.2.6' +gem 'will_paginate', '~> 3.1.3' group :development do gem 'sunspot_solr', '2.2.0' end -group :integration_develop, :testing_develop, :development do - gem 'progress_bar', '1.0.3' +group :development do + gem 'progress_bar', '~> 1.0.3' end diff --git a/README.md b/README.md index 624053c..17b59b1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -redsun +Redsun ====== Red Sun - Apache Solr Search Integration for Redmine based on Sunspot + +Read more about it here: http://dkd.github.io/redsun/ \ No newline at end of file diff --git a/app/controllers/redsun_search_controller.rb b/app/controllers/redsun_search_controller.rb index 92c084d..a7db017 100644 --- a/app/controllers/redsun_search_controller.rb +++ b/app/controllers/redsun_search_controller.rb @@ -2,8 +2,8 @@ class RedsunSearchController < ApplicationController unloadable - before_filter :find_optional_project - before_filter :set_search_form + before_action :find_optional_project + before_action :set_search_form def index searchstring = params[:search_form][:searchstring] || '' @@ -18,13 +18,13 @@ def index allowed_issues = [] allowed_wikis = [] - if @project && @scope == 'project' + if @project && search_scope == 'project' params[:search_form][:project_id] = @project.id projects = @project.self_and_descendants.all allowed_projects << projects.collect { |p| p.id if User.current.allowed_to?(:view_project, p) }.compact allowed_issues << projects.collect { |project| project .id if User.current.allowed_to?(:view_issues, project) }.compact allowed_wikis << projects.collect { |project| project.id if User.current.allowed_to?(:view_wiki_pages, @project) }.compact - elsif @scope == 'my_projects' + elsif search_scope == 'my_projects' projects = User.current.memberships.collect(&:project).compact.uniq allowed_projects << projects.collect(&:id) allowed_issues << projects.collect { |project| project.id if User.current.allowed_to?(:view_issues, project) }.compact @@ -41,10 +41,7 @@ def index allowed_issues = allowed_issues.push(0) allowed_wikis = allowed_wikis.push(0) - sort_order = @sort_order - sort_field = @sort_field - - @search = Sunspot.search([Project, Issue, WikiPage, Journal]) do + @search = Sunspot.search([Project, Issue, WikiPage, Journal, Attachment]) do fulltext searchstring do highlight :description highlight :comments @@ -54,6 +51,7 @@ def index highlight :notes highlight :id highlight :title + highlight :filename end any_of do @@ -78,26 +76,35 @@ def index with(:journalized_type, 'Issue') with(:is_private, false) end + + all_of do + with :class, Attachment + with(:project_id).any_of allowed_projects.flatten + end + end %w(author_id project_name assigned_to_id + priority_id + tracker_id status_id - tracker_id).each do |easy_facet| + filetype + category_id).each do |easy_facet| facet easy_facet, minimum_count: 1 if params.key?(:search_form) && params[:search_form][easy_facet].present? with(easy_facet, params[:search_form][easy_facet]) end end - %w(priority_id - class_name).each do |easy_facet| - facet easy_facet, minimum_count: 1 - if params.key?(:search_form) && params[:search_form][easy_facet.to_sym].present? - with(easy_facet, params[:search_form][easy_facet.to_sym]) - end - end + # class filter + class_filter = if params[:search_form].key?(:class_name) + with(:class_name).any_of(params[:search_form][:class_name]) + else + all_of {} # not necessary for searching, but object is needed to set exclude option + end + facet :class_name, minimum_count: 1, exclude: class_filter # exclude option gives access to full list of items %w(created_on updated_on).each do |date_facet| if params[:search_form].present? && params[:search_form][date_facet].present? @@ -133,6 +140,7 @@ def index order_by(sort_field.to_sym, sort_order.downcase.to_sym) order_by(:score, :desc) + spellcheck :count => 3 end @searchstring = searchstring @@ -146,13 +154,6 @@ def index def set_search_form params[:search_form] = {} unless params[:search_form].present? - - if params[:project_id].present? - @project = Project.find(params[:project_id]) - elsif params[:search_form][:project_id].present? - @project = Project.find(params[:search_form][:project_id]) - end - # Reset facets if search button is pressed if params[:commit].present? [:author_id, @@ -162,26 +163,17 @@ def set_search_form :created_on, :updated_on, :class_name, - :active].each do |facet| + :active, + :project_name].each do |facet| params[:search_form].delete(facet) if params[:search_form][facet].present? end end - - if params[:search_form].present? && Issue::SORT_FIELDS.include?(params[:search_form][:sort_field]) - @sort_field = params[:search_form][:sort_field] - else - @sort_field = 'score' - end - - if params[:search_form].present? && Issue::SORT_ORDER.collect(&:first).include?(params[:search_form][:sort_order]) - @sort_order = params[:search_form][:sort_order] - else - @sort_order = 'DESC' - end - @scope = params[:search_form][:scope] || 'all_projects' + params[:search_form][:class_name] ||= [] + @sort_field = sort_field + @sort_order = sort_order @scope_selector = [[l(:label_my_projects), 'my_projects'], [l(:label_project_all), 'all_projects']] - @redsun_path = @project.present? ? redsun_project_search_path(project_id: @project.id) : redsun_search_path + @redsun_path = @project.present? ? redsun_project_search_path(@project) : redsun_search_path @scope_selector.push([l(:label_and_its_subprojects, @project.name), 'project']) if @project end @@ -197,9 +189,27 @@ def date_conditions private + def search_scope + params[:search_form][:scope] || 'all_projects' + end + + def sort_order + return params[:search_form][:sort_order] if Issue::SORT_ORDER.collect(&:first).include?(params[:search_form][:sort_order]) + 'DESC' + end + + def sort_field + return params[:search_form][:sort_field] if Issue::SORT_FIELDS.include?(params[:search_form][:sort_field]) + 'score' + end + def find_optional_project - return true unless params[:id] - @project = Project.find(params[:id]) + return true unless (params[:id] || params[:project_id]) + if params[:id] && controller.controller_name == "projects" + @project = Project.find(params[:id]) + else + @project = Project.find(params[:project_id]) + end check_project_privacy rescue ActiveRecord::RecordNotFound render_404 diff --git a/app/helpers/redsun_search_helper.rb b/app/helpers/redsun_search_helper.rb index 01c90be..e7d001c 100644 --- a/app/helpers/redsun_search_helper.rb +++ b/app/helpers/redsun_search_helper.rb @@ -3,12 +3,36 @@ module RedsunSearchHelper def facet(name, results, attribute = nil, partial = 'facet') render partial: partial, locals: { facet: name, results: results, attribute: attribute } end + + def selected_attr_for(facet, val) + if params[:search_form] && params[:search_form][facet.to_sym] + "selected" if params[:search_form][facet.to_sym] == val.to_s + end + end def highlighter_for(field, hit, record) if hit.highlights(field).present? hit.highlights(field).collect { |segment| segment.format { |word| "#{word}" } }.join(' ').html_safe else - record.send(field) if record.respond_to?(field.to_sym) + truncate(record.send(field).to_s, length: 300) if record.respond_to?(field.to_sym) end end + + def search2_enabled? + Setting[:plugin_redmine_redsun]['enable_select2_plugin'] + end + + def issue_coment_number(comment_id) + journal = Journal.find(comment_id) + entries = journal.journalized.journals + index = entries.find_index(journal) + index + 1 + end + + def issue_comment_path(comment_id) + journal = Journal.find(comment_id) + entries = journal.journalized.journals + index = entries.find_index(journal) + issue_url(journal.journalized, anchor: "note-#{issue_coment_number(comment_id)}", format: :html) + end end diff --git a/app/views/redsun_search/_class_name_facet.html.erb b/app/views/redsun_search/_class_name_facet.html.erb index c128dbb..8012e86 100644 --- a/app/views/redsun_search/_class_name_facet.html.erb +++ b/app/views/redsun_search/_class_name_facet.html.erb @@ -8,8 +8,13 @@ <% results.facet(facet.to_sym).rows.each do |result| %>
  • - <%= link_to "#{t('redsun.facets.values.'+result.value.to_s.downcase.tr(" ", "_"), default: result.value)} (#{number_with_delimiter(result.count, delimiter: '.')})", - url_for(search_form: {facet.to_sym => result.value}.reverse_merge(params[:search_form])), class: params[:search_form][facet.to_sym].blank? ? "" : "selected" + <% if @project.present? + url = redsun_project_search_path(@project, search_form: { class_name: [result.value] }.reverse_merge(params[:search_form])) + else + url = url_for(search_form: { class_name: [result.value]}.reverse_merge(params[:search_form])) + end %> + <%= link_to "#{t('redsun.facets.values.' + result.value.to_s.downcase.tr(" ", "_"), default: result.value)} (#{number_with_delimiter(result.count, delimiter: '.')})", + url, class: result.value.in?(params[:search_form][:class_name]) ? 'selected' : '' %>
  • <% end -%> diff --git a/app/views/redsun_search/_date_facet.html.erb b/app/views/redsun_search/_date_facet.html.erb index 5f3a93e..20c2d25 100644 --- a/app/views/redsun_search/_date_facet.html.erb +++ b/app/views/redsun_search/_date_facet.html.erb @@ -1,26 +1,36 @@ -<% if results.facet(facet).present? && results.facet(facet).rows && results.facet(facet).rows.count > 0 || params[:search_form][facet].present? %> +<% if results.facet(facet).present? && results.facet(facet).rows && results.facet(facet).rows.count > 1 || params[:search_form][facet].present? %>

    <%=t "redsun.facet.#{facet.to_sym}"%>

    - - -