<commit_before># -*- encoding: utf-8 -*-
class AnalysisTemplateController < ApplicationController

  def index
    list
    render :action => 'list'
  end



  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    if params[:mode] && params[:import_mail_id]
      order_by = []
      if import_mail = ImportMail.find(:first, :conditions => ["id = ?", params[:import_mail_id]])
        order_by << "bp_pic_id = #{import_mail.bp_pic_id} desc" if !import_mail.bp_pic_id.blank?
        order_by << "business_partner_id = #{import_mail.business_partner_id} desc" if !import_mail.business_partner_id.blank?
      end
      order_by << "id desc"
      @analysis_templates = AnalysisTemplate.find(:all, :conditions => "deleted = 0", :order => order_by.join(","))
    else
      @analysis_template_pages, @analysis_templates = paginate :analysis_templates, :conditions => "deleted = 0", :per_page => current_user.per_page
    end
  end

  def show
    @analysis_template = AnalysisTemplate.find(params[:id])
    @analysis_template_items = @analysis_template.analysis_template_items
  end

  def new
    @analysis_template = AnalysisTemplate.new
    @analysis_template.analysis_template_type = params[:mode]
    unless params[:import_mail_id].blank?
      import_mail = ImportMail.find(params[:import_mail_id])
      @analysis_template.business_partner = import_mail.business_partner
      @analysis_template.bp_pic = import_mail.bp_pic
    end
    
    if params[:mode] == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif params[:mode] == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
  end

  def create
    ActiveRecord::Base.transaction do
      @analysis_template = AnalysisTemplate.new(params[:analysis_template])
      @analysis_template.analysis_template_type = params[:mode]
      set_user_column @analysis_template
      @analysis_template.save!
      items = get_items(params[:mode], params)
      items.each do |item|
        item.analysis_template_id = @analysis_template.id
        unless AnalysisTemplateItem.correct_regex(item.pattern)
          flash[:err] = "Invalid Regex pattern"
          raise  ActiveRecord::RecordInvalid.new(item)
        end
        item.save!
      end
    end # transaction
    
    flash[:notice] = 'AnalysisTemplate was successfully created.'
    redirect_to(params[:back_to] || {:action => 'list'})
  rescue ActiveRecord::RecordInvalid
    if params[:mode] == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif params[:mode] == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(get_items(params[:mode], params))
    render :action => 'new', :back_to => back_to
  end

  def edit
    @analysis_template = AnalysisTemplate.find(params[:id])
    @mode = @analysis_template.analysis_template_type

    if @mode == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif @mode == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(@analysis_template.analysis_template_items)
  end

  def update
    ActiveRecord::Base.transaction do
      @analysis_template = AnalysisTemplate.find(params[:id], :conditions =>["deleted = 0"])
      @mode = @analysis_template.analysis_template_type
      @analysis_template.attributes = params[:analysis_template]
      set_user_column @analysis_template
      @analysis_template.save!

      # delete -> insert items
      delete_analysis_template_item(@analysis_template.id)
      items = get_items(@mode, params)
      items.each do |item|
        item.analysis_template_id = @analysis_template.id
        unless AnalysisTemplateItem.correct_regex(item.pattern)
          flash[:err] = "Invalid Regex pattern"
          raise  ActiveRecord::RecordInvalid.new(item)
        end
        item.save!
      end
    end # transaction

    flash[:notice] = 'AnalysisTemplate was successfully updated.'
    
    redirect_to(params[:back_to] || {:action => :show, :id => @analysis_template})
  rescue ActiveRecord::RecordInvalid
    if @mode == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif @mode == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(get_items(@mode, params))

    render :action => 'edit', :id => @analysis_template.id, :back_to => back_to
  end

  def destroy
    @analysis_template = AnalysisTemplate.find(params[:id], :conditions =>["deleted = 0"])
    @analysis_template.deleted = 9
    @analysis_template.deleted_at = Time.now
    set_user_column @analysis_template
    @analysis_template.save!
    <commit_msg>細かい修正<commit_after># -*- encoding: utf-8 -*-
