<commit_before>class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :set_locale
  before_filter :configure_permitted_parameters, if: :devise_controller?

  # bug cancan rails 4 ForbbidenAttributesError
  # https://github.com/ryanb/cancan/issues/835
  before_filter do
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end

  rescue_from CanCan::AccessDenied do |exception|
    if user_signed_in? 
      redirect_to root_url, :alert => t('nlt.permission_denied')
    else
      redirect_to new_user_session_url, :alert => exception.message
    end
  end

  # after a user sing ins
  def after_sign_in_path_for(resource)
    # go to their location if they have one
    if current_user.woeid?
      ads_woeid_path(id: current_user.woeid, type: 'give')
    else
    # or ask for the location
      location_ask_path
    end
  end
   
  def set_locale
      I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options={})
    #logger.debug "default_url_options is passed options: #{options.inspect}\n"
    { locale: I18n.locale }
  end

  private

  def get_section_locations
    key = 'section_locations'
    if Rails.cache.fetch(key)
      @section_locations = Rails.cache.fetch(key)
    else
      locations = []
      Ad.available.all.group_by(&:woeid_code).each do |woeid_code, ad| 
        locations.append [woeid_code,ad.count]
      end
      @section_locations = locations.sort {|a,b| b[1] <=> a[1]}
      Rails.cache.write(key, @locations)
    end
  end

  def get_location_suggest 
    ip_address = GeoHelper.get_ip_address request
    GeoHelper.suggest ip_address
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password, :remember_me) }
  end


end<commit_msg>[woeid] refactoring section_locations<commit_after>class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :set_locale
  before_filter :configure_permitted_parameters, if: :devise_controller?

  # bug cancan rails 4 ForbbidenAttributesError
  # https://github.com/ryanb/cancan/issues/835
  before_filter do
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end

  rescue_from CanCan::AccessDenied do |exception|
    if user_signed_in? 
      redirect_to root_url, :alert => t('nlt.permission_denied')
    else
      redirect_to new_user_session_url, :alert => exception.message
    end
  end

  # after a user sing ins
  def after_sign_in_path_for(resource)
    # go to their location if they have one
    if current_user.woeid?
      ads_woeid_path(id: current_user.woeid, type: 'give')
    else
    # or ask for the location
      location_ask_path
    end
  end
   
  def set_locale
      I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options={})
    #logger.debug "default_url_options is passed options: #{options.inspect}\n"
    { locale: I18n.locale }
  end

  private

  def get_section_locations
    key = 'section_locations'
    if Rails.cache.fetch(key)
      @section_locations = Rails.cache.fetch(key)
    else
      @section_locations = Ad.available.group_by(&:woeid_code).map { |w,a| [w, a.count] }
      Rails.cache.write(key, @section_locations)
    end
  end

  def get_location_suggest 
    ip_address = GeoHelper.get_ip_address request
    GeoHelper.suggest ip_address
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password, :remember_me) }
  end


end
{'fraction_non_alphanumeric': 0.09243697478991597, 'fraction_numerical': 0.005135387488328665, 'mean_word_length': 3.8582766439909295, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1998616', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1398, 'n_words': 319}