Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Commit

Permalink
Finished Controller test and Helper Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bthuilot committed Jul 18, 2019
1 parent 97ab38a commit b916c7b
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 48 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# Wiki
# Pearl Wiki
Open source wiki software powered by [rails](https://github.com/rails/rails)

I started this project when I was looking for wiki software to use for a club I was a apart of and didn't like any of the options I found

Thus spawned this!

Checkout the live demo [here]()

## Getting started
*The wiki requires ruby versions >= 2.5.0*

To install first clone the source using
```bash
$ git clone https://github.com/bthuilot/PearlWiki
$ cd PearlWiki
```

Once inside the cloned directory, run
```bash
# Install Dependecys
$ gem install bundler && bundle install
# Set up and seed database
$ rake db:migrate
$ rake db:seed
# Start the server
rails server
```

## Code Status
[![Build Status](https://travis-ci.com/bthuilot/PearlWiki.svg?token=DwRwXcm2t95em7ygX8ov&branch=master)](https://travis-ci.com/bthuilot/PearlWiki)

## License
Pearl Wiki is released under the [MIT License]().
1 change: 0 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class CategoriesController < ApplicationController
include MarkdownHelper

before_action :set_category, only: [:edit, :update, :show, :destroy]
before_action :set_renderer, only: [:show]
before_action :get_all_categories

def new
Expand Down
1 change: 0 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class PostsController < ApplicationController
include MarkdownHelper

before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :set_renderer, only: [:show]
before_action :set_categories

def show
Expand Down
28 changes: 11 additions & 17 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
class SearchController < ApplicationController
include MarkdownHelper
include SearchHelper

before_action :get_all_categories
before_action :get_search_params
before_action :set_textonly_renderer

def search
@posts = Post.search(@search_term, @search_category_id)
@posts_length = @posts.length
@max_page = (@posts_length / RESULTS_PER_PAGE) + 1
if @page > 1
start_result = (@page - 1) * RESULTS_PER_PAGE
@posts = @posts[start_result...start_result + RESULTS_PER_PAGE]
end
@posts = get_search_results
get_search_page
end


private

RESULTS_PER_PAGE = 20

def get_all_categories
@categories = Category.all
end

def get_search_params
def get_search_results
if params[:search]
@search_term = params[:search][:search_term]
@search_category_id = /\A\d+\z/.match(params[:search][:search_category]) ? params[:search][:search_category].to_i : nil
@page = params[:page].to_i || 1
search_term = params[:search][:term]
search_category_id = /\A\d+\z/.match(params[:search][:category]) ? params[:search][:category].to_i : nil
end
Post.search(search_term, search_category_id)
end

def get_search_page
@page = params[:page] ? [params[:page].to_i, get_max_page].min : 1
end
end
1 change: 0 additions & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class StaticPagesController < ApplicationController
include MarkdownHelper

before_action :set_renderer, only: [:home, :about]
before_action :get_categories

before_action :about, only: [:edit_about, :update_about]
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'redcarpet/render_strip'
module MarkdownHelper
def set_renderer
@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
def markdown_renderer
Redcarpet::Markdown.new(Redcarpet::Render::HTML)
end

def set_textonly_renderer
@textonly = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
def textonly_renderer
Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
end
end
39 changes: 38 additions & 1 deletion app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
module SearchHelper
include MarkdownHelper

RESULTS_PER_PAGE = 20

def get_text_preview post
@textonly.render(post.contents[0...TEXT_LENGTH]).concat(post.contents.length > TEXT_LENGTH ? '...' : '')
contents = post.contents || ""
textonly_renderer.render(contents || "")[0...TEXT_LENGTH].concat(contents.length > TEXT_LENGTH ? '...' : '')
end

def get_max_page
max_page = (@posts.length / RESULTS_PER_PAGE)
@posts.length % RESULTS_PER_PAGE > 0 ? max_page + 1 : max_page
end

def has_left_arrow
@page > 1 and @page > 0
end

def has_right_arrow
@page < get_max_page and @page > 0
end

def get_lower_page
start = @page < 1 ? 1 : @page
[start - 4, 1].max
end

def get_higher_page
start = @page < 1 ? 1 : @page
[start + 4, get_max_page].min
end

def get_paginated_results
if @page > 0
start_result = (@page - 1) * RESULTS_PER_PAGE
@posts[start_result...start_result + RESULTS_PER_PAGE]
else
@posts
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/categories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% provide(:title, "Edit #{@category.name}") %>
<div class="row justify-content-center">
<div class="col-12">
<%= form_for @category, method: :post, url: update_categories_url(@category.name) do |form| %>
<%= form_for @category, method: :post, url: update_categories_url(@category) do |form| %>
<div class="form-group">
<%= form.label :name, for: 'category-name-input' %>
<%= form.text_field :name, class: 'form-control', id: 'category-name-input' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
</div>
<div class="row justify-content-center">
<div class="col-10">
<%= @markdown.render(h @category.homepage).html_safe %>
<%= markdown_renderer.render(h @category.homepage).html_safe %>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@

<!-- TODO Fix styling for dropdown -->
<%= form_for :search, method: :get, html: {class: 'form-inline my-2 my-lg-0 navbar-search-select'}, url: '/search' do |form| %>
<%= form.select :search_category, Category.all.collect {|c| [c.name, c.id]}, {include_blank: 'All Categories'}, {class: 'custom-select navbar-dark select-no-border'} %>
<%= form.select :category, Category.all.collect {|c| [c.name, c.id]}, {include_blank: 'All Categories'}, {class: 'custom-select navbar-dark select-no-border'} %>

<%= form.text_field :search_term, class: 'form-control mr-sm-2', placeholder: "Search" %>
<%= form.text_field :term, class: 'form-control mr-sm-2', placeholder: "Search" %>
<br>
<%= form.submit :search, class: 'btn btn-outline-success my-2 my-sm-0' %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="row justify-content-center align-items-center">
<div class="col-9">
<h1 class="post-title">
<a class="black-link no-underline-link" href="<%= categories_url(@post.category.name) %>">
<a class="black-link no-underline-link" href="<%= categories_url(@post.category) %>">
<%= @post.category.name %>
</a>
<span class="breadcrumb-post">
Expand All @@ -29,6 +29,6 @@
</div>
<div class="row justify-content-center">
<div class="col-10">
<%= @markdown.render(h @post.contents).html_safe %>
<%= markdown_renderer.render(h @post.contents).html_safe %>
</div>
</div>
23 changes: 12 additions & 11 deletions app/views/search/search.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<% provide(:title, "Search") %>
<%= form_for :search, method: :get, html: {class: ''}, url: '/search' do |form| %>
<div class="row">
<div class="col-12">
<div class="form-row">
<div class="form-group col-md-10 col-sm-12">
<%= form.text_field :search_term, class: "form-control", placeholder: 'Search' %>
<%= form.text_field :term, class: "form-control", placeholder: 'Search' %>
</div>
<div class="form-group col-md-2 col-sm-6">
<%= form.select :search_category, Category.all.collect {|c| [c.name, c.id]}, {include_blank: 'All Categories'}, {class: 'form-control custom-select'} %>
<%= form.select :category, Category.all.collect {|c| [c.name, c.id]}, {include_blank: 'All Categories'}, {class: 'form-control custom-select'} %>
</div>
</div>
</div>
Expand All @@ -17,18 +18,18 @@
</div>
<div class="col-12 text-center">
<div class="btn-group mr-2" role="group" aria-label="First group">
<% if @page > 1 %>
<%= button_tag(type: 'submit', class: "btn btn-light", name: "page", value: @page - 1) do %>
<% if has_left_arrow %>
<%= button_tag(type: 'submit', class: "prev-page-button btn btn-light", name: "page", value: @page - 1) do %>
<i class="material-icons">
keyboard_arrow_left
</i>
<% end %>
<% end %>
<% start = @page < 1 ? 1 : @page; ([start - 4, 1].max..[start + 4, @max_page - 1].min).each do |page| %>
<%= form.submit page, name: "page", value: page, class: "btn btn-light #{page == @page ? 'active' : ''} " %>
<% (get_lower_page..get_higher_page).each do |page| %>
<%= form.submit page, name: "page", value: page, class: "page-buttons btn btn-light #{page == @page ? 'active' : ''} " %>
<% end %>
<% if @page < @max_page - 1 %>
<%= button_tag(type: 'submit', class: "btn btn-light", name: "page", value: @page + 1) do %>
<% if has_right_arrow %>
<%= button_tag(type: 'submit', class: "next-page-button btn btn-light", name: "page", value: @page + 1) do %>
<i class="material-icons">
keyboard_arrow_right
</i>
Expand All @@ -39,16 +40,16 @@
</div>
<div class="row justify-content-center top-buffer">
<div class="col-12 text-center">
<%= button_tag(type: 'submit', class: "btn btn-light #{ @page < 1 ? 'active' : ''}", name: "page", value: -1) do %>
All Results (<%= @posts_length %>)
<%= button_tag(type: 'submit', class: "all-results-button btn btn-light #{ @page < 1 ? 'active' : ''}", name: "page", value: -1) do %>
All Results (<%= @posts.length %>)
<% end %>
</div>
</div>
<% end %>
<div class="row large-top-buffer">
<div class="col-12">
<div class="list-group">
<% @posts.each do |post| %>
<% get_paginated_results.each do |post| %>
<a href="<%= post_url(post) %>" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><%= post.title %></h5>
Expand Down
2 changes: 1 addition & 1 deletion app/views/static_pages/about.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
</div>
<div class="row justify-content-center">
<div class="col-10">
<%= @markdown.render(h @about.contents).html_safe %>
<%= markdown_renderer.render(h @about.contents).html_safe %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
</div>
<div class="row justify-content-center">
<div class="col-10">
<%= @markdown.render(h @home.contents).html_safe %>
<%= markdown_renderer.render(h @home.contents).html_safe %>
</div>
</div>
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Rails.application.routes.draw do

get 'search/search_all'
get 'search/search_category'
# Static Pages
root to: 'static_pages#home'
get '/home' => 'static_pages#home', as: 'home'
Expand Down
75 changes: 75 additions & 0 deletions test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,80 @@
require 'test_helper'

class SearchControllerTest < ActionDispatch::IntegrationTest
test "Should get search page" do
get search_url
assert_response :success
assert_equal 32, @controller.instance_variable_get(:@posts).length
assert_select ".list-group-item", {count: 20}
assert_equal 1, @controller.instance_variable_get(:@page)
assert_select ".all-results-button", "All Results (32)"
assert_select ".next-page-button", "keyboard_arrow_right"
assert_select ".prev-page-button", {count: 0}
assert_select ".page-buttons", {count: 2}
assert_select ".list-group-item", {count: 20}
assert_select ".page-buttons.active", {count: 1}
end

test "Should get all results" do
get search_url, params: {page: -1}
assert_response :success
assert_equal -1, @controller.instance_variable_get(:@page)
assert_select ".prev-page-button", {count: 0}
assert_select ".next-page-button", {count: 0}
assert_select ".page-buttons.active", {count: 0}
assert_select ".list-group-item", {count: 32}
assert_select ".all-results-button.active", "All Results (32)"
end

test "Should get page 2" do
get search_url, params: {page: 2}
assert_response :success
assert_equal 2, @controller.instance_variable_get(:@page)
assert_select ".page-buttons.active", {value: 2}
assert_select ".prev-page-button", {count: 1}
assert_select ".next-page-button", {count: 0}
assert_select ".list-group-item", {count: 12}
end

test "Should only get posts in dev ops category" do
get search_url, params: {search: {category: categories(:dev_ops).id}}
assert_response :success
assert_equal 1, @controller.instance_variable_get(:@page)
assert_select ".page-buttons.active", {value: 1}
assert_select ".prev-page-button", {count: 0}
assert_select ".next-page-button", {count: 0}
assert_select ".list-group-item", {count: 20}
assert_select ".list-group-item" do
assert_select "div small", "Category: Developer Operations Category"
end
end

test "Should only get posts with 0 in title" do
get search_url, params: {search: {term: "0"}}
assert_response :success
assert_equal 1, @controller.instance_variable_get(:@page)
assert_select ".page-buttons.active", {value: 1}
assert_select ".prev-page-button", {count: 0}
assert_select ".next-page-button", {count: 0}
assert_select ".list-group-item", {count: 3}
assert_select ".list-group-item" do
assert_select "h5", /.+0/
end
end

test "Should only get posts with 0 in title and in dev ops category" do
get search_url, params: {search: {category: categories(:dev_ops).id, term: "0"}}
assert_response :success
assert_equal 1, @controller.instance_variable_get(:@page)
assert_select ".page-buttons.active", {value: 1}
assert_select ".prev-page-button", {count: 0}
assert_select ".next-page-button", {count: 0}
assert_select ".list-group-item", {count: 2}
assert_select ".list-group-item" do
assert_select "h5", /.+0/
end
assert_select ".list-group-item" do
assert_select "div small", "Category: Developer Operations Category"
end
end
end
2 changes: 1 addition & 1 deletion test/fixtures/categories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ finance:
homepage: This is the category for all *finance* pages

dev_ops:
name: Developer Opeartions Category
name: Developer Operations Category
homepage: This is the category for all *dev ops* pages

admin:
Expand Down
Loading

0 comments on commit b916c7b

Please sign in to comment.