Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Redsun 2.0
  • Loading branch information
KieranHayes committed Nov 16, 2016
2 parents a9c97c6 + 9532252 commit cb7348a
Show file tree
Hide file tree
Showing 21 changed files with 1,487 additions and 223 deletions.
9 changes: 4 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
90 changes: 50 additions & 40 deletions app/controllers/redsun_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] || ''
Expand All @@ -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
Expand All @@ -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
Expand All @@ -54,6 +51,7 @@ def index
highlight :notes
highlight :id
highlight :title
highlight :filename
end

any_of do
Expand All @@ -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?
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand Down
26 changes: 25 additions & 1 deletion app/helpers/redsun_search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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| "<span class='redsun-highlight'>#{word}</span>" } }.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
9 changes: 7 additions & 2 deletions app/views/redsun_search/_class_name_facet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
</li>
<% results.facet(facet.to_sym).rows.each do |result| %>
<li>
<%= 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' : ''
%>
</li>
<% end -%>
Expand Down
56 changes: 33 additions & 23 deletions app/views/redsun_search/_date_facet.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
<h3><%=t "redsun.facet.#{facet.to_sym}"%></h3>
<style>
ul.facets {
max-height: 200px;
overflow: scroll;
}
</style>

<ul class="facets">
<% results.facet(facet.to_sym).rows.each do |result| %>
<% if params[:search_form][facet].blank? || params[:search_form].present? && params[:search_form][facet].present? && params[:search_form][facet] == result.value %>
<li>
<% label = t("redsun.facet.date_range_rows.#{result.value}") %>
<%= link_to((label.present? ? label : "unknown" ) + " (#{result.count})",
url_for(:controller => "redsun_search", :action => :index, :search_form => {facet.to_sym => result.value}.reverse_merge(params[:search_form])))
%>
<%= link_to t("facet.remove"),
url_for(:controller => "redsun_search", :action => :index, :search_form => { facet.to_sym => "" }.reverse_merge(params[:search_form])),
:class => "remove" if params[:search_form][facet].present? %>
</li>
<% if search2_enabled? %>
<select data-cleared-url="<%=url_for(:search_form => { facet.to_sym => "" }.reverse_merge(params[:search_form]))%>" class="redsun-select2" size="1" data-placeholder="<%=t("redsun.placeholder.#{facet}", default: "Select a value!")%>">
<option></option>
<% results.facet(facet.to_sym).rows.each do |result| %>
<option <%=selected_attr_for(facet, result.value) %> value="<%=url_for(:search_form => {facet.to_sym => result.value}.reverse_merge(params[:search_form]))%>">
<% label = t("redsun.facet.date_range_rows.#{result.value}") %>
<%= (label.present? ? label : "unknown" ) + " (#{result.count})"%>
</option>
<% end %>
<% end -%>
</ul>
</select>
<% else %>
<style>
ul.facets {
max-height: 200px;
overflow: scroll;
}
</style>
<ul class="facets">
<% results.facet(facet.to_sym).rows.each do |result| %>
<% if params[:search_form][facet].blank? || params[:search_form].present? && params[:search_form][facet].present? && params[:search_form][facet] == result.value %>
<li>
<% label = t("redsun.facet.date_range_rows.#{result.value}") %>
<%= link_to((label.present? ? label : "unknown" ) + " (#{result.count})",
url_for(:controller => "redsun_search", :action => :index, :search_form => {facet.to_sym => result.value}.reverse_merge(params[:search_form])))
%>
<%= link_to t("facet.remove"),
url_for(:controller => "redsun_search", :action => :index, :search_form => { facet.to_sym => "" }.reverse_merge(params[:search_form])),
:class => "remove" if params[:search_form][facet].present? %>
</li>
<% end %>
<% end -%>
</ul>
<% end -%>
<% end -%>
55 changes: 36 additions & 19 deletions app/views/redsun_search/_facet.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
<% if results.facet(facet).present? && results.facet(facet).rows && results.facet(facet).rows.count > 1 || params[:search_form][facet].present? %>
<h3><%=t "redsun.facet.#{facet.to_sym}"%></h3>

<ul class="facets">
<% results.facet(facet.to_sym).rows.each do |result| %>
<li>
<h3><%= t("redsun.facet.#{facet}")%></h3>
<% if search2_enabled? %>
<select data-cleared-url="<%=url_for(:search_form => { facet => "" }.reverse_merge(params[:search_form]))%>" class="redsun-select2" size="1" data-placeholder="<%=t("redsun.placeholder.#{facet}", default: "Select a value!")%>">
<option></option>
<% results.facet(facet).rows.each do |result| %>
<% if (attribute.present? && result.instance.present?) %>
<% label = result.instance.send(attribute.to_sym) %>
<%= link_to((label.present? ? label : "unknown" ) + " (#{result.count})",
url_for(:search_form => {facet.to_sym => result.value}.reverse_merge(params[:search_form])))
%>
<% val = result.instance.send(attribute.to_sym) %>
<option <%=selected_attr_for(facet, result.value) %> value="<%=url_for(:search_form => {facet => result.value}.reverse_merge(params[:search_form]))%>">
<%= (val.present? ? val : "unknown" ) + " (#{number_with_delimiter(result.count, delimiter: '.')})" %>
</option>
<% else %>
<%= link_to "#{t('redsun.facets.values.'+result.value.to_s.downcase.tr(" ", "_"), :default => result.value)} (#{result.count})",
url_for(:search_form => {facet.to_sym => result.value}.reverse_merge(params[:search_form]))
%>
<option <%=selected_attr_for(facet, result.value) %> value="<%=url_for(:search_form => {facet => result.value}.reverse_merge(params[:search_form]))%>">
<%= "#{result.value} (#{number_with_delimiter(result.count, delimiter: '.')})" %>
</option>
<% end %>
<% end %>
</select>
<% else %>
<ul class="facets">
<% results.facet(facet).rows.each do |result| %>
<li>
<% if (attribute.present? && result.instance.present?) %>
<% label = result.instance.send(attribute.to_sym) %>
<%= link_to((label.present? ? label : "unknown" ) + " (#{result.count})",
url_for(:search_form => {facet => result.value}.reverse_merge(params[:search_form])))
%>
<%= link_to t("redsun.facet.remove"),
url_for(:search_form => { facet.to_sym => "" }.reverse_merge(params[:search_form])),
:class => "remove" if params[:search_form][facet].present? %>
</li>
<% end -%>
</ul>
<% else %>
<%= link_to "#{result.value} (#{result.count})",
url_for(:search_form => {facet => result.value}.reverse_merge(params[:search_form]))
%>
<% end %>
<%= link_to t("redsun.facet.remove"),
url_for(:search_form => { facet => "" }.reverse_merge(params[:search_form])),
:class => "remove" if params[:search_form][facet].present? %>
</li>
<% end -%>
</ul>
<% end %>
<% end -%>
Loading

0 comments on commit cb7348a

Please sign in to comment.