<commit_before>module ActiveScaffold::Actions
  module Create
    def self.included(base)
      base.before_filter :create_authorized_filter, :only => [:new, :create]
    end

    def new
      do_new
      respond_to_action(:new)
    end

    def create
      do_create
      respond_to_action(:create)
    end

    protected
    def response_location
      url_for(params_for(:action => "show", :id => @record.id)) if successful?
    end

    def new_respond_to_html
      if successful?
        render(:action => 'create')
      else
        return_to_main
      end
    end
    
    def new_respond_to_js
      render(:partial => 'create_form')
    end

    def create_respond_to_html
      if params[:iframe]=='true' # was this an iframe post ?
        do_refresh_list if successful? && active_scaffold_config.create.refresh_list && !render_parent?
        responds_to_parent do
          render :action => 'on_create', :formats => [:js], :layout => false
        end
      else
        if successful?
          flash[:info] = as_(:created_model, :model => @record.to_label)
          if action = active_scaffold_config.create.action_after_create
            redirect_to params_for(:action => action, :id => @record.id)
          elsif active_scaffold_config.create.persistent
            redirect_to params_for(:action => "new")
          else
            return_to_main
          end
        else
          if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.always_show_create
            list
          else
            render(:action => 'create')
          end
        end
      end
    end

    def create_respond_to_js
      do_refresh_list if successful? && active_scaffold_config.create.refresh_list && !render_parent?
      render :action => 'on_create'
    end

    def create_respond_to_xml
      render :xml => response_object.to_xml(:only => active_scaffold_config.create.columns.names), :content_type => Mime::XML, :status => response_status, :location => response_location
    end

    def create_respond_to_json
      render :text => response_object.to_json(:only => active_scaffold_config.create.columns.names), :content_type => Mime::JSON, :status => response_status, :location => response_location
    end

    def create_respond_to_yaml
      render :text => Hash.from_xml(response_object.to_xml(:only => active_scaffold_config.create.columns.names)).to_yaml, :content_type => Mime::YAML, :status => response_status, :location => response_location
    end

    # A simple method to find and prepare an example new record for the form
    # May be overridden to customize the behavior (add default values, for instance)
    def do_new
      @record = new_model
      apply_constraints_to_record(@record)
      create_association_with_parent(@record) if nested?
      @record
    end

    # A somewhat complex method to actually create a new record. The complexity is from support for subforms and associated records.
    # If you want to customize this behavior, consider using the +before_create_save+ and +after_create_save+ callbacks.
    def do_create(options = {})
      attributes = options[:attributes] || params[:record]
      begin
        active_scaffold_config.model.transaction do
          @record = update_record_from_params(new_model, active_scaffold_config.create.columns, attributes)
          apply_constraints_to_record(@record, :allow_autosave => true)
          create_association_with_parent(@record) if nested?
          before_create_save(@record)
          self.successful = [@record.valid?, @record.associated_valid?].all? # this syntax avoids a short-circuit
          create_save(@record) unless options[:skip_save]
        end
      rescue ActiveRecord::ActiveRecordError => ex
        flash[:error] = ex.message
        self.successful = false<commit_msg>fix when exception is raised on update_record_from_params<commit_after>module ActiveScaffold::Actions
  module Create
    def self.included(base)
      base.before_filter :create_authorized_filter, :only => [:new, :create]
    end

    def new
      do_new
      respond_to_action(:new)
    end

    def create
      do_create
      respond_to_action(:create)
    end

    protected
    def response_location
      url_for(params_for(:action => "show", :id => @record.id)) if successful?
    end

    def new_respond_to_html
      if successful?
        render(:action => 'create')
      else
        return_to_main
      end
    end
    
    def new_respond_to_js
      render(:partial => 'create_form')
    end

    def create_respond_to_html
      if params[:iframe]=='true' # was this an iframe post ?
        do_refresh_list if successful? && active_scaffold_config.create.refresh_list && !render_parent?
        responds_to_parent do
          render :action => 'on_create', :formats => [:js], :layout => false
        end
      else
        if successful?
          flash[:info] = as_(:created_model, :model => @record.to_label)
          if action = active_scaffold_config.create.action_after_create
            redirect_to params_for(:action => action, :id => @record.id)
          elsif active_scaffold_config.create.persistent
            redirect_to params_for(:action => "new")
          else
            return_to_main
          end
        else
          if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.always_show_create
            list
          else
            render(:action => 'create')
          end
        end
      end
    end

    def create_respond_to_js
      do_refresh_list if successful? && active_scaffold_config.create.refresh_list && !render_parent?
      render :action => 'on_create'
    end

    def create_respond_to_xml
      render :xml => response_object.to_xml(:only => active_scaffold_config.create.columns.names), :content_type => Mime::XML, :status => response_status, :location => response_location
    end

    def create_respond_to_json
      render :text => response_object.to_json(:only => active_scaffold_config.create.columns.names), :content_type => Mime::JSON, :status => response_status, :location => response_location
    end

    def create_respond_to_yaml
      render :text => Hash.from_xml(response_object.to_xml(:only => active_scaffold_config.create.columns.names)).to_yaml, :content_type => Mime::YAML, :status => response_status, :location => response_location
    end

    # A simple method to find and prepare an example new record for the form
    # May be overridden to customize the behavior (add default values, for instance)
    def do_new
      @record = new_model
      apply_constraints_to_record(@record)
      create_association_with_parent(@record) if nested?
      @record
    end

    # A somewhat complex method to actually create a new record. The complexity is from support for subforms and associated records.
    # If you want to customize this behavior, consider using the +before_create_save+ and +after_create_save+ callbacks.
    def do_create(options = {})
      attributes = options[:attributes] || params[:record]
      begin
        active_scaffold_config.model.transaction do
          @record = update_record_from_params(new_model, active_scaffold_config.create.columns, attributes)
          apply_constraints_to_record(@record, :allow_autosave => true)
          create_association_with_parent(@record) if nested?
          before_create_save(@record)
          self.successful = [@record.valid?, @record.associated_valid?].all? # this syntax avoids a short-circuit
          create_save(@record) unless options[:skip_save]
        end
      rescue ActiveRecord::ActiveRecordError => ex
        flash[:error] = ex.message
        self.successful = false
        @record ||= new_model # ensure @record exists or display form will fail
{'fraction_non_alphanumeric': 0.0866444501409895, 'fraction_numerical': 0.0, 'mean_word_length': 3.0242392986075295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '5774907', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2193, 'n_words': 543}