Refactored searches (user and globals earch) and used constants.

This commit is contained in:
Matej Zrimšek 2016-09-28 12:54:47 +02:00
parent f10b25dd31
commit 21ec05d541
11 changed files with 56 additions and 39 deletions

View file

@ -28,12 +28,12 @@ function truncateLongString( el, chars ) {
/*
* Usefull for converting locals messages to error format
* (i.e. lower cased capital and no dot at the end).
* (i.e. no dot at the end).
*/
String.prototype.strToErrorFormat = function() {
var length = this.length;
if (this[length - 1] === ".") {
length -= 1;
}
return this.charAt(0).toLowerCase() + this.slice(1, length);
return this.slice(0, length);
}

View file

@ -119,6 +119,9 @@ modal
disableInviteBtn();
})
.on("ajax:error", inviteExistingForm.selector, function(ev, data, status) {
//Clear previous info
inviteExistingResults.html("");
// Display form errors
inviteExistingForm.renderFormErrors("", data.responseJSON);
});

View file

@ -12,7 +12,8 @@ class OrganizationsController < ApplicationController
begin
if params[:file].size > FILE_MAX_SIZE.megabytes
error = t("organizations.parse_sheet.errors.file_size_exceeded")
error = t 'general.file.size_exceeded', file_size: FILE_MAX_SIZE
format.html {
flash[:alert] = error
redirect_to session.delete(:return_to)

View file

@ -2,8 +2,6 @@ class SearchController < ApplicationController
before_filter :load_vars, only: :index
before_filter :load_markdown, only: :index
MIN_QUERY_CHARS = 2
def index
if not @search_query
redirect_to new_search_path
@ -50,21 +48,27 @@ class SearchController < ApplicationController
@search_page = params[:page].to_i || 1
@display_query = @search_query
if @search_query.length < MIN_QUERY_CHARS
flash[:error] = t'search.index.error.query_length', n: MIN_QUERY_CHARS
if @search_query.length < NAME_MIN_LENGTH
flash[:error] = t 'general.query.length_too_short',
min_length: NAME_MIN_LENGTH
return redirect_to :back
end
# splits the search query to validate all entries
@splited_query = @search_query.split
if @splited_query.first.length < MIN_QUERY_CHARS
flash[:error] = t'search.index.error.query_length', n: MIN_QUERY_CHARS
if @splited_query.first.length < NAME_MIN_LENGTH
flash[:error] = t 'general.query.length_too_short',
min_length: NAME_MIN_LENGTH
redirect_to :back
elsif @splited_query.first.length > TEXT_MAX_LENGTH
flash[:error] = t 'general.query.length_too_long',
max_length: TEXT_MAX_LENGTH
redirect_to :back
elsif @splited_query.length > 1
@search_query = ''
@splited_query.each_with_index do |w, i|
@search_query += "#{@splited_query[i]} " if w.length >= MIN_QUERY_CHARS
@search_query += "#{@splited_query[i]} " if w.length >= NAME_MIN_LENGTH
end
else
@search_query = @splited_query.join(' ')

View file

@ -120,19 +120,27 @@ class Users::SettingsController < ApplicationController
respond_to do |format|
format.json {
if params.include? :existing_query and
(query = params[:existing_query].strip()).present?
if query.length < 3
query = params[:existing_query].strip()
if query.length < NAME_MIN_LENGTH
render json: {
"existing_query": [
I18n.t("users.settings.organizations.edit.modal_add_user.existing_query_too_short")
]},
status: :unprocessable_entity
t('general.query.length_too_short', min_length: NAME_MIN_LENGTH)
]
},
status: :unprocessable_entity
elsif query.length > NAME_MAX_LENGTH
render json: {
"existing_query": [
t('general.query.length_too_long', max_length: NAME_MAX_LENGTH)
]
},
status: :unprocessable_entity
else
# Okay, query exists and is non-blank, find users
nr_of_results = User.search(true, query, @org).count
users = User.search(false, query, @org).limit(5)
users = User.search(false, query, @org).limit(EXISTING_USERS_SEARCH_LIMIT)
nr_of_members = User.organization_search(false, query, @org).count
@ -150,11 +158,7 @@ class Users::SettingsController < ApplicationController
}
end
else
render json: {
"existing_query": [
I18n.t("users.settings.organizations.edit.modal_add_user.existing_query_blank")
]},
status: :unprocessable_entity
render json: {}, status: :bad_request
end
}
end

View file

