Skip to content
Extraits de code Groupes Projets
application_controller.rb 2,8 ko
Newer Older
  • Learn to ignore specific revisions
  • 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 :mobile_except_ipad
    
    maxwell's avatar
    maxwell a validé
      before_filter :set_contacts_notifications_and_status, :except => [:create, :update]
    
      before_filter :count_requests
    
      before_filter :set_invites
    
      before_filter :set_locale
    
      before_filter :set_grammatical_gender
    
      inflection_method :grammatical_gender => :gender
    
    danielvincent's avatar
    danielvincent a validé
    
    
    maxwell's avatar
    maxwell a validé
      def set_contacts_notifications_and_status
    
          @all_aspects = current_user.aspects.fields(:name, :contacts)
    
          @aspects_dropdown_array = @all_aspects.collect{|x| [x.to_s, x.id]}
    
          @notification_count = Notification.for(current_user, :unread =>true).all.count
    
    Raphael's avatar
    Raphael a validé
        end
    
      def mobile_except_ipad
        if is_mobile_device?
    
          if request.env["HTTP_USER_AGENT"].include? "iPad"
    
    danielvincent's avatar
    danielvincent a validé
            session[:mobile_view] = false
    
      def count_requests
    
        @request_count = Request.to(current_user.person).count if current_user
    
    maxwell's avatar
    maxwell a validé
        if user_signed_in?
    
          @invites = current_user.invites
        end
      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
    
      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
    
    
      def similar_people contact, opts={}
        opts[:limit] ||= 5
    
        aspect_ids = contact.aspect_ids
    
        count = Contact.count(:user_id => current_user.id,
                              :person_id.ne => contact.person.id,
                              :aspect_ids.in => aspect_ids)
    
        if count > opts[:limit]
          offset = rand(count-opts[:limit])
        else
          offset = 0
        end
    
    
        contacts = Contact.all(:user_id => current_user.id,
                               :person_id.ne => contact.person.id,
                               :aspect_ids.in => aspect_ids,
    
                               :skip => offset,
                               :limit => opts[:limit])
    
        contacts.collect!{ |contact| contact.person }
      end
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
    end