application_helper.rb 3.4 KB

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