<commit_before># encoding: UTF-8
# Handle CRUD dealing with Customers

class CustomersController < ApplicationController
  before_filter :authorize_user_can_create_customers, :only => [:new, :create]
  before_filter :authorize_user_can_edit_customers,   :only => [:edit, :update, :destroy]
  before_filter :authorize_user_can_read_customers,   :only => [:show]

  def show
    @customer = Customer.from_company(current_user.company_id).find(params[:id])
  end

  def new
    @customer = current_user.company.customers.new
  end

  def create
    @customer         = Customer.new(params[:customer])
    @customer.company = current_user.company

    if @customer.save
      flash['notice'] = _('Customer was successfully created.')
      redirect_to root_path
    else
      render :new
    end
  end

  def edit
    @customer = Customer.from_company(current_user.company_id).where(:id => params[:id]).includes(:projects).first
  end

  def update
    @customer = Customer.from_company(current_user.company_id).find(params[:id])

    if @customer.update_attributes(params[:customer])
      flash['notice'] = _('Customer was successfully updated.')
      redirect_to :action => :edit, :id => @customer.id
    else
      render :edit
    end
  end  

  def destroy
    @customer = Customer.from_company(current_user.company_id).find(params[:id])

    if @customer.has_projects?
      flash['notice'] = 
        _("Please delete all projects for #{@customer.name} before deleting it.")

    #TODO: What the ... ?
    elsif @customer.name == current_user.company.name
      flash['notice'] = _("You can't delete your own company.")

    else
      flash['notice'] = _("Customer was successfully deleted.")
      @customer.destroy
    end

    redirect_to root_path
  end

  def search
    search_criteria = params[:term].strip

    @customers = []
    @users = []
    @tasks = []
    @projects = []
    @resources = []
    @limit = 5
    unless search_criteria.blank?
      if search_criteria.to_i > 0
        @tasks = current_user.company.tasks.where(:task_num => search_criteria)
      elsif params[:entity]
        @limit = 100000
        if params[:entity] =~ /user/
          @users = current_user.company.users.where('lower(name) LIKE ?', '%' + search_criteria.downcase + '%').where(:active => true)
        elsif params[:entity] =~ /customer/
          @customers = current_user.company.customers.where('lower(name) LIKE ?', '%' + search_criteria.downcase + '%').where(:active => true)
        elsif params[:entity] =~ /task/
          @tasks = Task.all_accessed_by(current_user).where('lower(tasks.name) LIKE ?', '%' + search_criteria.downcase + '%').where("tasks.status = 0")
        elsif params[:entity] =~ /resource/
          @resources = current_user.company.resources.where('lower(name) like ?', '%' + search_criteria.downcase + '%')
        elsif params[:entity] =~ /project/
          @projects = current_user.company.projects.where('lower(name) like ?', '%' + search_criteria.downcase + '%')
        end
      else<commit_msg>only query user accessible tasks<commit_after># encoding: UTF-8
# Handle CRUD dealing with Customers

class CustomersController < ApplicationController
  before_filter :authorize_user_can_create_customers, :only => [:new, :create]
  before_filter :authorize_user_can_edit_customers,   :only => [:edit, :update, :destroy]
  before_filter :authorize_user_can_read_customers,   :only => [:show]

  def show
    @customer = Customer.from_company(current_user.company_id).find(params[:id])
  end

  def new
    @customer = current_user.company.customers.new
  end

  def create
    @customer         = Customer.new(params[:customer])
    @customer.company = current_user.company

    if @customer.save
      flash['notice'] = _('Customer was successfully created.')
      redirect_to root_path
    else
      render :new
    end
  end

  def edit
    @customer = Customer.from_company(current_user.company_id).where(:id => params[:id]).includes(:projects).first
  end

  def update
    @customer = Customer.from_company(current_user.company_id).find(params[:id])

    if @customer.update_attributes(params[:customer])
      flash['notice'] = _('Customer was successfully updated.')
      redirect_to :action => :edit, :id => @customer.id
    else
      render :edit
    end
  end  

  def destroy
    @customer = Customer.from_company(current_user.company_id).find(params[:id])

    if @customer.has_projects?
      flash['notice'] = 
        _("Please delete all projects for #{@customer.name} before deleting it.")

    #TODO: What the ... ?
    elsif @customer.name == current_user.company.name
      flash['notice'] = _("You can't delete your own company.")

    else
      flash['notice'] = _("Customer was successfully deleted.")
      @customer.destroy
    end

    redirect_to root_path
  end

  def search
    search_criteria = params[:term].strip

    @customers = []
    @users = []
    @tasks = []
    @projects = []
    @resources = []
    @limit = 5
    unless search_criteria.blank?
      if search_criteria.to_i > 0
        @tasks = Task.all_accessed_by(current_user).where(:task_num => search_criteria)
      elsif params[:entity]
        @limit = 100000
        if params[:entity] =~ /user/
          @users = current_user.company.users.where('lower(name) LIKE ?', '%' + search_criteria.downcase + '%').where(:active => true)
        elsif params[:entity] =~ /customer/
          @customers = current_user.company.customers.where('lower(name) LIKE ?', '%' + search_criteria.downcase + '%').where(:active => true)
        elsif params[:entity] =~ /task/
          @tasks = Task.all_accessed_by(current_user).where('lower(tasks.name) LIKE ?', '%' + search_criteria.downcase + '%').where("tasks.status = 0")
        elsif params[:entity] =~ /resource/
          @resources = current_user.company.resources.where('lower(name) like ?', '%' + search_criteria.downcase + '%')
        elsif params[:entity] =~ /project/
          @projects = current_user.company.projects.where('lower(name) like ?', '%' + search_criteria.downcase + '%')
        end
      else
{'fraction_non_alphanumeric': 0.13681795838112404, 'fraction_numerical': 0.003277076847452073, 'mean_word_length': 3.2865168539325844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '5900728', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1823, 'n_words': 381}