class AnalysisTemplateController < ApplicationController

  def index
    list
    render :action => 'list'
  end



  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    if params[:mode] && params[:import_mail_id]
      order_by = []
      if import_mail = ImportMail.find(:first, :conditions => ["id = ?", params[:import_mail_id]])
        order_by << "bp_pic_id = #{import_mail.bp_pic_id} desc" if !import_mail.bp_pic_id.blank?
        order_by << "business_partner_id = #{import_mail.business_partner_id} desc" if !import_mail.business_partner_id.blank?
      end
      order_by << "id desc"
      @analysis_templates = AnalysisTemplate.find(:all, :conditions => "deleted = 0", :order => order_by.join(","))
    else
      @analysis_template_pages, @analysis_templates = paginate :analysis_templates, :conditions => "deleted = 0", :per_page => current_user.per_page
    end
  end

  def show
    @analysis_template = AnalysisTemplate.find(params[:id])
    @analysis_template_items = @analysis_template.analysis_template_items
  end

  def new
    @analysis_template = AnalysisTemplate.new
    @analysis_template.analysis_template_type = params[:mode]
    unless params[:import_mail_id].blank?
      import_mail = ImportMail.find(params[:import_mail_id])
      @analysis_template.business_partner = import_mail.business_partner
      @analysis_template.bp_pic = import_mail.bp_pic
    end
    
    if params[:mode] == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif params[:mode] == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
  end

  def create
    ActiveRecord::Base.transaction do
      @analysis_template = AnalysisTemplate.new(params[:analysis_template])
      @analysis_template.analysis_template_type = params[:mode]
      set_user_column @analysis_template
      @analysis_template.save!
      items = get_items(params[:mode], params)
      items.each do |item|
        item.analysis_template_id = @analysis_template.id
        unless AnalysisTemplateItem.correct_regex(item.pattern)
          flash[:err] = "Invalid Regex pattern"
          raise  ActiveRecord::RecordInvalid.new(item)
        end
        item.save!
      end
    end # transaction
    
    flash[:notice] = 'AnalysisTemplate was successfully created.'
    redirect_to(params[:back_to] || {:action => 'list'})
  rescue ActiveRecord::RecordInvalid
    if params[:mode] == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif params[:mode] == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(get_items(params[:mode], params))
    render :action => 'new', :back_to => back_to
  end

  def edit
    @analysis_template = AnalysisTemplate.find(params[:id])
    @mode = @analysis_template.analysis_template_type

    if @mode == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif @mode == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(@analysis_template.analysis_template_items)
  end

  def update
    ActiveRecord::Base.transaction do
      @analysis_template = AnalysisTemplate.find(params[:id], :conditions =>["deleted = 0"])
      @mode = @analysis_template.analysis_template_type
      @analysis_template.attributes = params[:analysis_template]
      set_user_column @analysis_template
      @analysis_template.save!

      # delete -> insert items
      delete_analysis_template_item(@analysis_template.id)
      items = get_items(@mode, params)
      items.each do |item|
        item.analysis_template_id = @analysis_template.id
        unless AnalysisTemplateItem.correct_regex(item.pattern)
          flash[:err] = "Invalid Regex pattern"
          raise  ActiveRecord::RecordInvalid.new(item)
        end
        item.save!
      end
    end # transaction

    flash[:notice] = 'AnalysisTemplate was successfully updated.'

    redirect_to(params[:back_to] || {:action => :show, :id => @analysis_template})
  rescue ActiveRecord::RecordInvalid
    if @mode == "biz_offer"
      @business_column_names = get_column_names("businesses")
      @biz_offer_column_names = get_column_names("biz_offers")
    elsif @mode == "bp_member"
      @human_resource_column_names = get_column_names("human_resources")
      @bp_member_column_names = get_column_names("bp_members")
    end
    @item_value_map = get_item_value_map(get_items(@mode, params))

    render :action => 'edit', :id => @analysis_template.id, :back_to => back_to
  end

  def destroy
    @analysis_template = AnalysisTemplate.find(params[:id], :conditions =>["deleted = 0"])
    @analysis_template.deleted = 9
    @analysis_template.deleted_at = Time.now
    set_user_column @analysis_template
    @analysis_template.save!
    
{'fraction_non_alphanumeric': 0.11013698630136987, 'fraction_numerical': 0.002009132420091324, 'mean_word_length': 4.007315957933242, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '126514', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3316, 'n_words': 594}