<commit_before>require_dependency "para/application_controller"

module Para
  module Admin
    class ResourcesController < Para::Admin::BaseController
      include Para::ModelHelper

      class_attribute :resource_name, :resource_class
      load_and_authorize_component
      helper_method :resource

      def new
        render 'para/admin/resources/new'
      end

      def create
        # Assign component the resource belongs to it
        resource.component = @component if resource.respond_to?(:component=)

        if resource.save
          flash_message(:success, resource)
          redirect_to after_form_submit_path
        else
          flash_message(:error, resource)
          render 'new'
        end
      end

      def edit
        render 'para/admin/resources/edit'
      end

      def update
        if resource.update_attributes(resource_params)
          flash_message(:success, resource)
          redirect_to after_form_submit_path
        else
          flash_message(:error, resource)
          render 'edit'
        end
      end

      def destroy
        resource.destroy
        flash_message(:success, resource)
        redirect_to @component.path
      end

      def order
        ActiveRecord::Base.transaction do
          resources_data.each do |resource_params|
            resource = resources_hash[resource_params[:id]]
            resource.position = resource_params[:position].to_i
            resource.save(validate: false)
          end
        end

        head 200
      end

      def tree
        ActiveRecord::Base.transaction do
          resources_data.each do |resource_params|
            resource = resources_hash[resource_params[:id]]
            resource.assign_attributes(
              position: resource_params[:position].to_i,
              parent_id: resource_params[:parent_id]
            )
            resource.save(validate: false)
          end
        end

        head 200
      end

      private

      def after_form_submit_path
        if params[:_save_and_edit]
          { action: 'edit', id: resource.id }
        elsif params[:_save_and_add_another]
          { action: 'new' }
        else
          params.delete(:return_to).presence || @component.path
        end
      end

      def resource_model
        self.class.resource_model
      end

      def resource
        @resource ||= begin
          self.class.ensure_resource_name_defined!
          instance_variable_get(:"@#{ self.class.resource_name }")
        end
      end

      def resource_params
        @resource_params ||= parse_resource_params(
          (params[:resource] && params.require(:resource).permit!) || {}
        )
      end

      def parse_resource_params(hash)
        model_field_mappings(resource_model).fields.each do |field|
          field.parse_input(hash)
        end<commit_msg>explain resources controller #resource_params fix<commit_after>require_dependency "para/application_controller"

module Para
  module Admin
    class ResourcesController < Para::Admin::BaseController
      include Para::ModelHelper

      class_attribute :resource_name, :resource_class
      load_and_authorize_component
      helper_method :resource

      def new
        render 'para/admin/resources/new'
      end

      def create
        # Assign component the resource belongs to it
        resource.component = @component if resource.respond_to?(:component=)

        if resource.save
          flash_message(:success, resource)
          redirect_to after_form_submit_path
        else
          flash_message(:error, resource)
          render 'new'
        end
      end

      def edit
        render 'para/admin/resources/edit'
      end

      def update
        if resource.update_attributes(resource_params)
          flash_message(:success, resource)
          redirect_to after_form_submit_path
        else
          flash_message(:error, resource)
          render 'edit'
        end
      end

      def destroy
        resource.destroy
        flash_message(:success, resource)
        redirect_to @component.path
      end

      def order
        ActiveRecord::Base.transaction do
          resources_data.each do |resource_params|
            resource = resources_hash[resource_params[:id]]
            resource.position = resource_params[:position].to_i
            resource.save(validate: false)
          end
        end

        head 200
      end

      def tree
        ActiveRecord::Base.transaction do
          resources_data.each do |resource_params|
            resource = resources_hash[resource_params[:id]]
            resource.assign_attributes(
              position: resource_params[:position].to_i,
              parent_id: resource_params[:parent_id]
            )
            resource.save(validate: false)
          end
        end

        head 200
      end

      private

      def after_form_submit_path
        if params[:_save_and_edit]
          { action: 'edit', id: resource.id }
        elsif params[:_save_and_add_another]
          { action: 'new' }
        else
          params.delete(:return_to).presence || @component.path
        end
      end

      def resource_model
        self.class.resource_model
      end

      def resource
        @resource ||= begin
          self.class.ensure_resource_name_defined!
          instance_variable_get(:"@#{ self.class.resource_name }")
        end
      end

      # We check for the existance of `params[:resource]` to avoid rails'
      # params sanitizer to raise if no `:resource` key is found. We prefer
      # to let the model handle validations instead of asking the controller
      # to validate some params presence
      #
      # Note : This also created a bug with cancan which tried to build the
      #        params on a `new` action, that did not contain the `:resource`
      #        key in the params, which made the `#new` action form raise
      #
      def resource_params
        @resource_params ||= parse_resource_params(
          (params[:resource] && params.require(:resource).permit!) || {}
        )
      end

      def parse_resource_params(hash)
        model_field_mappings(resource_model).fields.each do |field|
          field.parse_input(hash)
        end
{'fraction_non_alphanumeric': 0.06916932907348243, 'fraction_numerical': 0.0019169329073482429, 'mean_word_length': 2.0437530384054448, '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': '2268385', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1583, 'n_words': 396}