diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 596069078..12b452097 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,18 +1,13 @@ class SearchController < ApplicationController + include PaginateHelper + before_filter :authenticate_user! unless APP_CONFIG['anonymous_access'] # load_and_authorize_resource def index - @type = params[:type] || 'all' - @query = params[:query] - Search.by_term_and_type( - @query, - @type, - current_ability, - {page: params[:page]} - ).each do |k, v| - var = :"@#{k}" - instance_variable_set var, v unless instance_variable_defined?(var) - end + @type = Search::TYPES.find{ |t| t == params[:type] } || Search::TYPES.first + @query = params[:query] + @search = Search.new(@query, current_ability, paginate_params) + @collection = @search.send(@type) end end diff --git a/app/models/search.rb b/app/models/search.rb index b25a9f046..ae7fcd2ff 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -1,29 +1,28 @@ -class Search - TYPES = ['projects', 'users', 'groups', 'platforms'] +class Search < Struct.new(:query, :ability, :paginate_params) + include ActiveModel::Conversion + extend ActiveModel::Naming - def self.by_term_and_type(term, type, ability, paginate_params) - results = {} - case type - when 'all' - TYPES.each{ |t| results[t] = find_collection(t, term, ability, paginate_params) } - when *TYPES - results[type] = find_collection(type, term, ability, paginate_params) - end - results - end + TYPES = %w(projects users groups platforms) - class << self - protected - def find_collection(type, term, ability, paginate_params) - scope = if type == 'users' - User.opened - else - type.classify.constantize.accessible_by(ability, :show) - end - scope.search(term). - search_order. - paginate(paginate_params) + TYPES.each do |type| + define_method type do + find_collection(type) end end + + private + + def find_collection(type) + scope = + if type == 'users' + User.opened + else + type.classify.constantize.accessible_by(ability, :show) + end + scope.search(query). + search_order. + paginate(paginate_params) + end + end \ No newline at end of file diff --git a/app/views/search/_form.html.haml b/app/views/search/_form.html.haml deleted file mode 100644 index fad76ff29..000000000 --- a/app/views/search/_form.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -.search - = form_tag search_index_path, method: 'get' do - .pic - .field= text_field_tag 'query', @query, placeholder: t("layout.search.header") \ No newline at end of file diff --git a/app/views/search/_form_advanced.html.haml b/app/views/search/_form_advanced.html.haml deleted file mode 100644 index 8951f0cfc..000000000 --- a/app/views/search/_form_advanced.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= form_tag search_index_path, method: 'get' do - .leftside= text_field_tag 'query', @query, placeholder: t("layout.search.header"), class: 'exsearch' - .lineForm.leftside.rmargin10= select_tag 'type', options_for_select(t('layout.search.types').invert, @type), class: 'sel80 cusel', id: 'selSearch' - .leftside= submit_tag t('layout.search.header'), class: 'button width100', data: {'disable-with' => t('layout.processing')} -.both \ No newline at end of file diff --git a/app/views/search/_group.html.haml b/app/views/search/_group.html.haml deleted file mode 100644 index 52c6f7e48..000000000 --- a/app/views/search/_group.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -%p.block - .img= image_tag 'ava-admin.png' - .forimg= link_to group.uname, group \ No newline at end of file diff --git a/app/views/search/_group.html.slim b/app/views/search/_group.html.slim new file mode 100644 index 000000000..897a5a6e4 --- /dev/null +++ b/app/views/search/_group.html.slim @@ -0,0 +1,7 @@ +h4 + - size = User::AVATAR_SIZES[:small] + => image_tag avatar_url(group, :small), alt: group.uname, height: size, width: size + a href=user_path(group) + = group.uname +p + = group.description.to_s.truncate(255) diff --git a/app/views/search/_platform.html.haml b/app/views/search/_platform.html.haml deleted file mode 100644 index c3f295ed7..000000000 --- a/app/views/search/_platform.html.haml +++ /dev/null @@ -1 +0,0 @@ -%p.block= link_to platform.name, platform \ No newline at end of file diff --git a/app/views/search/_platform.html.slim b/app/views/search/_platform.html.slim new file mode 100644 index 000000000..1b12b602e --- /dev/null +++ b/app/views/search/_platform.html.slim @@ -0,0 +1,9 @@ +h4 + - if platform.hidden? + i.fa.fa-lock.text-danger> + - else + i.fa.fa-unlock-alt.text-success> + a href=platform_path(platform) + = platform.name +p + = platform.description.to_s.truncate(255) \ No newline at end of file diff --git a/app/views/search/_project.html.haml b/app/views/search/_project.html.haml deleted file mode 100644 index b41f60028..000000000 --- a/app/views/search/_project.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -%p.block - = link_to "#{project.owner.uname} / #{project.name}", project - %br - = project.description \ No newline at end of file diff --git a/app/views/search/_project.html.slim b/app/views/search/_project.html.slim new file mode 100644 index 000000000..0916c60e6 --- /dev/null +++ b/app/views/search/_project.html.slim @@ -0,0 +1,12 @@ +h4 + - if project.public? + i.fa.fa-unlock-alt.text-success> + - else + i.fa.fa-lock.text-danger> + a href=project_path(project) + = project.name_with_owner +p + = project.description +.help-block + => t('.updated') + = time_ago_in_words(project.updated_at) diff --git a/app/views/search/_table.html.haml b/app/views/search/_table.html.haml deleted file mode 100644 index 90197f916..000000000 --- a/app/views/search/_table.html.haml +++ /dev/null @@ -1,10 +0,0 @@ -- collection = instance_variable_get("@#{type}") -%table.tablesorter.bmargin5{cellpadding: "0", cellspacing: "0"} - %thead - %tr - %th #{t "layout.#{type}.list_header"} (#{collection.count}) - %tbody - - collection.each do |c| - %tr - %td= render type.singularize, type.singularize.to_sym => c -= link_to t('layout.search.all'), search_index_path(query: @query, type: type) if collection.present? \ No newline at end of file diff --git a/app/views/search/_user.html.haml b/app/views/search/_user.html.haml deleted file mode 100644 index 4f84663e4..000000000 --- a/app/views/search/_user.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -%p.block - .img= image_tag avatar_url(user) - .forimg= link_to user.fullname, user \ No newline at end of file diff --git a/app/views/search/_user.html.slim b/app/views/search/_user.html.slim new file mode 100644 index 000000000..b6919ccd0 --- /dev/null +++ b/app/views/search/_user.html.slim @@ -0,0 +1,12 @@ +h4 + - size = User::AVATAR_SIZES[:small] + => image_tag avatar_url(user, :small), alt: user.uname, height: size, width: size + a href=user_path(user) + = user.fullname +.help-block + - if user.location.present? + i.fa.fa-map-marker> + => user.location + - unless user.hide_email + i.fa.fa-envelope-o> + => mail_to user.email, user.email, class: 'text-muted' diff --git a/app/views/search/index.html.haml b/app/views/search/index.html.haml deleted file mode 100644 index 74e4b3cb3..000000000 --- a/app/views/search/index.html.haml +++ /dev/null @@ -1,17 +0,0 @@ -%h3= title t('layout.search.advanced') -= render 'form_advanced' -- if @type == 'all' - #all - = render 'table', type: 'projects' - .both - .left.width400.rmargin55 - = render 'table', type: 'users' - = render 'table', type: 'groups' - .left.width400 - = render 'table', type: 'platforms' -- else - - collection = instance_variable_get("@#{@type}") - .tmargin10{id: @type}= render collection: collection, partial: @type.to_s.underscore.singularize - %br - = will_paginate collection -.both diff --git a/app/views/search/index.html.slim b/app/views/search/index.html.slim new file mode 100644 index 000000000..92ce9cb3a --- /dev/null +++ b/app/views/search/index.html.slim @@ -0,0 +1,42 @@ +.row + .col-md-3.col-md-offset-1 + h3 + = t('.advanced') + .col-md-7.offset20 + = simple_form_for @search, url: search_index_path, method: :get do |f| + .row + .col-md-6 + =hidden_field_tag :type, @type + = f.input :query, label: false, input_html: { name: 'query' } + .col-md-6 + = f.button :submit, t('.submit') + |   + = t('layout.or') + |   + a href=search_index_path + = t('layout.clear') + + +.row + .col-md-3.col-md-offset-1 + .panel.panel-default + .panel-body + ul.nav.nav-pills.nav-stacked + - t('.types').each do |k, v| + li class=('active' if k.to_s == @type) + a href=search_index_path(params.merge(type: k, page: 1)) + span.badge.pull-right + = @search.send(k).count + = v + + + .col-md-7 + table.table.table-striped + thead + tr + th + tbody + - @collection.each do |c| + tr + td= render @type.singularize, @type.singularize.to_sym => c + = will_paginate @collection diff --git a/config/locales/layout/search.en.yml b/config/locales/layout/search.en.yml index 177ee6cba..cf410a200 100644 --- a/config/locales/layout/search.en.yml +++ b/config/locales/layout/search.en.yml @@ -2,12 +2,21 @@ en: layout: search: header: Search + search: + index: + submit: Search advanced: Advanced search - all: Show All - no_results: Nothing found for "%{query}". + no_results: 'Nothing found for "%{query}".' types: - all: All projects: Projects users: Users groups: Groups platforms: Platforms + + project: + updated: "Updated:" + + simple_form: + placeholders: + search: + query: "Search" \ No newline at end of file diff --git a/config/locales/layout/search.ru.yml b/config/locales/layout/search.ru.yml index 53df60bcd..7d7ab6ea3 100644 --- a/config/locales/layout/search.ru.yml +++ b/config/locales/layout/search.ru.yml @@ -2,12 +2,21 @@ ru: layout: search: header: Поиск + search: + index: + submit: Поиск advanced: Расширенный поиск - all: Показать все - no_results: По запросу "%{query}" ничего не найдено. + no_results: 'По запросу "%{query}" ничего не найдено.' types: - all: Все projects: Проекты users: Пользователи groups: Группы platforms: Платформы + + project: + updated: "Обновлен:" + + simple_form: + placeholders: + search: + query: "Поиск" \ No newline at end of file