application_helper.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. return if service.nil?
  75. content_tag :span, [
  76. omniauth_provider_icon(service.provider),
  77. service_label_text(service)
  78. ].join.html_safe, class: "label label-default label-service service-#{service.provider}"
  79. end
  80. def load_ace_editor!
  81. unless content_for?(:ace_editor_script)
  82. content_for :ace_editor_script, javascript_include_tag('ace')
  83. end
  84. end
  85. def highlighted?(id)
  86. @highlighted_ranges ||=
  87. case value = params[:hl].presence
  88. when String
  89. value.split(/,/).flat_map { |part|
  90. case part
  91. when /\A(\d+)\z/
  92. (part.to_i)..(part.to_i)
  93. when /\A(\d+)?-(\d+)?\z/
  94. ($1 ? $1.to_i : 1)..($2 ? $2.to_i : Float::INFINITY)
  95. else
  96. []
  97. end
  98. }
  99. else
  100. []
  101. end
  102. @highlighted_ranges.any? { |range| range.cover?(id) }
  103. end
  104. def agent_type_to_human(type)
  105. type.gsub(/^.*::/, '').underscore.humanize.titleize
  106. end
  107. private
  108. def user_omniauth_authorize_path(provider)
  109. send "user_#{provider}_omniauth_authorize_path"
  110. end
  111. end