Simple search implementation

This commit is contained in:
Dmitriy Zaporozhets 2012-03-16 01:14:39 +02:00
parent b2c13bdd77
commit 5d2bd5ec3a
13 changed files with 127 additions and 4 deletions

View file

@ -11,6 +11,7 @@
//= require jquery.tagify
//= require jquery.cookie
//= require jquery.endless-scroll
//= require jquery.highlight
//= require bootstrap-modal
//= require modernizr
//= require chosen

View file

@ -947,3 +947,7 @@ p.time {
padding: 4px;
margin: 2px;
}
.highlight_word {
background:#EEDC94;
}

View 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

View file

@ -37,6 +37,10 @@ class MergeRequest < ActiveRecord::Base
scope :closed, where(:closed => true)
scope :assigned, lambda { |u| where(:assignee_id => u.id)}
def self.search query
where("title like :query", :query => "%#{query}%")
end
def validate_branches
if target_branch == source_branch
errors.add :base, "You can not use same branch for source and target branches"

View file

@ -54,6 +54,10 @@ class Project < ActiveRecord::Base
UsersProject.access_roles
end
def self.search query
where("name like :query or code like :query or path like :query", :query => "%#{query}%")
end
def to_param
code
end

View file

@ -1,4 +1,4 @@
- @active_projects.first(5).each do |project|
- projects.first(5).each do |project|
.wll
= link_to project do
%h4

View file

@ -20,7 +20,7 @@
.row
.dashboard_block
.row
.span10= render "dashboard/projects_feed"
.span10= render "dashboard/projects_feed", :projects => @active_projects
.span4.right
- if current_user.can_create_project?
.alert-message.block-message.warning

View file

@ -1,6 +1,7 @@
%nav.main_menu
= render "layouts/const_menu_links"
= 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 "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"}"

View file

@ -7,7 +7,9 @@
%h1
GITLAB
%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?
= link_to admin_projects_path, :class => "admin_link", :title => "Admin area" do
= image_tag "admin.PNG", :width => 16

View file

@ -0,0 +1 @@

View 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
&ndash;
= 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]}");
})

View file

@ -1,5 +1,5 @@
Gitlab::Application.routes.draw do
get 'search' => "search#show"
# Optionally, enable Resque here
require 'resque/server'

View 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();
};