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

Commit

Permalink
feat: allow Markdown in consultations and questions' descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Feb 19, 2023
1 parent eadb245 commit 3b7bc2c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem 'puma', '~> 6.0'
gem 'pundit', '~> 2.3'
gem 'rails', '~> 7.0'
gem 'rails-i18n', '~> 7.0'
gem 'redcarpet', '~> 3.6'
gem 'redis', '~> 5.0'
gem 'sentry-rails', '~> 5.7'
gem 'sentry-ruby', '~> 5.7'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
redcarpet (3.6.0)
redis (5.0.6)
redis-client (>= 0.9.0)
redis-client (0.12.1)
Expand Down Expand Up @@ -364,6 +365,7 @@ DEPENDENCIES
pundit (~> 2.3)
rails (~> 7.0)
rails-i18n (~> 7.0)
redcarpet (~> 3.6)
redis (~> 5.0)
rubocop (~> 1.45)
rubocop-minitest (~> 0.27)
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ main {
background-color: rgba($white, var(--bs-bg-opacity));
}

td > p {
margin-bottom: 0;
}

footer {
--bs-bg-opacity: 1;

Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module ApplicationHelper
def markdown(text, plain_text: false)
MarkdownService.render(text, plain_text:)
end
end
4 changes: 4 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def voted?(token)
receipts.exists?(token:)
end

def short_description
description.split("\n").first.strip
end

def position
weight + 1
end
Expand Down
21 changes: 21 additions & 0 deletions app/services/markdown_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'redcarpet/render_strip'

class MarkdownService < Redcarpet::Render::HTML
OPTIONS = {
filter_html: true,
no_images: true,
no_styles: true
}.freeze

def self.render(text, plain_text: false)
if plain_text
markdown = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
else
markdown = Redcarpet::Markdown.new(self, **OPTIONS)
end

markdown.render(text).html_safe
end
end
2 changes: 1 addition & 1 deletion app/views/application/_questions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<% @consultation.questions.order(:id).each do |question| %>
<tr>
<td><%= question.id %></td>
<td><%= question.description %></td>
<td><%= markdown question.short_description, plain_text: true %></td>
<td><%= question.status %></td>
<td><%= link_to t('application.edit'), edit_consultation_question_url(@consultation, question) %></td>
<% if @consultation.synchronous? %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/consultations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2><%= @consultation.title %></h2>
<div class="mb-3">
<%= raw(@consultation.description) %>
<%= markdown @consultation.description %>
</div>
<%= render @consultation.status %>
2 changes: 1 addition & 1 deletion app/views/question_groups/_questions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tr>
<td scope="row"><%= f.check_box :question_ids, { multiple: true }, question.id, nil %></td>
<td><%= question.id %></td>
<td><%= question.description %></td>
<td><%= markdown question.short_description, plain_text: true %></td>
</tr>
<% end %>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions app/views/questions/booth/_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= form_with url: { controller: :votes, action: :create }, local: true do |f| %>
<table class="table">
<table class="table table-hover">
<tbody>
<% (@question.group.random_order ? @question.group.questions.shuffle : @question.group.questions).each do |question| %>
<tr>
<td><%= question.description %></td>
<td><%= markdown question.description %></td>
<% question.options.each do |option| %>
<td>
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/booth/_single.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_with url: { controller: :votes, action: :create }, local: true do |f| %>
<table class="table table-hover table-borderless">
<table class="table table-hover">
<tbody>
<% @question.options.each do |option| %>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<% if @question.group.present? %>
<% if @question.group.description %>
<div class="mb-3">
<%= raw(@question.group.description) %>
<%= markdown @question.group.description %>
</div>
<% end %>
<% else %>
<% if @question.description %>
<div class="mb-3">
<%= raw(@question.description) %>
<%= markdown @question.description %>
</div>
<% end %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/questions/tally.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h2><%= @consultation.title %></h2>
<% if @question.description %>
<div class="mb-3">
<%= raw(@question.description) %>
<%= markdown @question.short_description, plain_text: true %>
</div>
<% end %>
<%= render "questions/ballot/#{@consultation.ballot}" %>
Expand Down

0 comments on commit 3b7bc2c

Please sign in to comment.