Simple search implementation
This commit is contained in:
parent
b2c13bdd77
commit
5d2bd5ec3a
13 changed files with 127 additions and 4 deletions
|
@ -11,6 +11,7 @@
|
||||||
//= require jquery.tagify
|
//= require jquery.tagify
|
||||||
//= require jquery.cookie
|
//= require jquery.cookie
|
||||||
//= require jquery.endless-scroll
|
//= require jquery.endless-scroll
|
||||||
|
//= require jquery.highlight
|
||||||
//= require bootstrap-modal
|
//= require bootstrap-modal
|
||||||
//= require modernizr
|
//= require modernizr
|
||||||
//= require chosen
|
//= require chosen
|
||||||
|
|
|
@ -947,3 +947,7 @@ p.time {
|
||||||
padding: 4px;
|
padding: 4px;
|
||||||
margin: 2px;
|
margin: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.highlight_word {
|
||||||
|
background:#EEDC94;
|
||||||
|
}
|
||||||
|
|
12
app/controllers/search_controller.rb
Normal file
12
app/controllers/search_controller.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
class SearchController < ApplicationController
|
||||||
|
def show
|
||||||
|
query = params[:search]
|
||||||
|
if query.blank?
|
||||||
|
@projects = []
|
||||||
|
@merge_requests = []
|
||||||
|
else
|
||||||
|
@projects = Project.search(query).limit(10)
|
||||||
|
@merge_requests = MergeRequest.search(query).limit(10)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -37,6 +37,10 @@ class MergeRequest < ActiveRecord::Base
|
||||||
scope :closed, where(:closed => true)
|
scope :closed, where(:closed => true)
|
||||||
scope :assigned, lambda { |u| where(:assignee_id => u.id)}
|
scope :assigned, lambda { |u| where(:assignee_id => u.id)}
|
||||||
|
|
||||||
|
def self.search query
|
||||||
|
where("title like :query", :query => "%#{query}%")
|
||||||
|
end
|
||||||
|
|
||||||
def validate_branches
|
def validate_branches
|
||||||
if target_branch == source_branch
|
if target_branch == source_branch
|
||||||
errors.add :base, "You can not use same branch for source and target branches"
|
errors.add :base, "You can not use same branch for source and target branches"
|
||||||
|
|
|
@ -54,6 +54,10 @@ class Project < ActiveRecord::Base
|
||||||
UsersProject.access_roles
|
UsersProject.access_roles
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.search query
|
||||||
|
where("name like :query or code like :query or path like :query", :query => "%#{query}%")
|
||||||
|
end
|
||||||
|
|
||||||
def to_param
|
def to_param
|
||||||
code
|
code
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
- @active_projects.first(5).each do |project|
|
- projects.first(5).each do |project|
|
||||||
.wll
|
.wll
|
||||||
= link_to project do
|
= link_to project do
|
||||||
%h4
|
%h4
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
.row
|
.row
|
||||||
.dashboard_block
|
.dashboard_block
|
||||||
.row
|
.row
|
||||||
.span10= render "dashboard/projects_feed"
|
.span10= render "dashboard/projects_feed", :projects => @active_projects
|
||||||
.span4.right
|
.span4.right
|
||||||
- if current_user.can_create_project?
|
- if current_user.can_create_project?
|
||||||
.alert-message.block-message.warning
|
.alert-message.block-message.warning
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
%nav.main_menu
|
%nav.main_menu
|
||||||
= render "layouts/const_menu_links"
|
= render "layouts/const_menu_links"
|
||||||
= link_to "Projects", projects_path, :class => "#{"current" if current_page?(projects_path)}"
|
= link_to "Projects", projects_path, :class => "#{"current" if current_page?(projects_path)}"
|
||||||
|
= link_to "Search", search_path, :class => "#{"current" if current_page?(search_path)}"
|
||||||
= link_to "Issues", dashboard_issues_path, :class => "#{"current" if current_page?(dashboard_issues_path)}", :id => "issues_slide"
|
= link_to "Issues", dashboard_issues_path, :class => "#{"current" if current_page?(dashboard_issues_path)}", :id => "issues_slide"
|
||||||
= link_to "Requests", dashboard_merge_requests_path, :class => "#{"current" if current_page?(dashboard_merge_requests_path)}", :id => "merge_requests_slide"
|
= link_to "Requests", dashboard_merge_requests_path, :class => "#{"current" if current_page?(dashboard_merge_requests_path)}", :id => "merge_requests_slide"
|
||||||
= link_to "Help", help_path, :class => "#{"current" if controller.controller_name == "help"}"
|
= link_to "Help", help_path, :class => "#{"current" if controller.controller_name == "help"}"
|
||||||
|
|
|
@ -7,7 +7,9 @@
|
||||||
%h1
|
%h1
|
||||||
GITLAB
|
GITLAB
|
||||||
%h1.project_name= title
|
%h1.project_name= title
|
||||||
.search= text_field_tag "search", nil, :placeholder => "Search", :class => "search-input"
|
.search
|
||||||
|
= form_tag search_path, :method => :get do |f|
|
||||||
|
= text_field_tag "search", nil, :placeholder => "Search", :class => "search-input"
|
||||||
- if current_user.is_admin?
|
- if current_user.is_admin?
|
||||||
= link_to admin_projects_path, :class => "admin_link", :title => "Admin area" do
|
= link_to admin_projects_path, :class => "admin_link", :title => "Admin area" do
|
||||||
= image_tag "admin.PNG", :width => 16
|
= image_tag "admin.PNG", :width => 16
|
||||||
|
|
1
app/views/search/_result.html.haml
Normal file
1
app/views/search/_result.html.haml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
41
app/views/search/show.html.haml
Normal file
41
app/views/search/show.html.haml
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
= form_tag search_path, :method => :get do |f|
|
||||||
|
.padded
|
||||||
|
= label_tag :search, "Looking for"
|
||||||
|
.input
|
||||||
|
= text_field_tag :search, params[:search],:placeholder => "issue 143", :class => "xxlarge"
|
||||||
|
= submit_tag 'Search', :class => "btn primary"
|
||||||
|
- if params[:search].present?
|
||||||
|
%br
|
||||||
|
%h3 Search results
|
||||||
|
%hr
|
||||||
|
.search_results
|
||||||
|
- if @projects.empty? && @merge_requests.empty?
|
||||||
|
%h3
|
||||||
|
%small Nothing here
|
||||||
|
- else
|
||||||
|
- if @projects.any?
|
||||||
|
- @projects.each do |project|
|
||||||
|
= link_to project do
|
||||||
|
%h4
|
||||||
|
%span.ico.project
|
||||||
|
= project.name
|
||||||
|
%small
|
||||||
|
last activity at
|
||||||
|
= project.last_activity_date.stamp("Aug 25, 2011")
|
||||||
|
- if @merge_requests.any?
|
||||||
|
- @merge_requests.each do |merge_request|
|
||||||
|
= link_to [merge_request.project, merge_request] do
|
||||||
|
%h5
|
||||||
|
Merge Request #
|
||||||
|
= merge_request.id
|
||||||
|
–
|
||||||
|
= truncate merge_request.title, :length => 50
|
||||||
|
%small
|
||||||
|
updated at
|
||||||
|
= merge_request.updated_at.stamp("Aug 25, 2011")
|
||||||
|
%strong
|
||||||
|
%span.label= merge_request.project.name
|
||||||
|
:javascript
|
||||||
|
$(function() {
|
||||||
|
$(".search_results").highlight("#{params[:search]}");
|
||||||
|
})
|
|
@ -1,5 +1,5 @@
|
||||||
Gitlab::Application.routes.draw do
|
Gitlab::Application.routes.draw do
|
||||||
|
get 'search' => "search#show"
|
||||||
|
|
||||||
# Optionally, enable Resque here
|
# Optionally, enable Resque here
|
||||||
require 'resque/server'
|
require 'resque/server'
|
||||||
|
|
53
vendor/assets/javascripts/jquery.highlight.js
vendored
Normal file
53
vendor/assets/javascripts/jquery.highlight.js
vendored
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
highlight v3
|
||||||
|
|
||||||
|
Highlights arbitrary terms.
|
||||||
|
|
||||||
|
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
|
||||||
|
|
||||||
|
MIT license.
|
||||||
|
|
||||||
|
Johann Burkard
|
||||||
|
<http://johannburkard.de>
|
||||||
|
<mailto:jb@eaio.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
jQuery.fn.highlight = function(pat) {
|
||||||
|
function innerHighlight(node, pat) {
|
||||||
|
var skip = 0;
|
||||||
|
if (node.nodeType == 3) {
|
||||||
|
var pos = node.data.toUpperCase().indexOf(pat);
|
||||||
|
if (pos >= 0) {
|
||||||
|
var spannode = document.createElement('span');
|
||||||
|
spannode.className = 'highlight_word';
|
||||||
|
var middlebit = node.splitText(pos);
|
||||||
|
var endbit = middlebit.splitText(pat.length);
|
||||||
|
var middleclone = middlebit.cloneNode(true);
|
||||||
|
spannode.appendChild(middleclone);
|
||||||
|
middlebit.parentNode.replaceChild(spannode, middlebit);
|
||||||
|
skip = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
|
||||||
|
for (var i = 0; i < node.childNodes.length; ++i) {
|
||||||
|
i += innerHighlight(node.childNodes[i], pat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return skip;
|
||||||
|
}
|
||||||
|
return this.each(function() {
|
||||||
|
innerHighlight(this, pat.toUpperCase());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
jQuery.fn.removeHighlight = function() {
|
||||||
|
return this.find("span.highlight").each(function() {
|
||||||
|
this.parentNode.firstChild.nodeName;
|
||||||
|
with (this.parentNode) {
|
||||||
|
replaceChild(this.firstChild, this);
|
||||||
|
normalize();
|
||||||
|
}
|
||||||
|
}).end();
|
||||||
|
};
|
Loading…
Reference in a new issue