Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
KieranHayes committed Feb 8, 2017
2 parents cb7348a + 672a99c commit 1b2de6e
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 129 deletions.
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ group :development do
gem 'sunspot_solr', '2.2.0'
end

group :development do
gem 'progress_bar', '~> 1.0.3'
end
gem 'progress_bar', '~> 1.0.3'
77 changes: 42 additions & 35 deletions app/controllers/redsun_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
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_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 search_scope == 'my_projects'
projects = User.current.memberships.collect(&:project).compact.uniq
Expand All @@ -41,9 +41,10 @@ def index
allowed_issues = allowed_issues.push(0)
allowed_wikis = allowed_wikis.push(0)

@search = Sunspot.search([Project, Issue, WikiPage, Journal, Attachment]) do
@search = Sunspot.search([Project, Issue, WikiPage, Journal, Attachment, News]) do
fulltext searchstring do
highlight :description
highlight :summary
highlight :comments
highlight :subject
highlight :wiki_content
Expand All @@ -60,6 +61,7 @@ def index
with(:project_id).any_of allowed_issues.flatten
with(:is_private, false)
end

all_of do
with :class, WikiPage
with(:project_id).any_of allowed_wikis.flatten
Expand All @@ -81,6 +83,11 @@ def index
with :class, Attachment
with(:project_id).any_of allowed_projects.flatten
end

all_of do
with :class, News
with(:project_id).any_of allowed_projects.flatten
end

end

Expand All @@ -90,33 +97,32 @@ def index
priority_id
tracker_id
status_id
category_id
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
class_name).each do |easy_facet|
facet_filter = if params[:search_form].key?(easy_facet)
with(easy_facet).any_of(params[:search_form][easy_facet])
else
all_of {} # not necessary for searching, but object is needed to set exclude option
end
facet easy_facet, minimum_count: 0, exclude: facet_filter
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

# Class
class_facet_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: 0, exclude: class_facet_filter
%w(created_on updated_on).each do |date_facet|
if params[:search_form].present? && params[:search_form][date_facet].present?
date_range = date_conditions.collect { |c| c[:date] if (c[:name].to_s == params[:search_form][date_facet.to_sym]) }.compact.first
with(date_facet).greater_than(date_range) unless date_range.nil?
end
end

if params.key?(:search_form) && params[:search_form][:active].present?
with(:active, (params[:search_form][:active] == 'true' ? true : false))
end

# Pagination
paginate(page: params[:page], per_page: 15)

Expand All @@ -140,9 +146,10 @@ def index

order_by(sort_field.to_sym, sort_order.downcase.to_sym)
order_by(:score, :desc)
spellcheck :count => 3
spellcheck(count: 3)
end
@searchstring = searchstring
redirect_if_neccessary

rescue Errno::ECONNREFUSED
render 'connection_refused'
Expand All @@ -154,22 +161,9 @@ def index

def set_search_form
params[:search_form] = {} unless params[:search_form].present?
# Reset facets if search button is pressed
if params[:commit].present?
[:author_id,
:status_id,
:tracker_id,
:priority_id,
:created_on,
:updated_on,
:class_name,
:active,
:project_name].each do |facet|
params[:search_form].delete(facet) if params[:search_form][facet].present?
end
end
@scope = params[:search_form][:scope] || 'all_projects'
params[:search_form][:class_name] ||= []
params[:search_form][:class_name] = [] if params[:search_form][:class_name].blank?
@sort_field = sort_field
@sort_order = sort_order
@scope_selector = [[l(:label_my_projects), 'my_projects'], [l(:label_project_all), 'all_projects']]
Expand All @@ -188,6 +182,19 @@ def date_conditions
end

private

def redirect_if_neccessary
# No need to redirect
return true if @search.total > 0
# Class name was select but no result was found
if params[:search_form].key?(:class_name) && params[:search_form][:class_name].any? && @search.facet(:class_name).rows.map(&:count).count > 0
flash_message = I18n.translate("redsun.redirect_for_missing_results",
class_name: I18n.translate("redsun.#{params[:search_form][:class_name].try(:first).try(:downcase)}") )
redirect_to redsun_search_url(search_form: params[:search_form].except(:class_name)), notice: flash_message
else
true
end
end

def search_scope
params[:search_form][:scope] || 'all_projects'
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/redsun_search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def facet(name, results, attribute = nil, partial = 'facet')

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
"selected" if val.to_s.in?(params[:search_form][facet.to_sym])
end
end

