hpi-schuelerklub/workshop-portal

View on GitHub
app/helpers/application_helper.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'redcarpet'
require 'redcarpet/render_strip'

# Very barebone "markdown" renderer that ignores all elements
# except paragraphs
class MarkdownRenderTruncate < Redcarpet::Render::Base
  def paragraph(text)
    CGI.escapeHTML(text) + ' '
  end

  def link(_link, _title, content)
    CGI.escapeHTML(content)
  end
end

module ApplicationHelper
  def menu_items
    (menu_item t(:events, scope: 'navbar'), events_path) +
      request_menu_item
  end

  def request_menu_item
    if can? :index, Request
      (menu_item t(:requests, scope: 'navbar'), requests_path)
    else
      (menu_item t(:new_request, scope: 'navbar'), new_request_path)
    end
  end

  # Render the given string as markdown
  #
  # @param text String containing markdown
  # @param truncatable boolean leave only paragraph elements
  # @return string html_safe rendered markdown
  def markdown(text, truncatable = false)
    renderer = truncatable ?
      MarkdownRenderTruncate.new :
      Redcarpet::Render::HTML.new(filter_html: true,
                                  hard_wrap: true,
                                  tables: true,
                                  strikethrough: true,
                                  link_attributes: { rel: 'nofollow', target: '_blank' },
                                  space_after_headers: true,
                                  fenced_code_blocks: true)

    Redcarpet::Markdown.new(renderer, autolink: true,
                                      superscript: true,
                                      disable_indented_code_blocks: true).render(text).html_safe
  end

  def dropdown_items
    o = ''
    # everyone gets settings
    o << (menu_item t(:settings, scope: 'navbar'), edit_user_registration_path)
    # everyone gets their profile, if it exists
    unless current_user.profile.present?
      o << (menu_item t(:create_profile, scope: 'navbar'), new_profile_path)
    end
    # pupils get their applications
    if current_user.role == 'pupil'
      o << (menu_item t(:my_events, scope: 'navbar'), application_letters_path)
    end
    # admins get user management
    if current_user.role == 'admin' || current_user.role == 'organizer'
      o << (menu_item t(:user_management, scope: 'navbar'), users_path)
    end
    # everyone gets logout
    o << (menu_item t(:logout, scope: 'navbar'), destroy_user_session_path, method: :delete)

    o.html_safe
  end

  def resource_name
    :user
  end

  def resource
    @user ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end