<commit_before>module Voluntary
  module V1
    module BaseController
      extend ActiveSupport::Concern
      
      included do
        rescue_from CanCan::AccessDenied, with: :access_denied
        rescue_from ActiveRecord::RecordNotFound, with: :not_found
        rescue_from Mongoid::Errors::DocumentNotFound, with: :not_found
        
        helper_method :parent, :application_navigation, :navigation_product_path, :navigation_product_name
      end
      
      def parent
        @parent
      end
      
      protected
      
      def application_navigation
        :main
      end
      
      def navigation_product_path
        '/'
      end
      
      def navigation_product_name
        'Core'
      end
      
      def current_ability
        Ability.new(current_user, controller_namespace: current_namespace)
      end
      
      def find_parent(types, parent_key = nil)
        parent_type, id = nil, nil
        
        if parent_type = types.select{|p| params.keys.include?("#{p}_id") }.first
          id = params["#{parent_type}_id"]
        elsif parent_type = types.select{|p| params[parent_key] && params[parent_key].keys.include?("#{p}_id") }.first
          id = params[parent_key]["#{parent_type}_id"]
        end
        
        return if parent_type.blank?
        
        parent = parent_type.classify.constantize
        parent = parent.friendly if parent.respond_to? :friendly
        parent = parent.find(id)
        
        root_model_class_name = Voluntary::ApplicationHelper.root_model_class_name_helper(parent)
        eval("@#{root_model_class_name.tableize.singularize} = parent") 
        
        parent
      end
      
      def response_with_standard(format = nil, error = nil)
        render status: error ? 500 : 200, json: { success: error ? false : true, error: error} and return true
      end
      
      private
      
      def current_namespace
        controller_name_segments = params[:controller].split('/')
        controller_name_segments.pop
        controller_namespace = controller_name_segments.join('/').downcase
      end
      
      def access_denied
        message = I18n.t('general.exceptions.access_denied')
        
        if request.format.try('json?')
          render status: 403, json: { error: message } and return
        else
          flash[:alert] = message
          
          if request.env["HTTP_REFERER"]
            redirect_to :back
          else
            redirect_to root_path
          end
        end
      end
      
      def not_found(e)
        if Rails.env.development?
          raise e
        else
          logger.info "not found (#{e.inspect})"
          redirect_to root_path, notice: t('general.exceptions.not_found')
        end
      end
    end
  end
end<commit_msg>Show error json for xhr requests on access denied.<commit_after>module Voluntary
  module V1
    module BaseController
      extend ActiveSupport::Concern
      
      included do
        rescue_from CanCan::AccessDenied, with: :access_denied
        rescue_from ActiveRecord::RecordNotFound, with: :not_found
        rescue_from Mongoid::Errors::DocumentNotFound, with: :not_found
        
        helper_method :parent, :application_navigation, :navigation_product_path, :navigation_product_name
      end
      
      def parent
        @parent
      end
      
      protected
      
      def application_navigation
        :main
      end
      
      def navigation_product_path
        '/'
      end
      
      def navigation_product_name
        'Core'
      end
      
      def current_ability
        Ability.new(current_user, controller_namespace: current_namespace)
      end
      
      def find_parent(types, parent_key = nil)
        parent_type, id = nil, nil
        
        if parent_type = types.select{|p| params.keys.include?("#{p}_id") }.first
          id = params["#{parent_type}_id"]
        elsif parent_type = types.select{|p| params[parent_key] && params[parent_key].keys.include?("#{p}_id") }.first
          id = params[parent_key]["#{parent_type}_id"]
        end
        
        return if parent_type.blank?
        
        parent = parent_type.classify.constantize
        parent = parent.friendly if parent.respond_to? :friendly
        parent = parent.find(id)
        
        root_model_class_name = Voluntary::ApplicationHelper.root_model_class_name_helper(parent)
        eval("@#{root_model_class_name.tableize.singularize} = parent") 
        
        parent
      end
      
      def response_with_standard(format = nil, error = nil)
        render status: error ? 500 : 200, json: { success: error ? false : true, error: error} and return true
      end
      
      private
      
      def current_namespace
        controller_name_segments = params[:controller].split('/')
        controller_name_segments.pop
        controller_namespace = controller_name_segments.join('/').downcase
      end
      
      def access_denied
        message = I18n.t('general.exceptions.access_denied')
        
        if request.format.try('json?') || request.xhr?
          render status: 403, json: { error: message } and return
        else
          flash[:alert] = message
          
          if request.env["HTTP_REFERER"]
            redirect_to :back
          else
            redirect_to root_path
          end
        end
      end
      
      def not_found(e)
        if Rails.env.development?
          raise e
        else
          logger.info "not found (#{e.inspect})"
          redirect_to root_path, notice: t('general.exceptions.not_found')
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0811002661934339, 'fraction_numerical': 0.004259094942324756, 'mean_word_length': 2.0058666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1214023', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1561, 'n_words': 346}