Module: ApplicationFormattersHelper

Includes:
ApplicationHTMLFormattersHelper
Included in:
ApplicationHelper, Course::Assessment::Submission::StatisticsDownloadService
Defined in:
app/helpers/application_formatters_helper.rb

Overview

Helpers for formatting objects/values on the application.

Constant Summary

Constants included from ApplicationHTMLFormattersHelper

ApplicationHTMLFormattersHelper::DefaultCodePipelineOptions, ApplicationHTMLFormattersHelper::DefaultHTMLPipeline, ApplicationHTMLFormattersHelper::DefaultHTMLPipelineOptions, ApplicationHTMLFormattersHelper::DefaultPipeline, ApplicationHTMLFormattersHelper::DefaultPipelineOptions, ApplicationHTMLFormattersHelper::HTMLSanitizerOptions, ApplicationHTMLFormattersHelper::HTMLSanitizerPipeline, ApplicationHTMLFormattersHelper::MAX_CODE_LINES, ApplicationHTMLFormattersHelper::MAX_CODE_SIZE, ApplicationHTMLFormattersHelper::SANITIZATION_FILTER_WHITELIST, ApplicationHTMLFormattersHelper::VIDEO_URL_WHITELIST, ApplicationHTMLFormattersHelper::VIDEO_WHITELIST_TRANSFORMER

Instance Method Summary collapse

Methods included from ApplicationHTMLFormattersHelper

#default_code_pipeline, #format_code_block, #format_html, #highlight_code_block, #sanitize

Instance Method Details

#display_topic(topic, options = {}) ⇒ Object

Renders the given topic.

Parameters:

  • topic (Course::Discussion::Topic)

    The topic to display.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :post_partial (String)

    The path to the post partial

  • :post_locals (Hash)

    The locals to be passed to the post partial

  • :footer (String)

    The path to the footer partial, this is normally used for add a reply form below all the posts.

  • :read_marks (Boolean)

    Set to true to eager load read marks for posts.

  • :with_votes (Boolean)

    Set to true to display votes.


77
78
79
80
81
# File 'app/helpers/application_formatters_helper.rb', line 77

def display_topic(topic, options = {})
  render partial: 'course/discussion/topic',
         object: topic,
         locals: options
end

#display_user(user) ⇒ String

Formats the given User as a user-visible string.

Parameters:

  • user (User)

    The User to display.

Returns:

  • (String)

    The user-visible string to represent the User, suitable for rendering as output.


30
31
32
# File 'app/helpers/application_formatters_helper.rb', line 30

def display_user(user)
  user&.name
end

#display_user_image(user) ⇒ String

Displays the given user's image.

Parameters:

  • user (User)

    The user to display

Returns:

  • (String)

    A HTML fragment containing the image to display for the user.


38
39
40
41
42
43
44
45
46
# File 'app/helpers/application_formatters_helper.rb', line 38

def display_user_image(user)
  (:span, class: ['image']) do
    if user.nil? || user.profile_photo.medium.url.nil?
      image_tag('user_silhouette.svg')
    else
      image_tag(user.profile_photo.medium.url)
    end
  end
end

#draft_class(item) ⇒ Array<String>

A helper for generating CSS classes, based on the published status of the item.

Parameters:

  • item (ActiveRecord::Base)

    An ActiveRecord object which has a draft field.

Returns:

  • (Array<String>)

    An array of CSS classes applicable for the provided item.


148
149
150
151
152
153
154
# File 'app/helpers/application_formatters_helper.rb', line 148

def draft_class(item)
  if item.published?
    []
  else
    ['draft']
  end
end

#draft_message(item) ⇒ String?

A helper for retrieving the title of a published item's status.

Parameters:

  • item (ActiveRecord::Base)

    An ActiveRecord object which has a published field.

Returns:

  • (String)

    A translated string representing the status of the item.

  • (nil)

    If the item is published.


161
162
163
# File 'app/helpers/application_formatters_helper.rb', line 161

def draft_message(item)
  t('common.draft') unless item.published?
end

#format_block_text(text) ⇒ String

Formats the given user-input string. The string is assumed not to contain HTML markup, and will be processed for simple formatting like newlines using the Rails +simple_format+ helper.

This will treat the given text as a block element.

Parameters:

  • text (String)

    The text to display.

Returns:

  • (String)

13
14
15
# File 'app/helpers/application_formatters_helper.rb', line 13

def format_block_text(text)
  simple_format(html_escape(text), {}, sanitize: false)
end

