Skip to content
Extraits de code Groupes Projets
application_controller.rb 3,32 ko
Newer Older
Raphael's avatar
Raphael a validé
#   Copyright (c) 2010, Diaspora Inc.  This file is
Raphael's avatar
Raphael a validé
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael a validé
#   the COPYRIGHT file.
Raphael Sofaer's avatar
Raphael Sofaer a validé
class ApplicationController < ActionController::Base
  protect_from_forgery :except => :receive
  before_filter :ensure_http_referer_is_set
  before_filter :set_header_data, :except => [:create, :update]
  before_filter :count_requests
  before_filter :set_invites
  before_filter :set_locale
  before_filter :set_git_header if (AppConfig[:git_update] && AppConfig[:git_revision])
  before_filter :which_action_and_user
  prepend_before_filter :clear_gc_stats
  before_filter :set_grammatical_gender

  inflection_method :grammatical_gender => :gender
danielvincent's avatar
danielvincent a validé

  def ensure_http_referer_is_set
    request.env['HTTP_REFERER'] ||= '/aspects'
  end

  def set_header_data
MrZYX's avatar
MrZYX a validé
    if user_signed_in?
      if request.format.html?
        @aspect = nil
        @object_aspect_ids = []
        @notification_count = Notification.for(current_user, :unread =>true).count
        @unread_message_count = ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}")
      end
      @all_aspects = current_user.aspects
Raphael's avatar
Raphael a validé
    end
  end

  def count_requests
Raphael's avatar
Raphael a validé
    @request_count = Request.where(:recipient_id => current_user.person.id).count if current_user
maxwell's avatar
maxwell a validé
    if user_signed_in?
      @invites = current_user.invites
    end
  end
    headers['X-Git-Update'] = AppConfig[:git_update]
    headers['X-Git-Revision'] = AppConfig[:git_revision]
  def which_action_and_user
    str = "event=request_with_user controller=#{self.class} action=#{self.action_name} "
      str << "uid=#{current_user.id} "
      str << "user_created_at='#{current_user.created_at.to_date.to_s}' user_created_at_unix=#{current_user.created_at.to_i} " if current_user.created_at
zhitomirskiyi's avatar
zhitomirskiyi a validé
      str << "user_non_pending_contact_count=#{current_user.contacts.size} user_contact_count=#{Contact.unscoped.where(:user_id => current_user.id).size} "
    else
      str << 'uid=nil'
    end
    Rails.logger.info str
  end

maxwell's avatar
maxwell a validé
    if user_signed_in?
      I18n.locale = current_user.language
    else
      I18n.locale = request.compatible_language_from AVAILABLE_LANGUAGE_CODES
    end

    WillPaginate::ViewHelpers.pagination_options[:previous_label] = "&laquo; #{I18n.t('previous')}"
    WillPaginate::ViewHelpers.pagination_options[:next_label] = "#{I18n.t('next')} &raquo;"
  def clear_gc_stats
    GC.clear_stats if GC.respond_to?(:clear_stats)
  end
    admins = AppConfig[:admins]
    unless admins.present? && admins.include?(current_user.username)
  def set_grammatical_gender
    if (user_signed_in? && I18n.inflector.inflected_locale?)
      gender = current_user.profile.gender.to_s.tr('!()[]"\'`*=|/\#.,-:', '').downcase
      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
Raphael Sofaer's avatar
Raphael Sofaer a validé
end