You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.1 KiB

8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. module ApplicationHelper
  3. DANGEROUS_SCOPES = %w(
  4. read
  5. write
  6. follow
  7. ).freeze
  8. def active_nav_class(*paths)
  9. paths.any? { |path| current_page?(path) } ? 'active' : ''
  10. end
  11. def active_link_to(label, path, **options)
  12. link_to label, path, options.merge(class: active_nav_class(path))
  13. end
  14. def show_landing_strip?
  15. !user_signed_in? && !single_user_mode?
  16. end
  17. def open_registrations?
  18. Setting.registrations_mode == 'open'
  19. end
  20. def approved_registrations?
  21. Setting.registrations_mode == 'approved'
  22. end
  23. def closed_registrations?
  24. Setting.registrations_mode == 'none'
  25. end
  26. def available_sign_up_path
  27. if closed_registrations?
  28. 'https://joinmastodon.org/#getting-started'
  29. else
  30. new_user_registration_path
  31. end
  32. end
  33. def open_deletion?
  34. Setting.open_deletion
  35. end
  36. def locale_direction
  37. if [:ar, :fa, :he].include?(I18n.locale)
  38. 'rtl'
  39. else
  40. 'ltr'
  41. end
  42. end
  43. def favicon_path
  44. env_suffix = Rails.env.production? ? '' : '-dev'
  45. "/favicon#{env_suffix}.ico"
  46. end
  47. def title
  48. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  49. end
  50. def class_for_scope(scope)
  51. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  52. end
  53. def can?(action, record)
  54. return false if record.nil?
  55. policy(record).public_send("#{action}?")
  56. end
  57. def fa_icon(icon, attributes = {})
  58. class_names = attributes[:class]&.split(' ') || []
  59. class_names << 'fa'
  60. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  61. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  62. end
  63. def custom_emoji_tag(custom_emoji)
  64. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  65. end
  66. def opengraph(property, content)
  67. tag(:meta, content: content, property: property)
  68. end
  69. def react_component(name, props = {}, &block)
  70. if block.nil?
  71. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  72. else
  73. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  74. end
  75. end
  76. def body_classes
  77. output = (@body_classes || '').split(' ')
  78. output << "flavour-#{current_flavour.parameterize}"
  79. output << "skin-#{current_skin.parameterize}"
  80. output << 'system-font' if current_account&.user&.setting_system_font_ui
  81. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  82. output << 'rtl' if locale_direction == 'rtl'
  83. output.reject(&:blank?).join(' ')
  84. end
  85. def cdn_host
  86. Rails.configuration.action_controller.asset_host
  87. end
  88. def cdn_host?
  89. cdn_host.present?
  90. end
  91. def storage_host
  92. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  93. end
  94. def storage_host?
  95. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  96. end
  97. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  98. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  99. text.split("\n").map { |line| '> ' + line }.join("\n")
  100. end
  101. end