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.

104 lines
2.5 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.open_registrations
  19. end
  20. def open_deletion?
  21. Setting.open_deletion
  22. end
  23. def locale_direction
  24. if [:ar, :fa, :he].include?(I18n.locale)
  25. 'rtl'
  26. else
  27. 'ltr'
  28. end
  29. end
  30. def favicon_path
  31. env_suffix = Rails.env.production? ? '' : '-dev'
  32. "/favicon#{env_suffix}.ico"
  33. end
  34. def title
  35. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  36. end
  37. def class_for_scope(scope)
  38. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  39. end
  40. def can?(action, record)
  41. return false if record.nil?
  42. policy(record).public_send("#{action}?")
  43. end
  44. def fa_icon(icon, attributes = {})
  45. class_names = attributes[:class]&.split(' ') || []
  46. class_names << 'fa'
  47. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  48. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  49. end
  50. def custom_emoji_tag(custom_emoji)
  51. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  52. end
  53. def opengraph(property, content)
  54. tag(:meta, content: content, property: property)
  55. end
  56. def react_component(name, props = {}, &block)
  57. if block.nil?
  58. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  59. else
  60. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  61. end
  62. end
  63. def body_classes
  64. output = (@body_classes || '').split(' ')
  65. output << "theme-#{current_theme.parameterize}"
  66. output << 'system-font' if current_account&.user&.setting_system_font_ui
  67. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  68. output << 'rtl' if locale_direction == 'rtl'
  69. output.reject(&:blank?).join(' ')
  70. end
  71. def cdn_host
  72. Rails.configuration.action_controller.asset_host
  73. end
  74. def cdn_host?
  75. cdn_host.present?
  76. end
  77. def storage_host
  78. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  79. end
  80. def storage_host?
  81. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  82. end
  83. end