Class: ApplicationController

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

Overview

ApplicationController

Instance Method Summary collapse

Instance Method Details

#fresh_when(opts = {}) ⇒ Object


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

def fresh_when(opts = {})
  opts[:etag] ||= []
  # 保证 etag 参数是 Array 类型
  opts[:etag] = [opts[:etag]] unless opts[:etag].is_a?(Array)
  # 加入页面上直接调用的信息用于组合 etag
  opts[:etag] << current_user
  # Config 的某些信息
  opts[:etag] << Setting.custom_head_html
  opts[:etag] << Setting.footer_html
  # 加入通知数量
  opts[:etag] << unread_notify_count
  # 加入flash,确保当页面刷新后flash不会再出现
  opts[:etag] << flash
  # 所有 etag 保持一天
  opts[:etag] << Date.current
  super(opts)
end

#redirect_back_or_default(default) ⇒ Object


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

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

#redirect_referrer_or_default(default) ⇒ Object


87
88
89
# File 'app/controllers/application_controller.rb', line 87

def redirect_referrer_or_default(default)
  redirect_to(request.referrer || default)
end

#render_403Object


57
58
59
# File 'app/controllers/application_controller.rb', line 57

def render_403
  render_optional_error_file(403)
end

#render_404Object


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

def render_404
  render_optional_error_file(404)
end

#render_optional_error_file(status_code) ⇒ Object


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

def render_optional_error_file(status_code)
  status = status_code.to_s
  fname = %w(404 403 422 500).include?(status) ? status : 'unknown'
  render template: "/errors/#{fname}", format: [:html],
         handler: [:erb], status: status, layout: 'application'
end

#require_userObject


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

def require_user
  if current_user.blank?
    respond_to do |format|
      format.html { authenticate_user! }
      format.all { head(:unauthorized) }
    end
  end
end

#set_active_menuObject


34
35
36
37
38
39
40
41
# File 'app/controllers/application_controller.rb', line 34

def set_active_menu
  @current = case controller_name
             when 'pages'
               ['/wiki']
             else
               ["/#{controller_name}"]
  end
end

#set_localeObject


44
45
46
47
48
49
50
51
# File 'app/controllers/application_controller.rb', line 44

def set_locale
  I18n.locale = user_locale

  # after store current locale
  cookies[:locale] = params[:locale] if params[:locale]
rescue I18n::InvalidLocale
  I18n.locale = I18n.default_locale
end

#set_seo_meta(title = '', meta_keywords = '', meta_description = '') ⇒ Object


72
73
74
75
76
# File 'app/controllers/application_controller.rb', line 72

def set_seo_meta(title = '', meta_keywords = '', meta_description = '')
  @page_title = "#{title}" if title.length > 0
  @meta_keywords = meta_keywords
  @meta_description = meta_description
end

#store_locationObject


78
79
80
# File 'app/controllers/application_controller.rb', line 78

def store_location
  session[:return_to] = request.request_uri
end

#unread_notify_countObject


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

def unread_notify_count
  return 0 if current_user.blank?
  @unread_notify_count ||= Notification.unread_count(current_user)
end