@ -1,11 +1,17 @@
<% provide(:head_title, t("search.index.head_title")) %>
<!--
<h1 class="page-header"><%= t 'search.index.page_title' %></h1>
<%= form_tag search_path, method: :get, class: 'form-inline' do %>
<!-- search form -->
<%= form_tag search_path, method: :get, id: 'search-bar', class: 'navbar-form navbar-left', role: 'search' do %>
<div class="form-group">
<%= text_field_tag :q, '', class: 'form-control', size: 25, maxlength: 50, pattern: '.{0,50}', required: '', title: t('search.index.error.max_length', n: 50) %>
<div class="input-group">
<input class="form-control" type="text" name="q" placeholder="<%= t('nav.search') %>">
<span class="input-group-btn">
<button id="search-button" class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
<%= button_tag 'Search', name: '', class: 'btn btn-default' %>
<% end %>
-->

View file

@ -107,7 +107,7 @@
<%= form_tag search_path, method: :get, id: 'search-bar', class: 'navbar-form navbar-right', role: 'search' do %>
<div class="form-group">
<div class="input-group">
<input class="form-control" type="text" name="q" maxlength="50" placeholder="<%= t('nav.search') %>">
<input class="form-control" type="text" name="q" placeholder="<%= t('nav.search') %>">
<span class="input-group-btn">
<button id="search-button" class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-search"></span>
@ -123,5 +123,4 @@
</nav>
<div id="loading-animation"></div>
<%= javascript_include_tag("navigation") %>

View file

@ -21,7 +21,7 @@
<% end %>
<% if nr_of_results > users.count %>
<div class="existing-users-smalltext"><em>
<%= t("users.settings.organizations.edit.modal_add_user.existing_users_smalltext", nr: users.count) %>
<%= t("users.settings.organizations.edit.modal_add_user.existing_users_smalltext", count: users.count) %>
</em></div>
<% end %>
<% end %>

View file

@ -45,8 +45,12 @@ DROPDOWN_TOP_OFFSET = 20
SEARCH_LIMIT = 20
EXISTING_USERS_SEARCH_LIMIT = 5
SHOW_ALL_RESULTS = -1
QUERY_MIN_LENGTH = 2
TEXT_EXTRACT_FILE_TYPES = [
"application/pdf",
"application/rtf",

View file

@ -125,8 +125,6 @@ en:
no_name: "(no name)"
error:
no_results: "No results for %{q}."
query_length: "Search query is too short. It should contain at least %{n} characters."
max_length: "Search query is too long. The query is limited to %{n} characters."
samples:
sample: "Sample: "
sample_type: "Sample type: "
@ -691,9 +689,6 @@ en:
name: "Task name"
name_placeholder: "My task"
confirm: "Add task"
error_length: "Name must contain from 2 to 50 characters."
error_invalid: "Name contains invalid characters ('|')."
error_whitespaces: "Name cannot be blank."
modal_edit_module:
title: "Rename task"
name: "Task name"
@ -973,7 +968,6 @@ en:
temp_file_failure: "We couldn't create temporary file. Please contact administrator."
no_file_selected: "You didn't select any file."
errors_list_title: "Samples were not imported because one or more errors was found:"
file_size_exceeded: "Must be less than 50MB"
list_row: "Row %{row}"
list_error: "%{key}: %{val}"
import_samples:
@ -1211,8 +1205,6 @@ en:
existing_heading: "Invite existing sciNote user"
existing_label: "Find existing user by name or email:"
existing_placeholder: "Name or email"
existing_query_too_short: "Query is too short (minimum is 3 characters)"
existing_query_blank: "Query can't be blank"
existing_results_title: "Choose user"
no_existing_users: "No existing users found."
existing_users_members_smalltext:
@ -1221,7 +1213,7 @@ en:
no_existing_users_members_smalltext:
one: "No existing users that could be invited found; the search, however, matched 1 user that is already member of the team."
other: "No existing users that could be invited found; the search, however, matched %{count} users that are already members of the team."
existing_users_smalltext: "Only showing top %{nr} matched results."
existing_users_smalltext: "Only showing top %{count} matched results."
existing_flash_success: "User %{user} successfully invited to team as %{role}."
existing_flash_error: "Error inviting user to team."
new_heading: "Invite new user"
@ -1537,6 +1529,9 @@ en:
not_blank: "can't be blank"
length_too_long: "is too long (maximum is %{max_length} characters)"
length_too_short: "is too short (minimum is %{min_length} characters)"
query:
length_too_long: "Search query is too long (maximum is %{max_length} characters)"
length_too_short: "Search query is too short (minimum is %{min_length} characters)"
busy: "The server is still processing your request. If you leave this page, the changes will be lost! Are you sure you want to continue?"
Add: "Add"

View file

@ -65,7 +65,8 @@
If you are the application owner check the logs for more information.
You can go back to
<a href="/">home page</a>
<a href="/search/new">or try searching.</a>
or try
<a href="/search/new">searching.</a>
</p>
</div>
</body>