application_controller.rb 3.9 KB
Newer Older
1
#   Copyright (c) 2010-2012, Diaspora Inc.  This file is
Raphael's avatar
Raphael committed
2
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael committed
3
#   the COPYRIGHT file.
4

Raphael Sofaer's avatar
Raphael Sofaer committed
5
class ApplicationController < ActionController::Base
6
  before_action :force_tablet_html
7
  has_mobile_fu
8
  protect_from_forgery :except => :receive
9

10 11 12 13 14 15 16
  before_action :ensure_http_referer_is_set
  before_action :set_locale
  before_action :set_diaspora_header
  before_action :set_grammatical_gender
  before_action :mobile_switch
  before_action :gon_set_current_user
  before_action :gon_set_preloads
17 18

  inflection_method :grammatical_gender => :gender
danielvincent's avatar
danielvincent committed
19

20
  helper_method :all_aspects,
21 22 23 24
                :all_contacts_count,
                :my_contacts_count,
                :only_sharing_count,
                :tag_followings,
25 26
                :tags,
                :open_publisher
Raphael Sofaer's avatar
Raphael Sofaer committed
27

28
  layout proc { request.format == :mobile ? "application" : "with_header_with_footer" }
Jonne Haß's avatar
Jonne Haß committed
29

30 31
  private

32 33 34 35
  def default_serializer_options
    {root: false}
  end

36
  def ensure_http_referer_is_set
37
    request.env['HTTP_REFERER'] ||= '/'
38 39
  end

40 41
  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
Mikica Ivosevic's avatar
Mikica Ivosevic committed
42
    is_mobile_device? ? root_path : new_user_session_path
43
  end
Maxwell Salzberg's avatar
Maxwell Salzberg committed
44

45
  def all_aspects
Raphael Sofaer's avatar
Raphael Sofaer committed
46
    @all_aspects ||= current_user.aspects
47 48
  end

49 50 51 52 53 54 55 56 57 58 59
  def all_contacts_count
    @all_contacts_count ||= current_user.contacts.count
  end

  def my_contacts_count
    @my_contacts_count ||= current_user.contacts.receiving.count
  end

  def only_sharing_count
    @only_sharing_count ||= current_user.contacts.only_sharing.count
  end
60

61 62 63 64
  def tags
    @tags ||= current_user.followed_tags
  end

65 66 67
  def ensure_page
    params[:page] = params[:page] ? params[:page].to_i : 1
  end
68

Jonne Haß's avatar
Jonne Haß committed
69 70
  def set_diaspora_header
    headers['X-Diaspora-Version'] = AppConfig.version_string
Marcelo Dominguez's avatar
Marcelo Dominguez committed
71

Jonne Haß's avatar
Jonne Haß committed
72 73 74 75
    if AppConfig.git_available?
      headers['X-Git-Update'] = AppConfig.git_update if AppConfig.git_update.present?
      headers['X-Git-Revision'] = AppConfig.git_revision if AppConfig.git_revision.present?
    end
76 77
  end

78
  def set_locale
maxwell's avatar
maxwell committed
79
    if user_signed_in?
80 81
      I18n.locale = current_user.language
    else
Jonne Haß's avatar
Jonne Haß committed
82
      locale = http_accept_language.language_region_compatible_from AVAILABLE_LANGUAGE_CODES
83
      locale ||= DEFAULT_LANGUAGE
84
      I18n.locale = locale
85
    end
86
  end
87

88
  def redirect_unless_admin
89
    unless current_user.admin?
90
      redirect_to stream_url, :notice => 'you need to be an admin to do that'
91
      return
92 93
    end
  end
94

95 96
  def set_grammatical_gender
    if (user_signed_in? && I18n.inflector.inflected_locale?)
97
      gender = current_user.gender.to_s.tr('!()[]"\'`*=|/\#.,-:', '').downcase
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      unless gender.empty?
        i_langs = I18n.inflector.inflected_locales(:gender)
        i_langs.delete  I18n.locale
        i_langs.unshift I18n.locale
        i_langs.each do |lang|
          token = I18n.inflector.true_token(gender, :gender, lang)
          unless token.nil?
            @grammatical_gender = token
            break
          end
        end
      end
    end
  end

  def grammatical_gender
    @grammatical_gender || nil
  end
116

117 118 119 120 121 122
  # use :mobile view for mobile and :html for everything else
  # (except if explicitly specified, e.g. :json, :xml)
  def mobile_switch
    if session[:mobile_view] == true && request.format.html?
      request.format = :mobile
    end
123 124
  end

125 126 127 128
  def force_tablet_html
    session[:tablet_view] = false
  end

129
  def after_sign_in_path_for(resource)
130
    stored_location_for(:user) || current_user_redirect_path
131
  end
132

133
  def max_time
134
    params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now + 1
135
  end
136 137

  def current_user_redirect_path
138
    current_user.getting_started? ? getting_started_path : stream_path
139
  end
140

141
  def gon_set_current_user
142 143 144 145 146 147
    return unless user_signed_in?
    a_ids = session[:a_ids] || []
    user = UserPresenter.new(current_user, a_ids)
    gon.push({:user => user})
  end

148 149 150 151
  def gon_set_preloads
    return unless gon.preloads.nil?
    gon.preloads = {}
  end
Raphael Sofaer's avatar
Raphael Sofaer committed
152
end