<commit_before>class SamplesController < EntitiesController
  include SamplesHelper
  before_filter :load_settings
  before_filter :get_data_for_sidebar, :only => :index

  def index
    @samples = get_samples(:page => params[:page], :per_page => params[:per_page])

    respond_with @samples do |format|
      format.xls { render :layout => 'header'}
      format.csv { render :csv => @samples}
    end
  end

  def show
    @timeline = timeline(@sample)
    @pricing = Setting.unroll(:sample_pricing)
    respond_with(@sample)
  end

  def new
    opp = params[:related].split('_').last.to_i if params[:related]
    @pc_names = rits_pc_names
    @sample.attributes = {:user => current_user, :opportunity => Opportunity.find(opp)}

    respond_with(@sample)
    # @pricing = Setting.unroll(:sample_pricing)
  end

  def create
    @comment_body = params[:comment_body]
    if rits_pc_hash.fetch(@sample.rits_purchase_contract_id, "Error") != "Error"
      info = rits_pc_hash.fetch(@sample.rits_purchase_contract_id)
      params[:sample].merge!(info)
    end

    @sample = Sample.new(params[:sample])
    # @sample.delivery_month = params[:date]

    respond_with(@sample) do |_format|
      if @sample.save
        if @sample.follow_up_date.present?
          follow_up_task(@sample)
        end
        @sample.add_comment_by_user(@comment_body, current_user)
      else
        @pc_names = rits_pc_names
      end

    end

    # if @sample.save
    #   if @sample.follow_up_date.present?
    #     follow_up_task(@sample)
    #   end
    #   @sample.add_comment_by_user(@comment_body, current_user)
    #   redirect_to opportunity_path(@sample.opportunity)
    # else
    #   flash[:warning] = errors_format(@sample.errors.messages)
    #   redirect_to opportunity_path(@sample.opportunity)
    # end
  end

  def edit
    @pc_names = rits_pc_names
    @pricing = Setting.unroll(:sample_pricing)
    if params[:previous].to_s =~ /(\d+)\z/
      @previous = Sample.my.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i
    end
    respond_to do |format|
      format.js {render :edit}
    end
  end

  def update
    old_sample_date = @sample.follow_up_date
    if rits_pc_hash.fetch(@sample.rits_purchase_contract_id, "Error") != "Error"
      info = rits_pc_hash.fetch(@sample.rits_purchase_contract_id)
      params[:sample].merge!(info)
    end
    if @sample.update_attributes(resource_params)
      flash[:notice] = @sample.name + " updated."
      if @sample.follow_up_date != old_sample_date
        follow_up_task(@sample)
      end
    else
      flash[:error] = "Update Failed. " + errors_format(@sample.errors.messages)
    end

    if request.referer.include?("sample")
      redirect_to sample_path(@sample)
    else
      redirect_to opportunity_path(@sample.opportunity)
    end<commit_msg>Fix Sample update controller with RITS call and sidebar update<commit_after>class SamplesController < EntitiesController
  include SamplesHelper
  before_filter :load_settings
  before_filter :get_data_for_sidebar, :only => :index

  def index
    @samples = get_samples(:page => params[:page], :per_page => params[:per_page])

    respond_with @samples do |format|
      format.xls { render :layout => 'header'}
      format.csv { render :csv => @samples}
    end
  end

  def show
    @timeline = timeline(@sample)
    @pricing = Setting.unroll(:sample_pricing)
    respond_with(@sample)
  end

  def new
    opp = params[:related].split('_').last.to_i if params[:related]
    @pc_names = rits_pc_names
    @sample.attributes = {:user => current_user, :opportunity => Opportunity.find(opp)}

    respond_with(@sample)
    # @pricing = Setting.unroll(:sample_pricing)
  end

  def create
    @comment_body = params[:comment_body]
    if rits_pc_hash.fetch(@sample.rits_purchase_contract_id, "Error") != "Error"
      info = rits_pc_hash.fetch(@sample.rits_purchase_contract_id)
      params[:sample].merge!(info)
    end

    @sample = Sample.new(params[:sample])
    # @sample.delivery_month = params[:date]

    respond_with(@sample) do |_format|
      if @sample.save
        if @sample.follow_up_date.present?
          follow_up_task(@sample)
        end
        @sample.add_comment_by_user(@comment_body, current_user)
      else
        @pc_names = rits_pc_names
      end

    end

    # if @sample.save
    #   if @sample.follow_up_date.present?
    #     follow_up_task(@sample)
    #   end
    #   @sample.add_comment_by_user(@comment_body, current_user)
    #   redirect_to opportunity_path(@sample.opportunity)
    # else
    #   flash[:warning] = errors_format(@sample.errors.messages)
    #   redirect_to opportunity_path(@sample.opportunity)
    # end
  end

  def edit
    @pc_names = rits_pc_names
    @pricing = Setting.unroll(:sample_pricing)
    if params[:previous].to_s =~ /(\d+)\z/
      @previous = Sample.my.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i
    end
    respond_to do |format|
      format.js {render :edit}
    end
  end

  def update
    old_sample_date = @sample.follow_up_date
    if rits_pc_hash.fetch(@sample.rits_purchase_contract_id, "Error") != "Error"
      info = rits_pc_hash.fetch(@sample.rits_purchase_contract_id)
      params[:sample].merge!(info)
    end
    # if @sample.update_attributes(resource_params)
    #   flash[:notice] = @sample.name + " updated."
    #   if @sample.follow_up_date != old_sample_date
    #     follow_up_task(@sample)
    #   end
    # else
    #   flash[:error] = "Update Failed. " + errors_format(@sample.errors.messages)
    # end

    respond_with(@sample) do |_format|
      if @sample.update_attributes(resource_params)
        if @sample.follow_up_date != old_sample_date
          follow_up_task(@sample)
        end
        get_data_for_sidebar if called_from_index_page?
      else
        @pc_names = rits_pc_names
      end
    end

    # if request.referer.include?("sample")
    #   redirect_to sample_path(@sample)
    # else
    #   redirect_to opportunity_path(@sample.opportunity)
    # end
{'fraction_non_alphanumeric': 0.11407726745150058, 'fraction_numerical': 0.000663239927043608, 'mean_word_length': 3.2659123055162658, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2131854', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1975, 'n_words': 352}