application_helper.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. module ApplicationHelper
  2. def icon_tag(name, options = {})
  3. dom_class = options[:class]
  4. case name
  5. when /\Aglyphicon-/
  6. "<span class='glyphicon #{name}#{' ' if dom_class}#{dom_class}'></span>".html_safe
  7. when /\Afa-/
  8. "<i class='#{'fa-solid ' unless /(?:\A| )fa-(?:solid|brands)(?: |\z)/.match?(dom_class)}#{name}#{' ' if dom_class}#{dom_class}'></i>".html_safe
  9. else
  10. raise "Unrecognized icon name: #{name}"
  11. end
  12. end
  13. def nav_link(name, path, options = {}, &block)
  14. content = link_to(name, path, options)
  15. active = current_page?(path)
  16. if block
  17. # Passing a block signifies that the link is a header of a hover
  18. # menu which contains what's in the block.
  19. begin
  20. @nav_in_menu = true
  21. @nav_link_active = active
  22. content += capture(&block)
  23. class_name = "dropdown dropdown-hover #{@nav_link_active ? 'active' : ''}"
  24. ensure
  25. @nav_in_menu = @nav_link_active = false
  26. end
  27. else
  28. # Mark the menu header active if it contains the current page
  29. @nav_link_active ||= active if @nav_in_menu
  30. # An "active" menu item may be an eyesore, hence `!@nav_in_menu &&`.
  31. class_name = !@nav_in_menu && active ? 'active' : ''
  32. end
  33. content_tag :li, content, class: class_name
  34. end
  35. def yes_no(bool)
  36. content_tag :span, bool ? 'Yes' : 'No', class: "label #{bool ? 'label-info' : 'label-default' }"
  37. end
  38. def working(agent)
  39. if agent.disabled?
  40. link_to 'Disabled', agent_path(agent), class: 'label label-warning'
  41. elsif agent.dependencies_missing?
  42. content_tag :span, 'Missing Gems', class: 'label label-danger'
  43. elsif agent.working?
  44. content_tag :span, 'Yes', class: 'label label-success'
  45. else
  46. link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
  47. end
  48. end
  49. def omniauth_provider_icon(provider)
  50. case provider.to_sym
  51. when :twitter, :tumblr, :github, :dropbox, :google
  52. icon_tag("fa-#{provider}", class: 'fa-brands')
  53. else
  54. icon_tag("fa-lock")
  55. end
  56. end
  57. def omniauth_provider_name(provider)
  58. t("devise.omniauth_providers.#{provider}")
  59. end
  60. def omniauth_button(provider)
  61. link_to [
  62. omniauth_provider_icon(provider),
  63. content_tag(:span, "Authenticate with #{omniauth_provider_name(provider)}")
  64. ].join.html_safe, user_omniauth_authorize_path(provider), class: "btn btn-default btn-service service-#{provider}"
  65. end
  66. def service_label_text(service)
  67. "#{omniauth_provider_name(service.provider)} - #{service.name}"
  68. end
  69. def service_label(service)
  70. return if service.nil?
  71. content_tag :span, [
  72. omniauth_provider_icon(service.provider),
  73. service_label_text(service)
  74. ].join.html_safe, class: "label label-default label-service service-#{service.provider}"
  75. end
  76. def load_ace_editor!
  77. unless content_for?(:ace_editor_script)
  78. content_for :ace_editor_script, javascript_include_tag('ace')
  79. end
  80. end
  81. def highlighted?(id)
  82. @highlighted_ranges ||=
  83. case value = params[:hl].presence
  84. when String
  85. value.split(/,/).flat_map { |part|
  86. case part
  87. when /\A(\d+)\z/
  88. (part.to_i)..(part.to_i)
  89. when /\A(\d+)?-(\d+)?\z/
  90. ($1 ? $1.to_i : 1)..($2 ? $2.to_i : Float::INFINITY)
  91. else
  92. []
  93. end
  94. }
  95. else
  96. []
  97. end
  98. @highlighted_ranges.any? { |range| range.cover?(id) }
  99. end
  100. def agent_type_to_human(type)
  101. type.gsub(/^.*::/, '').underscore.humanize.titleize
  102. end
  103. private
  104. def user_omniauth_authorize_path(provider)
  105. send "user_#{provider}_omniauth_authorize_path"
  106. end
  107. end