<commit_before>module Adminable
  class ResourcesController < ApplicationController
    before_action :set_resource
    before_action :set_entry, only: [:edit, :update, :destroy]

    before_action do
      append_view_path(
        [
          Adminable::Engine.root.join('app/views/adminable', controller_name),
          Adminable::Engine.root.join('app/views/adminable/resources')
        ]
      )
    end

    def index
      @q = @resource.model.ransack(params[:q])
      @entries = @q.result.includes(*@resource.includes).all
                   .page(params[:page]).per(25)
    end

    def new
      @entry = @resource.model.new
    end

    def edit
    end

    def create
      @entry = @resource.model.new(resource_params)

      if @entry.save
        redirect_to polymorphic_path(@resource.model),
                    notice: t(
                      'adminable.resources.created',
                      resource: @resource.model.model_name.human
                    )
      else
        flash.now[:alert] = @entry.errors.full_messages
        render :new
      end
    end

    def update
      if @entry.update(resource_params)
        redirect_to polymorphic_path(@resource.model),
                    notice: t(
                      'adminable.resources.updated',
                      resource: @resource.model.model_name.human
                    )
      else
        flash.now[:alert] = @entry.errors.full_messages
        render :edit
      end
    end

    def destroy
      @entry.destroy

      redirect_to polymorphic_path(@resource.model),
                  notice: t(
                    'adminable.resources.deleted',
                    resource: @resource.model.model_name.human
                  )
    end

    private

      def set_resource
        @resource = Adminable::Configuration.find_resource(resource_model).clone

        @resource.attributes.index = index_attributes
        @resource.attributes.form = form_attributes
      end

      def set_entry
        @entry = @resource.model.find(params[:id])
      end

      def index_attributes
        @resource.attributes.index
      end

      def form_attributes
        @resource.attributes.form
      end

      def resource_model
        controller_path.sub(%r{^adminable/}, '')
      end

      def resource_params
        params.require(@resource.model.model_name.param_key).permit(
          *@resource.attributes.form.values.map(&:strong_parameter)
        )<commit_msg>Refactor attributes methods for resources controller<commit_after>module Adminable
  class ResourcesController < ApplicationController
    before_action :set_resource
    before_action :set_entry, only: [:edit, :update, :destroy]

    before_action do
      append_view_path(
        [
          Adminable::Engine.root.join('app/views/adminable', controller_name),
          Adminable::Engine.root.join('app/views/adminable/resources')
        ]
      )
    end

    def index
      @q = @resource.model.ransack(params[:q])
      @entries = @q.result.includes(*@resource.includes).all
                   .page(params[:page]).per(25)
    end

    def new
      @entry = @resource.model.new
    end

    def edit
    end

    def create
      @entry = @resource.model.new(resource_params)

      if @entry.save
        redirect_to polymorphic_path(@resource.model),
                    notice: t(
                      'adminable.resources.created',
                      resource: @resource.model.model_name.human
                    )
      else
        flash.now[:alert] = @entry.errors.full_messages
        render :new
      end
    end

    def update
      if @entry.update(resource_params)
        redirect_to polymorphic_path(@resource.model),
                    notice: t(
                      'adminable.resources.updated',
                      resource: @resource.model.model_name.human
                    )
      else
        flash.now[:alert] = @entry.errors.full_messages
        render :edit
      end
    end

    def destroy
      @entry.destroy

      redirect_to polymorphic_path(@resource.model),
                  notice: t(
                    'adminable.resources.deleted',
                    resource: @resource.model.model_name.human
                  )
    end

    def self.attributes_for(type)
      return unless %i(index form).include?(type)

      before_action do
        yield(@resource.attributes.send(type))
      end
    end

    private

      def set_resource
        @resource = Adminable::Configuration.find_resource(resource_model).clone
      end

      def set_entry
        @entry = @resource.model.find(params[:id])
      end

      def resource_model
        controller_path.sub(%r{^adminable/}, '')
      end

      def resource_params
        params.require(@resource.model.model_name.param_key).permit(
          *@resource.attributes.form.values.map(&:strong_parameter)
        )
{'fraction_non_alphanumeric': 0.09318923327895595, 'fraction_numerical': 0.0008156606851549756, 'mean_word_length': 1.8550640279394646, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1783415', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1283, 'n_words': 236}