#format_boolean(bool) ⇒ String

Changes boolean values (True/False) into readable forms (Yes/No).

Parameters:

  • bool (Boolean|nil)

Returns:

  • (String)

    A readable form of true and false, Yes and No.


181
182
183
# File 'app/helpers/application_formatters_helper.rb', line 181

def format_boolean(bool)
  bool ? t('common.truthy') : t('common.falsey')
end

#format_datetime(date, format = :long, user: nil) ⇒ String

Format the given datetime

Parameters:

  • date (DateTime)

    The datetime to be formatted

  • format (Symbol) (defaults to: :long)

    The output format. Use Ruby's defaults or see above for some predefined formats. e.g. :long => "December 04, 2007 00:00" :short => "04 Dec 00:00" :date_only_long => "December 04, 2007" :date_only_short => "04 Dec"

Returns:

  • (String)

    the formatted datetime string


98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/application_formatters_helper.rb', line 98

def format_datetime(date, format = :long, user: nil)
  user ||= respond_to?(:current_user) ? current_user : nil
  user_zone = user&.time_zone || Application.config.x.default_user_time_zone
  # TODO: Fix the query. This is a workaround to display the time in the correct zone, there are
  # places where datetimes are directly fetched from db and skipped AR, which result in incorrect
  # time zone.
  date = date.in_time_zone(user_zone) if date.zone != user_zone

  date.to_formatted_s(format)
end

#format_duration(total_seconds) ⇒ Object

Returns the duration in the format of "HH:MM:SS", eg 04H05M11S

Returns:

  • the duration in the format of "HH:MM:SS", eg 04H05M11S


110
111
112
113
114
115
# File 'app/helpers/application_formatters_helper.rb', line 110

def format_duration(total_seconds)
  seconds = total_seconds % 60
  minutes = (total_seconds / 60) % 60
  hours = total_seconds / (60 * 60)
  format('%02dH%02dM%02dS', hours, minutes, seconds)
end

#format_inline_text(text) ⇒ String

Formats the given user-input string. The string is assumed not to contain HTML markup.

Parameters:

  • text (String)

    The text to display.

Returns:

  • (String)

21
22
23
# File 'app/helpers/application_formatters_helper.rb', line 21

def format_inline_text(text)
  html_escape(text)
end

Links to the given User.

Parameters:

  • user (User)

    The User to display.

  • options (Hash) (defaults to: {})

    The options to pass to +link_to+

Yields:

  • The user will be yielded to the provided block, and the block can override the display of the User.

Yield Parameters:

  • user (User)

    The user to display.

Returns:

  • (String)

    The user-visible string, including embedded HTML which will display the string within a link to bring to the User page.


57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/application_formatters_helper.rb', line 57

def link_to_user(user, options = {})
  link_path = user_path(user)
  link_to(link_path, options) do
    if block_given?
      yield(user)
    else
      display_user(user)
    end
  end
end

#time_period_class(item) ⇒ Array<String>

A helper for generating CSS classes, based on the time-bounded status of the item.

Parameters:

  • item (ActiveRecord::Base)

    An ActiveRecord object which has time-bounded fields.

Returns:

  • (Array<String>)

    An array of CSS classes applicable for the provided item.


121
122
123
124
125
126
127
128
129
# File 'app/helpers/application_formatters_helper.rb', line 121

def time_period_class(item)
  if !item.started?
    ['not-started']
  elsif item.ended?
    ['ended']
  else # Started, but not yet ended.
    ['currently-active']
  end
end

#time_period_message(item) ⇒ String?

A helper for retrieving the title for a time-bounded item's status.

Parameters:

  • item (ActiveRecord::Base)

    An ActiveRecord object which has time-bounded fields.

Returns:

  • (String)

    A translated string representing the status of the item.

  • (nil)

    If the item is valid.


136
137
138
139
140
141
142
# File 'app/helpers/application_formatters_helper.rb', line 136

def time_period_message(item)
  if !item.started?
    t('common.not_started')
  elsif item.ended?
    t('common.ended')
  end
end

#unread_class(item) ⇒ Array<String>

A helper for generating CSS classes, based on the unread status of the item.

Parameters:

  • item (ActiveRecord::Base)

    An ActiveRecord object which acts as readable.

Returns:

  • (Array<String>)

    An array of CSS classes applicable for the provided item.


169
170
171
172
173
174
175
# File 'app/helpers/application_formatters_helper.rb', line 169

def unread_class(item)
  if item.unread?(current_user)
    ['unread']
  else
    []
  end
end