Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP docs searchable #637

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class SearchController < ApplicationController

skip_before_action :verify_authenticity_token, :only => [:show]

def index
@data = []

@query = params[:query]

@data = Search.find_word(@query)

respond_to do |format|
format.html
end

end
end
31 changes: 31 additions & 0 deletions app/models/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Search
class << self
def all_documents
mdfiles = File.join("**", "*.md.erb")
Dir.glob(mdfiles)
end

def make_link_pretty(doc)
remove_page = "pages/"
remove_file_tag = ".md.erb"
doc = doc.sub(remove_page, '')
doc.chomp(remove_file_tag)
end

def find_word(query)
data = []
docs = Search.all_documents
docs.each do |doc|
File.open(doc) do |f|
f.any? do |line|
if line.include?(query)
link = make_link_pretty(doc)
data << {:link => link, :content => line}
end
end
end
end
return data
end
end
end
7 changes: 7 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
<div class="Docs__page-container__inner PageContainer">

<nav class="Docs__nav">
<form method="get" action="/search" class="query">
<div class="FormField">
<input autofocus="autofocus" class="input" type="query" name="query">
<button class="button" type="submit">Search</button>
</div>
</form>
<br/>
<% if request.url.include?('/docs/tutorials') %>
<p class="Docs__nav__section-heading">Tutorials</p>
<ul class="Docs__nav__sub-nav">
Expand Down
11 changes: 11 additions & 0 deletions app/views/search/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1> Search Results <h1>

<h2>Returning results for <%= @query %><h2>
<table>
<% @data.each do |result| %>
<tr>
<td><%= link_to result[:link], docs_page_path(result[:link]) %></td>
<td><%= result[:content] %></td>
</tr>
<% end %>
<table>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@

# Take us straight to the docs when running standalone
root to: redirect("/docs")

get '/search' => 'search#index', as: :search
end
30 changes: 30 additions & 0 deletions spec/models/search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rails_helper'

RSpec.describe Search, type: :model do
context 'returns all documents from pages directory' do
it 'expect v2.md.erb to be returned' do
expect(Search.all_documents).to include("pages/agent/v2.md.erb")
end

it 'gets all 130 documents in pages directory' do
expect(Search.all_documents.count).to eq(130)
end

it 'makes sure there are documents in pages directory' do
expect(Search.all_documents.count).to_not eq(0)
end
end

context 'makes url pretty' do
let(:doc) { 'pages/example.md.erb' }
it 'makes example link pretty' do
expect(Search.make_link_pretty(doc)).to eq('example')
end
end

context 'return results for query' do
it 'returns 6 results for hello' do
expect(Search.find_word('hello').count).to eq(6)
end
end
end