Module: ApplicationHelper

Included in:
MatchStartController, PlayerActionsController, PlayerActionsHelper
Defined in:
app/helpers/application_helper.rb

Overview

General controller/view helpers for this application.

Instance Method Summary collapse

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File 'app/helpers/application_helper.rb', line 60

def error?
  begin
    yield if block_given?
    false
  rescue => e
    log_error e
    true
  end
end


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/application_helper.rb', line 99

def help_link
  link_with_glyph(
    '',
    ApplicationDefs::HELP_LINK,
    'question-sign',
    {
      class: ApplicationDefs::HELP_LINK_HTML_CLASS,
      # `target: blank` option ensures that this link will be opened in a new tab
      target: 'blank',
      title: 'Help',
      data: { toggle: 'tooltip' }
    }
  )
end


48
49
50
51
52
53
54
# File 'app/helpers/application_helper.rb', line 48

def link_with_glyph(link_text, link_target, glyph, options={})
  link_to link_target, options do
    inserted_html = "#{(:i, nil, class: 'icon-' << glyph)} #{link_text}".html_safe
    inserted_html << yield if block_given?
    inserted_html
  end
end

#log_error(e) ⇒ Object



56
57
58
# File 'app/helpers/application_helper.rb', line 56

def log_error(e)
  Rails.logger.fatal({exception: {message: e.message, backtrace: e.backtrace}}.awesome_inspect)
end

#matchObject



113
114
115
116
117
118
119
# File 'app/helpers/application_helper.rb', line 113

def match
  if @match_view
    @match_view.match
  else
    @match ||= Match.new
  end
end

#replace_page_contents(replacement_partial, alert_message = @alert_message, html_element = "#{ApplicationDefs::HTML_CLASS_PREFIX}#{ApplicationDefs::APP_VIEW_HTML_CLASS}") ⇒ Object

Renders a shared JavaScript template that replaces the old contents of the current page with new contents. In essence, it acts like a page refresh.

Parameters:

  • replacement_partial (String)

    The partial with which the page should be replaced.

  • alert_message (String) (defaults to: @alert_message)

    An alert message to be displayed.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/application_helper.rb', line 11

def replace_page_contents(
  replacement_partial,
  alert_message=@alert_message,
  html_element="#{ApplicationDefs::HTML_CLASS_PREFIX}#{ApplicationDefs::APP_VIEW_HTML_CLASS}"
)
  @alert_message = alert_message
  @html_element = html_element

  Rails.logger.fatal({
    method: __method__,
    alert_message: @alert_message,
    html_element: @html_element
  }.awesome_inspect)

  @replacement_partial = replacement_partial
  if (
    error? do
      respond_to do |format|
        format.js do
          render ApplicationDefs::REPLACE_CONTENTS_JS, formats: [:js]
        end
      end
    end
  )
    @alert_message ||= "Unable to update the page, #{self.class.report_error_request_message}"
    if replacement_partial == ApplicationDefs::NEW_MATCH_PARTIAL
      return(redirect_to(root_path, remote: true))
    else
      return reset_to_match_entry_view
    end
  end
end

#reset_to_match_entry_view(error_message = @alert_message) ⇒ Object



44
45
46
# File 'app/helpers/application_helper.rb', line 44

def reset_to_match_entry_view(error_message=@alert_message)
  replace_page_contents ApplicationDefs::NEW_MATCH_PARTIAL, error_message
end

#userObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/application_helper.rb', line 79

def user
  return @user if @user
  users = User.where name: user_name
  @user = if users.empty?
    u = User.new name: user_name
    u.save!
    u.reset_hotkeys!
    u
  else
    users.shift
  end
end

#user_initialized?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'app/helpers/application_helper.rb', line 70

def user_initialized?
  begin
    user
    true
  rescue Mongoid::Errors
    false
  end
end

#user_nameString

Returns The currently signed in user name. Defaults to User.default_user_name.

Returns:

  • (String)

    The currently signed in user name. Defaults to User.default_user_name



92
93
94
95
96
97
98
# File 'app/helpers/application_helper.rb', line 92

def user_name
  name = begin
    ActionController::HttpAuthentication::Basic::user_name_and_password(request).first
  rescue NoMethodError # Occurs when no authentication has been done
    User::DEFAULT_NAME
  end
end