Skip to content

Commit

Permalink
[WIP] adds Guidelines
Browse files Browse the repository at this point in the history
Closes #42

This PR handles adding guideline resources are described in #42.

To follow the requested URL structure (comment:
#42 (comment)),
an default locale of `en` is introduced and used as a scope in routes,
to ensure that `/en/guidelines` and `/guidelines` route default to the
same resource

Because `guidelines` are ephemeral, a presenter is used to display
initial guidelines that have guideline categories that, in turn, have
many articles. Presenters also take care of fetching and rendering
metadata stored in YML files

TODO:
- add markdown parsing / rendering based on https://github.com/panvol/pandemic-volunteers/pull/44/files#diff-e55a6c615d18aeb75e0610b835b16658
- make methods such as `GuidelineCategory.extract_slug` etc., metadata
  file fetching and loading, and loading dirs via `Pathname` reusable.
  This is because GuidelineCategory and GuidelineArticle will be sharing
  90% of their methods. GuidelineArticle will also need to be able to
  load its own metadata
- move `GuidelinesPresenter` to `app/models` since `GuidelineCategory`
  is very similar and acts as a stand-in for a model
- add designs
  • Loading branch information
dimanyc committed Apr 17, 2020
1 parent 2e4562b commit 6d445b2
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
class ApplicationController < ActionController::Base
before_action :set_locale

def default_url_options
{ host: ENV["DOMAIN"] || "localhost:3000" }
{
host: ENV["DOMAIN"] || "localhost:3000",
locale: I18n.locale,
}
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end

def locale
default_url_options[:locale]
end
end
9 changes: 9 additions & 0 deletions app/controllers/guideline_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GuidelineCategoriesController < ApplicationController
def index
@category =
end

def fetch_categories
GuidelinesPresenter.new(locale).categories
end
end
5 changes: 5 additions & 0 deletions app/controllers/guidelines_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class GuidelinesController < ApplicationController
def index
@guidelines = GuidelinesPresenter.new(locale)
end
end
24 changes: 24 additions & 0 deletions app/models/guideline_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class GuidelineCategory
attr_reader :slug, :position, :title, :description, :svg_filename, :articles
def initialize(dir_path, metadata)
@dir_path = dir_path
@slug = extract_slug
@position = extract_position
@title = metadata[:title]
@description = metadata[:description]
@svg_filename = metadata[:svg_filename]
@articles = []
end

private

def extract_slug
dir_path.match(/\d+-(\w.*)$/)[1]
end

def extract_position
dir_path.match(/(\d+)(?!.*\d)/)[1]&.to_i
end

attr_reader :dir_path
end
45 changes: 45 additions & 0 deletions app/presenters/guidelines_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class GuidelinesPresenter
ROOT_PATH = Rails.root.join("lib/markdown").freeze
METADATA_FILE_NAME = "metadata.yml".freeze

def initialize(locale)
@locale = locale
end

def categories
guideline_dir_paths.map do |dir_path|
GuidelineCategory.new(
dir_path.to_s,
metadata(dir_path),
)
end
end

def title
metadata(guidelines_path)[:title]
end

def description
metadata(guidelines_path)[:description]
end

def metadata(dir_path)
YAML::load_file(File.join(dir_path, METADATA_FILE_NAME)).symbolize_keys!
end

private

def guideline_dir_paths
Pathname.new(guidelines_path).children.select(&:directory?)
end

def guidelines_path
root_path("guidelines")
end

def root_path(dir_name)
"#{ROOT_PATH}/#{locale}/#{dir_name}"
end

attr_reader :locale
end
5 changes: 5 additions & 0 deletions app/views/guideline_categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= render partial: "shared/guideline_index", locals: {
title: @category.title,
description: @category.description,
children: @categories.articles,
} %>
5 changes: 5 additions & 0 deletions app/views/guidelines/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= render partial: "shared/guideline_index", locals: {
title: @guidelines.title,
description: @guidelines.description,
children: @guidelines.categories,
} %>
25 changes: 25 additions & 0 deletions app/views/shared/_guideline_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= link_to "/guidelines/#{slug}" do %>
<div class="card shadow">

<div class="card-body bg-light">
<div class="row my-5 mx-0">
<div class="col-md-4 d-flex text-center justify-content-center">
<%= inline_svg_tag "graphics/home/#{svg_filename}.svg",
size: "200",
class: "m-auto"
%>
</div>
<div class="col-md-8">
<p class="cover-lead font-weight-bold">
<span class="mr-2 align-middle">
<%= title %>
</span>
</p>
<p>
<%= description %>
</p>
</div>
</div>
</div>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/shared/_guideline_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="text-center container py-5">
<p class="section-title font-weight-bold">
<%= title %>
</p>
<p class="cover-lead">
<%= description %>
</p>
<% children.each do |child| %>
<%= render partial: "shared/guideline_card", locals: {
title: child.title,
description: child.description,
slug: child.slug,
svg_filename: child.svg_filename,
} %>
<% end %>
</div>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
get '/hospitals', to: 'hospitals#index'

get '/privacy', to: 'pages#privacy'

scope "(:locale)", locale: /en|ja/ do
resources :guidelines, only: %I(index)
get "/guidelines/:category_name", to: "guideline_categories#index"
end
end
12 changes: 12 additions & 0 deletions lib/markdown/en/guidelines/00-elderly-crews/00-role-definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Role Definition
> What elderly crews is all about.
Elderly crews are crucial parts of the Pandemic Volunteers team who go around to retirement homes and elderly individuals’ homes to check up on them and bring them essential needs such as food and clothes so that they do not have to leave the home and risk catching COVID-19.

This is an especially important job because it targets the elderly, those who are at most risk of dying from or hurting themselves permanently if they do catch the virus, so volunteers are supposed to be even more careful than normal because of their added vulnerabilities.

Volunteers must also know how to reach out to their local elderly individuals, how to sterilize groceries and PPE, nonviolent communication and have to avoid the most amount of contact possible. If one is interested in this position and does not think they have the required skills, they can always follow the following knowledgebase articles to learn more and hopefully be qualified enough to be able to carry out such a job efficiently, and, more importantly, safely.

Elderly crew volunteers have the choice of being assigned targets by Coordination Crew meetings, and joining an action crew to listen in and know what those targets are, or they can find targets individually and help those in need as soon as they know about them.

They must also remember not to treat elderly individuals like children, or patronize them in any way: they may not be necessarily dependent on outside groups for support most of the time, and in a time of crisis such as this one it is only right that they do what is best for their health without any judgment.
3 changes: 3 additions & 0 deletions lib/markdown/en/guidelines/00-elderly-crews/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Elderly Crews
description: Guidelines for elderly crews.
svg_filename: undraw_collab_8oes_modified
3 changes: 3 additions & 0 deletions lib/markdown/en/guidelines/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Guidelines
description: Pandemic Volunteers guidelines
svg_filename: undraw_reading_0re1
70 changes: 70 additions & 0 deletions spec/models/guideline_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "rails_helper"

RSpec.describe GuidelineCategory do
it "returns a slug" do
dir_path = "/foo/bar/00-bar-baz"
presenter = described_class.new(dir_path, {})
expect(presenter.slug).to eq("bar-baz")
end

it "returns a position" do
dir_path = "/foo/bar/00-bar-baz"
presenter = described_class.new(dir_path, {})
expect(presenter.position).to eq(0)
end

it "returns a title" do
dir_path = "/foo/bar/00-bar-baz"

metadata = {
title: "foo",
description: "bar",
svg_filename: "baz",
}

presenter = described_class.new(dir_path, metadata)
expect(presenter.title).to eq("foo")
end

it "returns a description" do
dir_path =
"/pandemic-volunteers/lib/markdown/en/guidelines/00-elderly-crews"

metadata = {
title: "foo",
description: "bar",
svg_filename: "baz",
}

presenter = described_class.new(dir_path, metadata)
expect(presenter.description).to eq("bar")
end

it "returns an svg_filename" do
dir_path = "/foo/bar/00-bar-baz"

metadata = {
title: "foo",
description: "bar",
svg_filename: "baz",
}

presenter = described_class.new(dir_path, metadata)
expect(presenter.svg_filename).to eq("baz")
end

it "returns articles" do
pending "TODO: add articles"
dir_path =
"/pandemic-volunteers/lib/markdown/en/guidelines/00-elderly-crews"

metadata = {
title: "foo",
description: "bar",
svg_filename: "baz",
}

presenter = described_class.new(dir_path, metadata)
expect(presenter.articles).not_to be_empty
end
end
34 changes: 34 additions & 0 deletions spec/presenters/guidelines_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "rails_helper"

RSpec.describe GuidelinesPresenter do
it "returns a list of guidelines based on locale" do
expect(presenter.categories).not_to be_empty
end

it "returns a list of guidelines with slugs" do
expect(presenter.categories.first.slug).not_to be_empty
end

it "parses metadata" do
allow(GuidelineCategory).to receive(:new)
presenter.categories
expect(GuidelineCategory).to have_received(:new).with(
instance_of(String),
instance_of(Hash),
)
end

it "returns a title" do
expect(presenter.title).not_to be_empty
end

it "returns a description" do
expect(presenter.description).not_to be_empty
end

private

def presenter
described_class.new(:en)
end
end
27 changes: 27 additions & 0 deletions spec/requests/guideline_categories_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

RSpec.describe "/guidelines", type: :request do
let(:locale) { :en }
it "returns a 200 " do
get "/#{locale}/guidelines"

expect(response.status).to eq(200)
end

it "returns page metadata" do
locale = :en
presenter = GuidelinesPresenter.new(locale)
get "/#{locale}/guidelines"

expect(response.body).to include presenter.title
expect(response.body).to include presenter.description
end

it "returns a list of guidelines" do
locale = :en
categories = GuidelinesPresenter.new(locale).categories
get "/#{locale}/guidelines"

expect(response.body).to include categories.first.title
end
end

0 comments on commit 6d445b2

Please sign in to comment.