<commit_before>module Netzke
  module Basepack
    class FormPanel < Netzke::Base
      module Services
        extend ActiveSupport::Concern

        included do

          #
          # Endpoints

          # Called when the form gets submitted (e.g. by pressing the Apply button)
          endpoint :netzke_submit, :pre => true do |params|
            netzke_submit(params)
          end

          # Can be called when the form needs to load a record with given ID. E.g.:
          #
          #     someForm.netzkeLoad({id: 100});
          endpoint :netzke_load do |params|
            @record = data_class && data_adapter.find_record(params[:id])
            {:set_form_values => js_record_data}
          end

          # Returns options for a combobox
          endpoint :get_combobox_options do |params|
            query = params[:query]

            field = fields[params[:column].to_sym]
            scope = field.to_options[:scope]
            query = params[:query]
            {:data => combobox_options_for_column(field, :query => query, :scope => scope, :record_id => params[:id])}
          end

        end

        # Overriding configuration_panel's get_combobox_options endpoint call
        def configuration_panel__fields__get_combobox_options(params)
          query = params[:query]
          {:data => (default_columns.map{ |c| c[:name].to_s }).grep(/^#{query}/).map{ |n| [n] }}.to_nifty_json
        end

        # Returns array of form values according to the configured columns
        # def array_of_values
        #   @record && @record.to_array(fields)
        # end

        def values
          record && record.to_hash(fields)
        end

        # Implementation for the "netzke_submit" endpoint (for backward compatibility)
        def netzke_submit(params)
          data = ActiveSupport::JSON.decode(params[:data])

          # File uploads are in raw params instead of "data" hash, so, mix them in into "data"
          if config[:file_upload]
            Netzke::Core.controller.params.each_pair do |k,v|
              data[k] = v if v.is_a?(ActionDispatch::Http::UploadedFile)
            end
          end

          success = create_or_update_record(data)

          if success
            {:set_form_values => js_record_data, :set_result => true}
          else
            # flash eventual errors
            @record.errors.to_a.each do |msg|
              flash :error => msg
            end
            {:netzke_feedback => @flash, :apply_form_errors => build_form_errors(record)}
          end
        end

        private

          # Builds the form errors
          def build_form_errors(record)
            form_errors = {}
            foreign_keys = data_adpater.hash_fk_model

            record.errors.map{|field, error|
              # Get the correct field name for the errors on foreign keys
              if foreign_keys.has_key?(field)
                fields.each do |k, v|
                  # Hack to stop to_nifty_json from camalizing model__field
                  field = k.to_s.gsub('__', '____') if k.to_s.split('__').first == foreign_keys[field].to_s
                end
              end

              form_errors[field] ||= []
              form_errors[field] << error
            }
            form_errors
          end

          # Creates/updates a record from hash
          def create_or_update_record(hsh)
            hsh.merge!(config[:strong_default_attrs]) if config[:strong_default_attrs]
            @record ||= data_adapter.find_record hsh.delete(data_class.primary_key) # only pick up the record specified in the params if it was not provided in the configuration
              #data_class.find(:first, :conditions => {data_class.primary_key => hsh.delete(data_class.primary_key)}) <commit_msg>Editing in form now works<commit_after>module Netzke
  module Basepack
    class FormPanel < Netzke::Base
      module Services
        extend ActiveSupport::Concern

        included do

          #
          # Endpoints

          # Called when the form gets submitted (e.g. by pressing the Apply button)
          endpoint :netzke_submit, :pre => true do |params|
            netzke_submit(params)
          end

          # Can be called when the form needs to load a record with given ID. E.g.:
          #
          #     someForm.netzkeLoad({id: 100});
          endpoint :netzke_load do |params|
            @record = data_class && data_adapter.find_record(params[:id])
            {:set_form_values => js_record_data}
          end

          # Returns options for a combobox
          endpoint :get_combobox_options do |params|
            query = params[:query]

            field = fields[params[:column].to_sym]
            scope = field.to_options[:scope]
            query = params[:query]
            {:data => combobox_options_for_column(field, :query => query, :scope => scope, :record_id => params[:id])}
          end

        end

        # Overriding configuration_panel's get_combobox_options endpoint call
        def configuration_panel__fields__get_combobox_options(params)
          query = params[:query]
          {:data => (default_columns.map{ |c| c[:name].to_s }).grep(/^#{query}/).map{ |n| [n] }}.to_nifty_json
        end

        # Returns array of form values according to the configured columns
        # def array_of_values
        #   @record && @record.to_array(fields)
        # end

        def values
          record && record.to_hash(fields)
        end

        # Implementation for the "netzke_submit" endpoint (for backward compatibility)
        def netzke_submit(params)
          data = ActiveSupport::JSON.decode(params[:data])

          # File uploads are in raw params instead of "data" hash, so, mix them in into "data"
          if config[:file_upload]
            Netzke::Core.controller.params.each_pair do |k,v|
              data[k] = v if v.is_a?(ActionDispatch::Http::UploadedFile)
            end
          end

          success = create_or_update_record(data)

          if success
            {:set_form_values => js_record_data, :set_result => true}
          else
            # flash eventual errors
            @record.errors.to_a.each do |msg|
              flash :error => msg
            end
            {:netzke_feedback => @flash, :apply_form_errors => build_form_errors(record)}
          end
        end

        private

          # Builds the form errors
          def build_form_errors(record)
            form_errors = {}
            foreign_keys = data_adapter.hash_fk_model

            record.errors.map{|field, error|
              # Get the correct field name for the errors on foreign keys
              if foreign_keys.has_key?(field)
                fields.each do |k, v|
                  # Hack to stop to_nifty_json from camalizing model__field
                  field = k.to_s.gsub('__', '____') if k.to_s.split('__').first == foreign_keys[field].to_s
                end
              end

              form_errors[field] ||= []
              form_errors[field] << error
            }
            form_errors
          end

          # Creates/updates a record from hash
          def create_or_update_record(hsh)
            hsh.merge!(config[:strong_default_attrs]) if config[:strong_default_attrs]
            @record ||= data_adapter.find_record hsh.delete(data_class.primary_key) # only pick up the record specified in the params if it was not provided in the configuration
              #data_class.find(:first, :conditions => {data_class.primary_key => hsh.delete(data_class.primary_key)}) 
{'fraction_non_alphanumeric': 0.08927628103539356, 'fraction_numerical': 0.000792393026941363, 'mean_word_length': 1.9639921722113503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, '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': '5989731', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2064, 'n_words': 548}