Skip to content

Commit

Permalink
Descriptions can be added to reports that support markdown formatting…
Browse files Browse the repository at this point in the history
…, addresses #9.
  • Loading branch information
remomueller committed Aug 10, 2019
1 parent 3aaaac0 commit 7a2a1e5
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Enhancements
- **Report Changes**
- Report pages without any reports now display a message
- Descriptions can be added to reports that support markdown formatting

## 10.0.0 (August 4, 2019)

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem "jquery-ui-rails", "~> 6.0.1"
gem "kaminari", "~> 1.1.1"
gem "mini_magick", "~> 4.9.2"
gem "pg_search", "~> 2.3.0"
gem "redcarpet", "~> 3.5.0"

# Rails defaults
gem "coffee-rails", "~> 4.2"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def find_report_or_redirect
def report_params
params.require(:report).permit(
:project_id, :report_type, :name, :header_label, :sites_enabled,
:archived, :filter_expression, :group_expression,
:archived, :filter_expression, :group_expression, :description,
row_hashes: [
:report_row_id, :label, :expression, :muted
]
Expand Down
75 changes: 75 additions & 0 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

# Renders text written with markdown.
module MarkdownHelper
def simple_markdown(text, target_blank: true, table_class: "", allow_links: true, allow_images: true, allow_tables: true, allow_lists: true, pretend_links: false)
result = text.to_s
result = replace_numbers_with_ascii(result) unless allow_lists
result = redcarpet_markdown.render(result)
result = result.encode("UTF-16", undef: :replace, invalid: :replace, replace: "").encode("UTF-8")
result = add_table_class(result, table_class) unless table_class.blank?
result = remove_links(result) unless allow_links
result = pretend_links(result) if pretend_links
if allow_images
result = wrap_images(result)
else
result = remove_images(result)
end
result = remove_tables(result) unless allow_tables
result = target_link_as_blank(result) if target_blank
result.html_safe
end

def target_link_as_blank(text)
text.to_s.gsub(/<a(.*?)>/m, "<a\\1 target=\"_blank\">").html_safe
end

def remove_links(text)
text.to_s.gsub(%r{<a[^>]*? href="(.*?)">(.*?)</a>}m, "\\2")
end

def pretend_links(text)
text.to_s.gsub(%r{<a[^>]*? href="(.*?)">(.*?)</a>}m, "<span class=\"text-primary\">\\2</span>")
end

def wrap_images(text)
text.to_s.gsub(/(<img.*?>)/m, "<div class=\"img-zoom-message\">\\1</div>")
end

def remove_images(text)
text.to_s.gsub(/<img src="(.*?)"(.*?)>/m, "<div>\\1</div>")
end

def remove_tables(text)
text.to_s.gsub(%r{<table(.*?)>(.*?)</table>}m, "")
end

def add_table_class(text, table_class)
text.to_s.gsub(/<table>/m, "<table class=\"#{table_class}\">").html_safe
end

def replace_numbers_with_ascii(text)
text.gsub(/^[ \t]*(\d)/) { |m| ascii_number($1) }
end

def ascii_number(number)
"&##{number.to_i + 48};"
end

def redcarpet_markdown
Redcarpet::Markdown.new(
Redcarpet::Render::HTML,
no_intra_emphasis: true,
fenced_code_blocks: true,
autolink: true,
strikethrough: true,
superscript: true,
tables: true,
lax_spacing: true,
space_after_headers: true,
underline: true,
highlight: true,
footnotes: true
)
end
end
1 change: 1 addition & 0 deletions app/views/internal/report_page.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- if @reports.present?
- @reports.each do |report|
= render "reports/description", report: report if report.description.present?
= render "reports/chart", report: report if report.chart?
= render "reports/table", report: report if report.table?
= render "reports/grades", report: report if report.grades?
Expand Down
2 changes: 2 additions & 0 deletions app/views/reports/_description.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dashboard-container.dashboard-container-border-accent
= simple_markdown report.description
1 change: 1 addition & 0 deletions app/views/reports/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
= render "forms/horizontal/select", form: form, object: report, key: :project_id, options: Project.order(:name).pluck(:name, :id), include_blank: "--Select a project--"
= render "forms/horizontal/select", form: form, object: report, key: :report_type, options: Report::REPORT_TYPES, include_blank: "--Select a report type--", id: true
= render "forms/horizontal/text_field", form: form, object: report, key: :name
= render "forms/horizontal/text_area", form: form, object: report, key: :description, rows: 7, help_text: "Supports markdown formatting."
= render "forms/horizontal/check_box", form: form, object: report, key: :sites_enabled, help_text: "Enable to include breakdown by site."
= render "forms/horizontal/check_box", form: form, object: report, key: :archived

Expand Down
1 change: 1 addition & 0 deletions app/views/reports/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
= render "forms/horizontal/show/text", object: @report, key: :header_label
= render "forms/horizontal/show/generic", title: "Rows", content: render("report_rows/index", report: @report)

= render "reports/description", report: @report if @report.description.present?
= render "reports/chart", report: @report if @report.chart?
= render "reports/table", report: @report if @report.table?
= render "reports/grades", report: @report if @report.grades?
5 changes: 5 additions & 0 deletions db/migrate/20190810155952_add_description_to_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDescriptionToReports < ActiveRecord::Migration[6.0]
def change
add_column :reports, :description, :text
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_23_212346) do
ActiveRecord::Schema.define(version: 2019_08_10_155952) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -148,6 +148,7 @@
t.text "filter_expression"
t.text "group_expression"
t.jsonb "data"
t.text "description"
t.index ["archived"], name: "index_reports_on_archived"
t.index ["project_id"], name: "index_reports_on_project_id"
end
Expand Down
1 change: 1 addition & 0 deletions test/controllers/reports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def report_params
project_id: projects(:one).id,
report_type: "expressions_by_site",
name: "New Report",
description: "This is the report description",
header_label: "Diagnosis",
archived: "0",
row_hashes: [
Expand Down

0 comments on commit 7a2a1e5

Please sign in to comment.