Expand Down
26 changes: 14 additions & 12 deletions app/views/redsun_search/_class_name_facet.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<% if results.facet(facet).present? && results.facet(facet).rows && results.facet(facet).rows.count >= 1 %>
<div class="tabs">
<ul>
<li>
<%= link_to t("redsun.facet.reset_object_facet"),
<div class="tabs redsun-tabs">
<ul>
<li>
<% count_all = @search.facet(:class_name).rows.map(&:count).sum %>
<% if count_all > 0 %>
<%= link_to t("redsun.facet.reset_object_facet", count: number_with_delimiter(count_all)),
url_for(search_form: {facet.to_sym => ""}.reverse_merge(params[:search_form]) ),
class: params[:search_form][facet.to_sym].blank? ? "selected" : "" %>
</li>
<% results.facet(facet.to_sym).rows.each do |result| %>
<% end %>
</li>
<% results.facet(facet.to_sym).rows.each do |result| %>
<% if result.count > 0 || facet.in?(params[:search_form][:class_name]) %>
<li>
<% if @project.present?
url = redsun_project_search_path(@project, search_form: { class_name: [result.value] }.reverse_merge(params[:search_form]))
Expand All @@ -17,8 +20,7 @@
url, class: result.value.in?(params[:search_form][:class_name]) ? 'selected' : ''
%>
</li>
<% end -%>

</ul>
</div>
<% end -%>
<% end %>
<% end %>
</ul>
</div>
2 changes: 1 addition & 1 deletion app/views/redsun_search/_date_facet.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% 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>
<% 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!")%>">
<select data-cleared-url="<%=url_for(:search_form => { facet.to_sym => "" }.reverse_merge(params[:search_form]))%>" class="redsun-select2-single" 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]))%>">
Expand Down
89 changes: 49 additions & 40 deletions app/views/redsun_search/_facet.html.erb
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
<% 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}")%></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?) %>
<% 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 %>
<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])))
%>
<% if results.facet(facet).present? &&
results.facet(facet).rows &&
results.facet(facet).rows.count > 1 &&
results.facet(facet).rows.sum(&:count) > 0 || params[:search_form][facet].present? %>

<% else %>
<%= link_to "#{result.value} (#{result.count})",
url_for(:search_form => {facet => result.value}.reverse_merge(params[:search_form]))
%>
<h3 class='redsun-sidebar'><%= t("redsun.facet.#{facet}")%></h3>
<% params[:search_form][facet] ||= [] %>
<% if search2_enabled? %>
<select multiple size="5" class="redsun-select2" data-placeholder="<%=t("redsun.placeholder.#{facet}", default: "Select a value!")%>">
<option></option>
<% results.facet(facet).rows.each do |result| %>
<% if result.count > 0 %>
<% if (attribute.present? && result.instance.present?)
val = result.instance.send(attribute.to_sym)
label = (val.present? ? val : "unknown" ) + " (#{number_with_delimiter(result.count, delimiter: '.')})"
else
label = "#{result.value} (#{number_with_delimiter(result.count, delimiter: '.')})"
end %>
<% if result.value.to_s.in?(params[:search_form][facet]) %>
<option value='<%= result.value%>' data-facet-url='<%= url_for(search_form: { facet => (params[:search_form][facet] - [result.value.to_s])}.reverse_merge(params[:search_form])) %>' selected><%= label %></option>
<% else %>
<option data-facet-url='<%= url_for(search_form: { facet => (params[:search_form][facet] + [result.value])}.reverse_merge(params[:search_form])) %>' value='<%= result.value.to_s %>'><%= label %></option>
<% end %>
<% 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 -%>
<% end %>
</select>
<% else %>
<ul class="facets">
<% results.facet(facet).rows.each do |result| %>
<% if result.count > 0 %>
<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 => (params[:search_form][facet] + [result.value]) }.reverse_merge(params[:search_form]))) %>
<% else %>
<%= link_to "#{result.value} (#{result.count})",
url_for(:search_form => { facet => (params[:search_form][facet] + [result.value]) }.reverse_merge(params[:search_form]))%>
<% end %>
<% if params[:search_form][facet].present? && result.value.to_s.in?(params[:search_form][facet]) %>
<%= link_to t("redsun.facet.remove"),
url_for(search_form: { facet.to_sym => (params[:search_form][facet] - [result.value.to_s]) }.reverse_merge(params[:search_form]) ),
class: "remove" %>
<% end %>
</li>
<% end %>
<% end %>
</ul>
<% end %>
<% end %>
7 changes: 4 additions & 3 deletions app/views/redsun_search/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<br><%= page_entries_info @search.results, { model: 'record' } %>
<div class='redsun-pagination-info'>
<%= page_entries_info @search.results, { model: 'record' } %>
</div>
<div class='redsun-pagination-top redsun-pagination'>
<%= will_paginate @search.results %>
</div>
Expand All @@ -18,11 +20,10 @@
</div>
<% end %>
</div>

<% end %>

<div class='redsun-pagination-bottom redsun-pagination'>
<%= will_paginate @search.results %>
<%= will_paginate @search.results %>
</div>
<% content_for :sidebar do %>
<%= facet(:project_name, @search, :project_name) %>
Expand Down
Loading

0 comments on commit 1b2de6e

Please sign in to comment.