Module: Thredded::ApplicationHelper

Includes:
NavHelper, RenderHelper, UrlsHelper
Defined in:
app/helpers/thredded/application_helper.rb

Constant Summary

Constants included from NavHelper

NavHelper::USER_NAV_MODERATION_PAGES, NavHelper::USER_NAV_PREFERENCES_PAGES, NavHelper::USER_NAV_PRIVATE_TOPICS_PAGES

Instance Method Summary collapse

Methods included from RenderHelper

#render_collection_to_strings_with_cache

Methods included from NavHelper

#current_page_moderation?, #current_page_preferences?, #current_page_private_topics?

Methods included from UrlsHelper

#delete_post_path, #edit_post_path, #edit_preferences_path, #edit_preferences_url, #mark_unread_path, #permalink_path, #post_path, #post_url, #quote_post_path, #search_path, #topic_path, #topic_url, #user_path

Instance Method Details

#moderatable_messageboards_idsObject



119
120
121
122
# File 'app/helpers/thredded/application_helper.rb', line 119

def moderatable_messageboards_ids
  @moderatable_messageboards_ids ||=
    thredded_current_user.thredded_can_moderate_messageboards.pluck(:id)
end

#paginate(collection, args = {}) ⇒ Object



81
82
83
# File 'app/helpers/thredded/application_helper.rb', line 81

def paginate(collection, args = {})
  super(collection, args.reverse_merge(views_prefix: 'thredded'))
end

#posts_pending_moderation_countObject



124
125
126
127
# File 'app/helpers/thredded/application_helper.rb', line 124

def posts_pending_moderation_count
  @posts_pending_moderation_count ||=
    Thredded::Post.where(messageboard_id: moderatable_messageboards_ids).pending_moderation.count
end

#render_posts(posts, partial: 'thredded/posts/post', content_partial: 'thredded/posts/content', locals: {}) ⇒ Object

Parameters:



74
75
76
77
78
79
# File 'app/helpers/thredded/application_helper.rb', line 74

def render_posts(posts, partial: 'thredded/posts/post', content_partial: 'thredded/posts/content', locals: {})
  posts_with_contents = render_collection_to_strings_with_cache(
    partial: content_partial, collection: posts, as: :post, expires_in: 1.week
  )
  render partial: partial, collection: posts_with_contents, as: :post_and_content, locals: locals
end

#thredded_container_classesObject



22
23
24
25
26
# File 'app/helpers/thredded/application_helper.rb', line 22

def thredded_container_classes
  ['thredded--main-container', content_for(:thredded_page_id)].tap do |classes|
    classes << 'thredded--is-moderator' unless moderatable_messageboards_ids.empty?
  end
end

#thredded_container_dataObject



14
15
16
17
18
19
20
# File 'app/helpers/thredded/application_helper.rb', line 14

def thredded_container_data
  {
    'thredded-locale' => I18n.locale,
    'thredded-page-id' => content_for(:thredded_page_id),
    'thredded-root-url' => thredded.root_path
  }
end

#thredded_page(&block) ⇒ Object

Render the page container with the supplied block as content.



29
30
31
32
33
34
# File 'app/helpers/thredded/application_helper.rb', line 29

def thredded_page(&block)
  # enable the host app to easily check whether a thredded view is being rendered:
  content_for :thredded, true
  content_for :thredded_page_content, &block
  render partial: 'thredded/shared/page'
end

#time_ago(datetime, default: '-', html_options: {}) ⇒ String

Returns html_safe datetime presentation.

Parameters:

  • datetime (DateTime)
  • default (String) (defaults to: '-')

    a string to return if time is nil.

Returns:

  • (String)

    html_safe datetime presentation



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/thredded/application_helper.rb', line 56

def time_ago(datetime, default: '-', html_options: {})
  return  :time, default if datetime.nil?
  html_options = html_options.dup
  is_current_year = datetime.year == Time.current.year
  if datetime > 4.days.ago
    content = t 'thredded.time_ago', time: time_ago_in_words(datetime)
    html_options['data-time-ago'] = true unless html_options.key?('data-time-ago')
  else
    content = I18n.l(datetime.to_date,
                     format: (is_current_year ? :short : :long))
  end
  html_options[:title] = I18n.l(datetime) unless html_options.key?(:title)
  time_tag datetime, content, html_options
end

#topic_css_classes(topic) ⇒ Array<String>

Parameters:

Returns:

  • (Array<String>)


87
88
89
90
91
92
93
# File 'app/helpers/thredded/application_helper.rb', line 87

def topic_css_classes(topic)
  [
    *topic.states.map { |s| "thredded--topic-#{s}" },
    *(topic.categories.map { |c| "thredded--topic-category-#{c.name}" } if topic.respond_to?(:categories)),
    *('thredded--private-topic' if topic.is_a?(Thredded::PrivateTopicView))
  ]
end

#topic_follow_reason_text(follow_reason) ⇒ Object

Parameters:

  • follow_reason ('manual', 'posted', 'mentioned', 'auto', nil)


96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/thredded/application_helper.rb', line 96

def topic_follow_reason_text(follow_reason)
  if follow_reason
    # rubocop:disable Metrics/LineLength
    # i18n-tasks-use t('thredded.topics.following.manual') t('thredded.topics.following.posted') t('thredded.topics.following.mentioned') t('thredded.topics.following.auto')
    # rubocop:enable Metrics/LineLength
    t("thredded.topics.following.#{follow_reason}")
  else
    t('thredded.topics.not_following')
  end
end

#unread_private_topics_countObject



107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/thredded/application_helper.rb', line 107

def unread_private_topics_count
  @unread_private_topics_count ||=
    if thredded_signed_in?
      Thredded::PrivateTopic
        .for_user(thredded_current_user)
        .unread(thredded_current_user)
        .count
    else
      0
    end
end

Returns html_safe link to the user.

Parameters:

Returns:

  • (String)

    html_safe link to the user



38
39
40
# File 'app/helpers/thredded/application_helper.rb', line 38

def user_link(user)
  render partial: 'thredded/users/link', locals: { user: user }
end

#user_mention(user) ⇒ String

Returns wrapped @mention string.

Parameters:

Returns:

  • (String)

    wrapped @mention string



44
45
46
47
48
49
50
51
# File 'app/helpers/thredded/application_helper.rb', line 44

def user_mention(user)
  username = user.send(Thredded.user_name_column)
  if username.include?(' ')
    %(@"#{username}")
  else
    "@#{username}"
  end
end

#view_hooksAllViewHooks

Returns View hooks configuration.

Returns:



10
11
12
# File 'app/helpers/thredded/application_helper.rb', line 10

def view_hooks
  @view_hooks ||= Thredded.view_hooks
end