<commit_before>module RailsBreadcrumbs
  module ActionController
    module BaseMethods
      
      def self.included(base)
        base.send :before_filter, :breadcrumbs
      end

      protected    
  
      def breadcrumbs
     
        if session[:referrer].nil?
          session[:referrer] = [{:path => request.path, :fullpath => request.fullpath}]
        elsif request.path != session[:referrer].last[:path]
          if add_to_referrer?
            session[:referrer] << {:path => request.path, :fullpath => request.fullpath}
          elsif index = in_referrer?
            session[:referrer] = session[:referrer].slice(Range.new(0, (index - 1)))    
            session[:referrer] << {:path => request.path, :fullpath => request.fullpath}
          else
            session[:referrer] = [{:path => request.path, :fullpath => request.fullpath}]            
          end
        end   
    
        path_parameters = request.path_parameters.dup
        controller = path_parameters.delete :controller
        action = path_parameters.delete :action
        path = request.path.dup
        pattern = path.dup
        path_parameters.each_pair do |key, value|
          pattern.gsub! value, ":#{key}" 
        end
    
        @breadcrumbs = []         
        path_parts = path.split('/').select{|p|p!=''}
        pattern_parts = pattern.split('/').select{|p|p!=''}
        i = 0
        while path_parts.size > 0    
          if controller == 'dynamic'
            route = pattern_parts[0] == ':locale' ? path_parts.slice(1, path_parts.size - 1).join('/') : path_parts.join('/')  
            route = Route.where(:route => route, :locale => I18n.locale).first
            @breadcrumbs << {:name => route.routable.name, :path => dynamic_url(:route => route.route, :locale => I18n.locale)} if route
          else          
            title = []
            if path_parts.size < 2
              title << 'home'              
            else
              path_parts_en = url_for(:locale => :en, :only_path => true).split('/').select{|p|p!=''}
              path_parts.each_index do |i| 
                title << path_parts_en[i] unless i == 0 or pattern_parts[i][0] == ':' or pattern_parts[i][0] == '*'
              end
            end
            @breadcrumbs << {
              :name => t("pages.#{title.join('/').gsub('-', '_')}.title", :locale => I18n.locale), 
              :path => url_for(path)
            }
          end
          index = in_referrer?(path)
          if @breadcrumbs.any? and index
            @breadcrumbs.last[:path] = session[:referrer][index][:fullpath]
          end 
          path_parts.pop      
          i += 1
        end  
        @breadcrumbs.reverse!    
    
      end
  
      def add_to_referrer?   
        last = session[:referrer].last[:path]
        path_parts = request.path.split('/')
        in_tree = last == path_parts.slice(0, path_parts.size - 1).join('/')
        in_tree and request.path != last
      end

      def in_referrer?(path=request.path)
        session[:referrer].index do |referrer|
          referrer[:path] == path
        end
      end 
         
    end
  end
end
      
ActionController::Base.send :include, RailsBreadcrumbs::ActionController::BaseMethods<commit_msg>Refactory to handle base_url changes<commit_after>module RailsBreadcrumbs
  module ActionController
    module BaseMethods
      
      def self.included(base)
        base.send :before_filter, :breadcrumbs
      end

      protected    
  
      def breadcrumbs
     
        if session[:referer].nil?
          session[:referer] = [{:base_url => request.base_url, :path => request.path, :url => request.url}]
        elsif is_last_referer?
          if add_to_referer?
            session[:referer] << {:base_url => request.base_url, :path => request.path, :url => request.url}
          elsif index = in_referer?
            session[:referer] = session[:referer].slice(Range.new(0, (index - 1)))    
            session[:referer] << {:base_url => request.base_url, :path => request.path, :url => request.url}
          else
            session[:referer] = [{:base_url => request.base_url, :path => request.path, :url => request.url}]            
          end
        else
          session[:referer].last = {:base_url => request.base_url, :path => request.path, :url => request.url}
        end   
        
        path_parts_en = url_for(:locale => :en).split('/').select{|p|p!=''}
    
        path_parts = request.path.split('/').select{|p|p!=''}
        path_parts.pop
        path = "/#{path_parts.join('/')}"
        
        pattern = path.dup
        request.path_parameters.each_pair do |key, value|
          pattern.gsub! value, ":#{key}" unless [:action, :controller].include? key
        end        
        pattern_parts = pattern.split('/').select{|p|p!=''}
        
        @breadcrumbs = []    
        i = 0
        while path_parts.size > 0    
          info = Rails.application.routes.recognize_path path 
          controller = info[:controller]
          action = info[:action]
          
          if controller == 'dynamic'
            route = pattern_parts.first == ':locale' ? path_parts.slice(1, path_parts.size - 1).join('/') : path_parts.join('/')  
            route = Route.where(:route => route, :locale => I18n.locale).first
            @breadcrumbs << {:name => route.routable.name, :url => "#{request.base_url}#{path}"} if route
          else          
            title = []
            if path_parts.size < 2
              title << 'home'              
            else
              path_parts.each_index do |i| 
                title << path_parts_en[i] unless i == 0 or pattern_parts[i][0] == ':' or pattern_parts[i][0] == '*'
              end
            end
            @breadcrumbs << {
              :name => t("pages.#{title.join('/').gsub('-', '_')}.title", :locale => I18n.locale), 
              :url => "#{request.base_url}#{path}"
            }
          end
          
          index = in_referer?(request.base_url, path)
          if @breadcrumbs.any? and index
            @breadcrumbs.last[:url] = session[:referer][index][:url] 
          end        
          
          path_parts.pop      
          path = "/#{path_parts.join('/')}"
          pattern_parts.pop
          pattern = "/#{pattern_parts.join('/')}"
          i += 1
        end  
        @breadcrumbs.reverse!    
    
      end
      
      def is_last_referer?
        last = session[:referer].last
        last[:base_url] == request.base_url && last[:path] == request.path
      end
  
      def add_to_referer?   
        last = session[:referer].last
        return false if last[:base_url] == request.base_url
        path_parts = request.path.split('/')
        in_tree = last[:path] == path_parts.slice(0, path_parts.size - 1).join('/')
        in_tree and request.path != last[:path]
      end

      def in_referer?(base_url=request.base_url, path=request.path)
        session[:referer].index do |referer|
          referer[:base_url] == base_url and referer[:path] == path
        end
      end 
         
    end
  end
end
      
ActionController::Base.send :include, RailsBreadcrumbs::ActionController::BaseMethods
{'fraction_non_alphanumeric': 0.13848073191017465, 'fraction_numerical': 0.005128915996673136, 'mean_word_length': 1.849526066350711, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 47, 'https://': 0, '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': '6602225', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2171, 'n_words': 419}