From cb9cf077de8bf315842a7f357b65ec7e5b5ce0de Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sun, 20 Oct 2013 01:05:20 -0400 Subject: Refactor and cover search_autocomplete_source --- app/helpers/application_helper.rb | 45 ---------------------- app/helpers/search_helper.rb | 78 ++++++++++++++++++++++++++++++++++++++ spec/helpers/search_helper_spec.rb | 64 +++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 45 deletions(-) create mode 100644 app/helpers/search_helper.rb create mode 100644 spec/helpers/search_helper_spec.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ab98c894b82..e2d97901410 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -98,51 +98,6 @@ module ApplicationHelper grouped_options_for_select(options, @ref || @project.default_branch) end - def search_autocomplete_source - return unless current_user - - projects = current_user.authorized_projects.map { |p| { label: "project: #{simple_sanitize(p.name_with_namespace)}", url: project_path(p) } } - groups = current_user.authorized_groups.map { |group| { label: "group: #{simple_sanitize(group.name)}", url: group_path(group) } } - - default_nav = [ - { label: "My Profile settings", url: profile_path }, - { label: "My SSH Keys", url: profile_keys_path }, - { label: "My Dashboard", url: root_path }, - { label: "Admin Section", url: admin_root_path }, - ] - - help_nav = [ - { label: "help: API Help", url: help_api_path }, - { label: "help: Markdown Help", url: help_markdown_path }, - { label: "help: Permissions Help", url: help_permissions_path }, - { label: "help: Public Access Help", url: help_public_access_path }, - { label: "help: Rake Tasks Help", url: help_raketasks_path }, - { label: "help: SSH Keys Help", url: help_ssh_path }, - { label: "help: System Hooks Help", url: help_system_hooks_path }, - { label: "help: Web Hooks Help", url: help_web_hooks_path }, - { label: "help: Workflow Help", url: help_workflow_path }, - ] - - project_nav = [] - if @project && @project.repository.exists? && @project.repository.root_ref - project_nav = [ - { label: "#{simple_sanitize(@project.name_with_namespace)} - Files", url: project_tree_path(@project, @ref || @project.repository.root_ref) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Network", url: project_network_path(@project, @ref || @project.repository.root_ref) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Graph", url: project_graph_path(@project, @ref || @project.repository.root_ref) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Issues", url: project_issues_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Merge Requests", url: project_merge_requests_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Milestones", url: project_milestones_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Snippets", url: project_snippets_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Team", url: project_team_index_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Wall", url: project_wall_path(@project) }, - { label: "#{simple_sanitize(@project.name_with_namespace)} - Wiki", url: project_wikis_path(@project) }, - ] - end - - [groups, projects, default_nav, project_nav, help_nav].flatten.to_json - end - def emoji_autocomplete_source # should be an array of strings # so to_s can be called, because it is sufficient and to_json is too slow diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb new file mode 100644 index 00000000000..33c5e4fb9db --- /dev/null +++ b/app/helpers/search_helper.rb @@ -0,0 +1,78 @@ +module SearchHelper + def search_autocomplete_source + return unless current_user + + [ + groups_autocomplete, + projects_autocomplete, + default_autocomplete, + project_autocomplete, + help_autocomplete + ].flatten.to_json + end + + private + + # Autocomplete results for various settings pages + def default_autocomplete + [ + { label: "My Profile settings", url: profile_path }, + { label: "My SSH Keys", url: profile_keys_path }, + { label: "My Dashboard", url: root_path }, + { label: "Admin Section", url: admin_root_path }, + ] + end + + # Autocomplete results for internal help pages + def help_autocomplete + [ + { label: "help: API Help", url: help_api_path }, + { label: "help: Markdown Help", url: help_markdown_path }, + { label: "help: Permissions Help", url: help_permissions_path }, + { label: "help: Public Access Help", url: help_public_access_path }, + { label: "help: Rake Tasks Help", url: help_raketasks_path }, + { label: "help: SSH Keys Help", url: help_ssh_path }, + { label: "help: System Hooks Help", url: help_system_hooks_path }, + { label: "help: Web Hooks Help", url: help_web_hooks_path }, + { label: "help: Workflow Help", url: help_workflow_path }, + ] + end + + # Autocomplete results for the current project, if it's defined + def project_autocomplete + if @project && @project.repository.exists? && @project.repository.root_ref + prefix = simple_sanitize(@project.name_with_namespace) + ref = @ref || @project.repository.root_ref + + [ + { label: "#{prefix} - Files", url: project_tree_path(@project, ref) }, + { label: "#{prefix} - Commits", url: project_commits_path(@project, ref) }, + { label: "#{prefix} - Network", url: project_network_path(@project, ref) }, + { label: "#{prefix} - Graph", url: project_graph_path(@project, ref) }, + { label: "#{prefix} - Issues", url: project_issues_path(@project) }, + { label: "#{prefix} - Merge Requests", url: project_merge_requests_path(@project) }, + { label: "#{prefix} - Milestones", url: project_milestones_path(@project) }, + { label: "#{prefix} - Snippets", url: project_snippets_path(@project) }, + { label: "#{prefix} - Team", url: project_team_index_path(@project) }, + { label: "#{prefix} - Wall", url: project_wall_path(@project) }, + { label: "#{prefix} - Wiki", url: project_wikis_path(@project) }, + ] + else + [] + end + end + + # Autocomplete results for the current user's groups + def groups_autocomplete + current_user.authorized_groups.map do |group| + { label: "group: #{simple_sanitize(group.name)}", url: group_path(group) } + end + end + + # Autocomplete results for the current user's projects + def projects_autocomplete + current_user.authorized_projects.map do |p| + { label: "project: #{simple_sanitize(p.name_with_namespace)}", url: project_path(p) } + end + end +end diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb new file mode 100644 index 00000000000..55b6b6b4dad --- /dev/null +++ b/spec/helpers/search_helper_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe SearchHelper do + # Override simple_sanitize for our testing purposes + def simple_sanitize(str) + str + end + + describe 'search_autocomplete_source' do + context "with no current user" do + before { stub!(:current_user).and_return(nil) } + + it "it returns nil" do + search_autocomplete_source.should be_nil + end + end + + context "with a user" do + let(:user) { create(:user) } + let(:result) { JSON.parse(search_autocomplete_source) } + + before do + stub!(:current_user).and_return(user) + end + + it "includes Help sections" do + result.select { |h| h['label'] =~ /^help:/ }.length.should == 9 + end + + it "includes default sections" do + result.count { |h| h['label'] =~ /^(My|Admin)\s/ }.should == 4 + end + + it "includes the user's groups" do + create(:group).add_owner(user) + result.count { |h| h['label'] =~ /^group:/ }.should == 1 + end + + it "includes the user's projects" do + create(:project, namespace: create(:namespace, owner: user)) + result.count { |h| h['label'] =~ /^project:/ }.should == 1 + end + + context "with a current project" do + before { @project = create(:project_with_code) } + + it "includes project-specific sections" do + result.count { |h| h['label'] =~ /^#{@project.name_with_namespace} - / }.should == 11 + end + + it "uses @ref in urls if defined" do + @ref = "foo_bar" + result.count { |h| h['url'] == project_tree_path(@project, @ref) }.should == 1 + end + end + + context "with no current project" do + it "does not include project-specific sections" do + result.count { |h| h['label'] =~ /Files$/ }.should == 0 + end + end + end + end +end -- cgit v1.2.3