<commit_before>require 'active_support/concern'

module Brightcontent
  module BaseControllerExt
    module DefaultActions
      extend ActiveSupport::Concern

      included do
        before_action :set_collection
        before_action :set_resource, only: [:show, :edit, :update, :destroy]
        helper_method :resource, :resource_class, :resource_index_path, :resource_path, :collection, :resource_edit_path
      end

      def base_collection
        instance_variable_get :"@#{resource_name.pluralize}"
      end

      def collection
        base_collection
      end

      def collection=(val)
        instance_variable_set :"@#{resource_name.pluralize}", val
      end

      def resource
        instance_variable_get :"@#{resource_instance_name}"
      end

      def resource=(val)
        instance_variable_set :"@#{resource_instance_name}", val
      end

      def resource_class
        controller_name.classify.constantize
      end

      def resource_name
        controller_name
      end

      def resource_instance_name
        resource_name.singularize
      end

      def index
      end

      def show
        redirect_to action: :edit
      end

      def new
        self.resource = base_collection.new
      end

      def edit
      end

      def create
        self.resource = base_collection.new(resource_params)
        if resource.save
          redirect_to resource_redirect_path, notice: t("flash.actions.create.notice", resource_name: resource_instance_name.capitalize)
        else
          render :new
        end
      end

      def update
        if resource.update(resource_params)
          redirect_to resource_redirect_path, notice: t("flash.actions.update.notice", resource_name: resource_instance_name.capitalize)
        else
          render :edit
        end
      end

      def destroy
        if resource.destroy
          redirect_to resource_index_path, notice: t("flash.actions.destroy.notice", resource_name: resource_instance_name.capitalize)
        else
          redirect_to resource_index_path, alert: t("flash.actions.destroy.alert", resource_name: resource_instance_name.capitalize)
        end
      end

      private

      def set_collection
        self.collection = resource_class.all
      end

      def set_resource
        self.resource = base_collection.find(params[:id])
      end

      def resource_params
        params.require(resource_instance_name).permit!
      end

      def resource_index_path
        [brightcontent, resource_class]
      end

      def resource_edit_path(res = resource)
        [brightcontent, :edit, res]
      end

      def resource_redirect_path<commit_msg>always try set resource, not only for the default instance actions<commit_after>require 'active_support/concern'

module Brightcontent
  module BaseControllerExt
    module DefaultActions
      extend ActiveSupport::Concern

      included do
        before_action :set_collection
        before_action :set_resource
        helper_method :resource, :resource_class, :resource_index_path, :resource_path, :collection, :resource_edit_path
      end

      def base_collection
        instance_variable_get :"@#{resource_name.pluralize}"
      end

      def collection
        base_collection
      end

      def collection=(val)
        instance_variable_set :"@#{resource_name.pluralize}", val
      end

      def resource
        instance_variable_get :"@#{resource_instance_name}"
      end

      def resource=(val)
        instance_variable_set :"@#{resource_instance_name}", val
      end

      def resource_class
        controller_name.classify.constantize
      end

      def resource_name
        controller_name
      end

      def resource_instance_name
        resource_name.singularize
      end

      def index
      end

      def show
        redirect_to action: :edit
      end

      def new
        self.resource = base_collection.new
      end

      def edit
      end

      def create
        self.resource = base_collection.new(resource_params)
        if resource.save
          redirect_to resource_redirect_path, notice: t("flash.actions.create.notice", resource_name: resource_instance_name.capitalize)
        else
          render :new
        end
      end

      def update
        if resource.update(resource_params)
          redirect_to resource_redirect_path, notice: t("flash.actions.update.notice", resource_name: resource_instance_name.capitalize)
        else
          render :edit
        end
      end

      def destroy
        if resource.destroy
          redirect_to resource_index_path, notice: t("flash.actions.destroy.notice", resource_name: resource_instance_name.capitalize)
        else
          redirect_to resource_index_path, alert: t("flash.actions.destroy.alert", resource_name: resource_instance_name.capitalize)
        end
      end

      private

      def set_collection
        self.collection = resource_class.all
      end

      def set_resource
        self.resource = base_collection.find(params[:id]) if params[:id].present?
      end

      def resource_params
        params.require(resource_instance_name).permit!
      end

      def resource_index_path
        [brightcontent, resource_class]
      end

      def resource_edit_path(res = resource)
        [brightcontent, :edit, res]
      end

      def resource_redirect_path
{'fraction_non_alphanumeric': 0.0603192279138827, 'fraction_numerical': 0.0, 'mean_word_length': 2.4042956411876184, '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': '979579', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1408, 'n_words': 334}