/app/helpers/application_helper.rb

http://github.com/teambox/teambox · Ruby · 287 lines · 229 code · 50 blank · 8 comment · 35 complexity · 6d48ba85fd6fd205807e3391caea921d MD5 · raw file

  1. # Methods added to this helper will be available to all templates in the application.
  2. module ApplicationHelper
  3. def content_for(*args)
  4. super unless args.first.to_sym == :column and mobile?
  5. end
  6. def logo_image
  7. logo = @organization ? @organization.logo(:top) : "header_logo_black.png"
  8. image_tag(logo, :alt => "Teambox")
  9. end
  10. def archived_project_strip(project)
  11. if project.try(:archived)
  12. render 'shared/strip', :project => project
  13. end
  14. end
  15. def submit_or_cancel(object, name, submit_id, loading_id)
  16. render 'shared/submit_or_cancel',
  17. :object => object,
  18. :name => name,
  19. :submit_id => submit_id,
  20. :loading_id => loading_id
  21. end
  22. # types: success, error, notice
  23. def show_flash
  24. flash.each do |type, message|
  25. unless message.blank?
  26. haml_tag :p, h(message.html_safe), :class => "flash flash-#{type}"
  27. end
  28. end
  29. end
  30. def navigation(project,projects,recent_projects)
  31. render 'shared/navigation',
  32. :project => project,
  33. :projects => projects,
  34. :recent_projects => recent_projects
  35. end
  36. def search_bar
  37. render 'shared/search_bar'
  38. end
  39. def header
  40. render 'shared/header'
  41. end
  42. def footer
  43. render 'shared/footer'
  44. end
  45. def location_name?(names)
  46. Array(names).any?{ |name| name == location_name }
  47. end
  48. def location_name
  49. "#{action_name}_#{controller.controller_name}"
  50. end
  51. def loading_image(id)
  52. image_tag('loading.gif', :id => id, :class => 'loading', :style => 'display: none')
  53. end
  54. def loading(action, id = nil)
  55. img_id = id ? "#{action}_loading_#{id}" : "#{action}_loading"
  56. image_tag('loading.gif', :id => img_id, :class => 'loading', :style => 'display: none', :alt => '')
  57. end
  58. def posted_date(datetime)
  59. datetime = datetime.in_time_zone(current_user.time_zone) if current_user
  60. content_tag :time, localize(datetime, :format => :long), :class => 'timeago',
  61. :datetime => datetime.xmlschema, :pubdate => true, :'data-msec' => datetime_ms(datetime)
  62. end
  63. def datetime_ms(datetime)
  64. datetime = datetime.in_time_zone(current_user.time_zone) if current_user
  65. datetime.to_i * 1000
  66. end
  67. def drag_image
  68. image_tag('drag.png', :class => 'drag')
  69. end
  70. def loading_action_image(e=nil, hidden=false)
  71. image_tag('loading.gif',
  72. :id => "loading_action#{ "_#{e}" if e}",
  73. :class => 'loading_action',
  74. :style => (hidden ? 'display:none' : nil))
  75. end
  76. def is_controller?(_controller, _action = nil)
  77. controller.controller_name == _controller.to_s and (_action == nil or controller.action_name == _action.to_s)
  78. end
  79. def support_link
  80. if url = Teambox.config.support_url
  81. link_to t('.support'), url
  82. end
  83. end
  84. def mobile_link
  85. link_to t('.mobile'), change_format_path(:m)
  86. end
  87. def help_link
  88. if url = Teambox.config.help_url
  89. link_to t('.help'), "#{url}/#{controller.controller_name}"
  90. end
  91. end
  92. def to_sentence(array)
  93. array.to_sentence(:two_words_connector => " #{t('common.and')} ",
  94. :last_word_connector => " #{t('common.and')} ")
  95. end
  96. def upgrade_browser
  97. render 'shared/upgrade_browser'
  98. end
  99. def chrome_frame
  100. render 'shared/chrome_frame'
  101. end
  102. def latest_announcement
  103. render 'shared/latest_announcement'
  104. end
  105. def errors_for(model, field)
  106. error = case errors = model.errors.on(field)
  107. when Array then errors.first
  108. when String then errors
  109. end
  110. "<div class='errors_for'>#{error}</div>".html_safe
  111. end
  112. def formatting_documentation_link
  113. link_to '', text_styles_path, :rel => :facebox, :class => :style_icon, :title => t('projects.show.text_styling')
  114. end
  115. def formatting_invitations_link
  116. link_to t('invitations.search.help'), invite_format_path, :rel => :facebox
  117. end
  118. def host_with_protocol
  119. request.protocol + request.host + request.port_string
  120. end
  121. def human_hours(hours)
  122. hours = (hours.to_f * 100).round.to_f / 100
  123. if hours > 0
  124. minutes = ((hours % 1) * 60).round
  125. if minutes == 60
  126. hours += 1
  127. minutes = 0
  128. end
  129. if minutes.zero?
  130. t('comments.comment.hours', :hours => hours.to_i)
  131. else
  132. t('comments.comment.hours_with_minutes', :hours => hours.to_i, :minutes => minutes)
  133. end
  134. end
  135. end
  136. def set_reload_url(path)
  137. @reload_url = path
  138. end
  139. def reload_url
  140. @reload_url || url_for(request.path_parameters)
  141. end
  142. def rss?
  143. request.format == :rss
  144. end
  145. def time_tracking_enabled?
  146. Teambox.config.allow_time_tracking || false
  147. end
  148. def auto_discovery_link_by_context(user, project)
  149. if user
  150. path = project ? project_path(project, :format => :rss) : projects_path(:format => :rss)
  151. auto_discovery_link_tag(:rss, user_rss_token(path))
  152. end
  153. end
  154. def configure_this_organization
  155. if Teambox.config.community && @community_role == :admin && @community_organization.description.blank? && params[:organization].nil?
  156. message = if location_name != "appearance_organizations"
  157. link_to("Click here", appearance_organization_path(@community_organization)) + " to configure your organization"
  158. else
  159. "Introduce some HTML code for your main site to configure your site"
  160. end
  161. %(<div style="background-color: rgb(255,255,220); border-bottom: 1px solid rgb(200,200,150); width: 100%; display: block; font-size: 12px; padding: 10px 0; text-align: center">
  162. #{message}
  163. </div>).html_safe
  164. end
  165. end
  166. def locale_select_values
  167. I18n.available_locales.map { |code|
  168. [t(code, :scope => :locales, :locale => code), code.to_s]
  169. }.sort_by(&:first)
  170. end
  171. def current_locale_name
  172. t(I18n.locale, :scope => :locales, :locale => I18n.locale)
  173. end
  174. # collecting stats about Teambox installations
  175. def tracking_code
  176. if Teambox.config.tracking_enabled and Rails.env.production?
  177. fake_img = "https://teambox.com/logo.png/#{request.host}"
  178. %(<div style="background-image: url(#{fake_img})"></div>).html_safe
  179. end
  180. end
  181. def preview_button
  182. content_tag(:button, :'data-alternate' => t('comments.preview.close'), :class => :preview) do
  183. t('comments.preview.preview')
  184. end
  185. end
  186. def upload_form_html_options(page, upload)
  187. id = upload.new_record? ? 'new_upload' : 'upload_file_form'
  188. form_classes = %w(upload_form app_form)
  189. form_classes << 'form_error' unless upload.errors.empty?
  190. form_classes << 'new_upload' if upload.new_record?
  191. hidden = page || (action_name == 'index' && upload.errors.empty?)
  192. form_style = hidden ? 'display: none' : nil
  193. {:html => { :multipart => true, :id => id, :style => form_style, :class => form_classes}}
  194. end
  195. ##
  196. ## eg. js_id(:edit_header,@project,@tasklist) => project_21_task_list_12_edit_header
  197. ## eg. js_id(:new_header,@project,Task.new) => project_21_task_list_new_header
  198. ## eg. js_id(@project,Task.new) => project_21_task_list
  199. def js_id(*args)
  200. if args.is_a?(Array)
  201. element = args[0].is_a?(String) || args[0].is_a?(Symbol) ? args[0] : nil
  202. models = args.slice(1,args.size-1)
  203. raise ArgumentError unless models.all?{|a|a.is_a?(ActiveRecord::Base)}
  204. elsif args.is_a?(ActiveRecord::Base)
  205. models = [args]
  206. else
  207. raise ArgumentError
  208. end
  209. generate_js_path(element,models)
  210. end
  211. def generate_js_path(element,models)
  212. path = []
  213. models.each do |model|
  214. path << model.class.to_s.underscore
  215. path << model.id unless model.new_record?
  216. end
  217. path << element unless element.nil?
  218. path.join('_')
  219. end
  220. def show_form_errors(target,form_id)
  221. page.select("##{form_id} .error").each do |e|
  222. e.remove
  223. end
  224. target.errors.each do |field,message|
  225. errors = <<BLOCK
  226. var e = $('#{form_id}').down('.#{field}');
  227. if (e) {
  228. if(e.down('.error'))
  229. e.down('.error').insert({bottom: "<br /><span>'#{message}'</span>"})
  230. else
  231. e.insert({ bottom: "<p class='error'><span>#{message}</span></p>"})
  232. }
  233. BLOCK
  234. page << errors
  235. end
  236. end
  237. end