Class: ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Includes:
UrlHelper
Defined in:
app/controllers/application_controller.rb

Instance Method Summary collapse

Methods included from UrlHelper

#feature_path, #link_to_project_feature

Instance Method Details

#after_sign_in_path_for(user) ⇒ Object



53
54
55
56
57
# File 'app/controllers/application_controller.rb', line 53

def (user)
  path = session["user.redirect_to"] || stored_location_for(user) || root_path
  path = root_path if path =~ /\/users\/(sign_in|password)/
  path
end

#api_authenticate!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/application_controller.rb', line 105

def api_authenticate!
  return if current_user

  allow_params_authentication!
  authenticate_or_request_with_http_basic do |username, password|
    params["user"] ||= {}
    params["user"].merge!(email: username, password: password)
    user = warden.authenticate(scope: :user)
    if user
      (:user, user)
    else
      head :unauthorized
    end
  end
end

#authenticated_via_token?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/controllers/application_controller.rb', line 99

def authenticated_via_token?
  @authenticated_via_token == true
end

#current_projectObject



131
132
133
# File 'app/controllers/application_controller.rb', line 131

def current_project
  @current_project ||= @project || (@default_project_slug ? Project[@default_project_slug] : (current_user && current_user.current_project))
end

#expire_revision!Object



66
67
68
69
70
71
# File 'app/controllers/application_controller.rb', line 66

def expire_revision!
  if session[:revision_expiration].blank? || session[:revision_expiration] < Time.now.utc
    session[:revision] = nil
    Rails.logger.info "[revision] expiring"
  end
end

#followed_projectsObject



124
125
126
127
128
# File 'app/controllers/application_controller.rb', line 124

def followed_projects
  return @followed_projects if defined?(@followed_projects)
  return @followed_projects = [] unless current_user
  @followed_projects = current_user.followed_projects.to_a
end

#no_cacheObject



157
158
159
160
161
# File 'app/controllers/application_controller.rb', line 157

def no_cache
  response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  response.headers["Pragma"] = "no-cache"
  response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

#oauth_authorize!(klass, scope:, redirect_to: nil) ⇒ Object

Raises:

  • (ArgumentError)


148
149
150
151
152
153
# File 'app/controllers/application_controller.rb', line 148

def oauth_authorize!(klass, scope:, redirect_to: nil)
  authorization = klass.for(current_user).find_or_create_by!(scope: scope)
  raise ArgumentError, "authorization already exists" if authorization.granted?
  session["#{authorization.id}_granted_redirect_url"] = redirect_to if redirect_to
  redirect_to authorization.url
end

#read_revisionObject



81
82
83
84
# File 'app/controllers/application_controller.rb', line 81

def read_revision
  revision_path = Rails.root.join("REVISION")
  File.exists?(revision_path) ? File.read(revision_path).chomp : ""
end

#require_loginObject



41
42
43
# File 'app/controllers/application_controller.rb', line 41

def 
  redirect_to main_app.new_user_session_path
end

#return_or_cache_revision!Object



73
74
75
76
77
78
79
# File 'app/controllers/application_controller.rb', line 73

def return_or_cache_revision!
  session[:revision] || read_revision.tap do |sha|
    session[:revision] = sha
    session[:revision_expiration] = 3.minutes.from_now
    Rails.logger.info "[revision] sha: #{sha[0..8]}, expiration: #{session[:revision_expiration]}"
  end
end

#revisionObject



61
62
63
64
# File 'app/controllers/application_controller.rb', line 61

def revision
  expire_revision!
  return_or_cache_revision!
end

#token_authenticate!Object



88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/application_controller.rb', line 88

def token_authenticate!
  return if current_user

  token = request.authorization[/\ABearer (.*)\z/, 1] if request.authorization
  user = User.joins(:api_tokens).find_by(api_tokens: { value: token }) if token
  return unless user

  @current_user = user
  @authenticated_via_token = true
end

#unfurling?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/controllers/application_controller.rb', line 47

def unfurling?
  request.env["HTTP_USER_AGENT"] =~ /^Slackbot-LinkExpanding/
end

#with_current_projectObject



135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/application_controller.rb', line 135

def with_current_project
  @default_project_slug = params[:project] if params[:project].is_a?(String)

  yield

  if current_user && current_project
    current_user.current_project_id = current_project.id
    current_user.save if current_user.current_project_id_changed?
  end
end