<commit_before>class Hoshin < ActiveRecord::Base
  
  include ModelBase

  hobo_model # Don't put anything above this

  fields do
    name :string, :default => 'plan', :null => false
    goals_count :integer, :default => 0, :null => false
    areas_count :integer, :default => 0, :null => false
    objectives_count :integer, :default => 0, :null => false
    indicators_count :integer, :default => 0, :null => false
    outdated_indicators_count :integer, :default => 0, :null => false
    outdated_tasks_count :integer, :default => 0, :null => false
    blind_objectives_count :integer, :default => 0, :null => false
    neglected_objectives_count :integer, :default => 0, :null => false
    tasks_count :integer, :default => 0, :null => false
    objectives_count :integer, :default => 0, :null => false
    hoshins_count :integer, :default => 0, :null => false
    header HoboFields::Types::TextileString
    timestamps
  end
  index [:company_id, :parent_id]
  
  attr_accessible :name, :id, :parent, :parent_id, :company, :company_id, :header
  attr_accessible :areas, :children, :children_ids, :creator_id, :creator
  
  belongs_to :creator, :class_name => "User", :creator => true, :inverse_of => :my_hoshins

  belongs_to :company, :inverse_of => :hoshins, :counter_cache => true
  belongs_to :parent, :inverse_of => :children, :class_name => "Hoshin", :counter_cache => true
  has_many :children, :inverse_of => :parent, :class_name => "Hoshin", :foreign_key => "parent_id", :dependent => :destroy
  
  has_many :areas, -> { order :position }, :dependent => :destroy, :inverse_of => :hoshin
  has_many :objectives, :through => :areas, :inverse_of => :hoshin, :accessible => true
  has_many :indicators, :through => :objectives, :inverse_of => :hoshin, :accessible => true
  has_many :tasks, :through => :objectives, :accessible => true
  has_many :goals, -> { order :position }, :dependent => :destroy, :inverse_of => :hoshin
  
  children :areas
  
  validate :validate_company
  
  default_scope lambda {
    if Company.current_id
      where(:company_id => Company.current_id)
    else
      where(:company_id => UserCompany.select(:company_id).where(:user_id => User.current_id)) unless User.current_user == -1
    end
  }
  
  after_create do |obj|
    user = User.current_user
    user.tutorial_step << :hoshin
    user.save!
  end
  
  def taks_by_status(status)
    tasks.where(:status => status)
  end
  
  def backlog_tasks
    tasks.backlog.order(:lane_pos)
  end
  def active_tasks
    tasks.active.order(:lane_pos)
  end
  def completed_tasks
    tasks.completed.visible.order(:lane_pos)
  end
  def discarded_tasks
    tasks.discarded.visible.order(:lane_pos)
  end
  def outdated_tasks
    tasks.overdue
  end
  def outdated_indicators
    tasks.overdue
  end
  def blind_indicators
    tasks.overdue
  end
  def outdated_indicators
    tasks.overdue
  end
  
  def health_update!
    if self.readonly?
      logger.debug "====== Not updating read-only hoshin"
      return
    end
    neglected = Objective.unscoped.where(hoshin_id: id).neglected.count(:id)
    self.neglected_objectives_count = neglected
    
    outdated_ind = Indicator.unscoped.where(hoshin_id: id).overdue.count(:id)
    self.outdated_indicators_count = outdated_ind

    outdated_tsk = Task.unscoped.where(hoshin_id: id).overdue.count(:id)
    self.outdated_tasks_count = outdated_tsk
    
    blind = Objective.unscoped.where(hoshin_id: id).blind.count(:id)
    self.blind_objectives_count = blind  
  
    self.save!
  end
  
  after_update do |hoshin|
    if hoshin.company_id_changed?
      IndicatorHistory.joins(:indicator).where(:indicators => {:hoshin_id => id}).update_all(:company_id => company_id)
      hoshin.indicators.update_all(:company_id => company_id)
      hoshin.tasks.update_all(:company_id => company_id)
      hoshin.objectives.update_all(:company_id => company_id)
      hoshin.areas.update_all(:company_id => company_id)
      hoshin.goals.update_all(:company_id => company_id)
    end
  end
  
  
  def users_with_pending_actions
    users = {}
    (objectives.neglected | objectives.blind | indicators.overdue | tasks.overdue).each {|o|
      if (users.has_key? o.responsible)<commit_msg>Fixed: Error while moving hoshing amongst companies<commit_after>class Hoshin < ActiveRecord::Base
  
  include ModelBase

  hobo_model # Don't put anything above this

  fields do
    name :string, :default => 'plan', :null => false
    goals_count :integer, :default => 0, :null => false
    areas_count :integer, :default => 0, :null => false
    objectives_count :integer, :default => 0, :null => false
    indicators_count :integer, :default => 0, :null => false
    outdated_indicators_count :integer, :default => 0, :null => false
    outdated_tasks_count :integer, :default => 0, :null => false
    blind_objectives_count :integer, :default => 0, :null => false
    neglected_objectives_count :integer, :default => 0, :null => false
    tasks_count :integer, :default => 0, :null => false
    objectives_count :integer, :default => 0, :null => false
    hoshins_count :integer, :default => 0, :null => false
    header HoboFields::Types::TextileString
    timestamps
  end
  index [:company_id, :parent_id]
  
  attr_accessible :name, :id, :parent, :parent_id, :company, :company_id, :header
  attr_accessible :areas, :children, :children_ids, :creator_id, :creator
  
  belongs_to :creator, :class_name => "User", :creator => true, :inverse_of => :my_hoshins

  belongs_to :company, :inverse_of => :hoshins, :counter_cache => true
  belongs_to :parent, :inverse_of => :children, :class_name => "Hoshin", :counter_cache => true
  has_many :children, :inverse_of => :parent, :class_name => "Hoshin", :foreign_key => "parent_id", :dependent => :destroy
  
  has_many :areas, -> { order :position }, :dependent => :destroy, :inverse_of => :hoshin
  has_many :objectives, :through => :areas, :inverse_of => :hoshin, :accessible => true
  has_many :indicators, :through => :objectives, :inverse_of => :hoshin, :accessible => true
  has_many :tasks, :through => :objectives, :accessible => true
  has_many :goals, -> { order :position }, :dependent => :destroy, :inverse_of => :hoshin
  
  children :areas
  
  validate :validate_company
  
  default_scope lambda {
    if Company.current_id
      where(:company_id => Company.current_id)
    else
      where(:company_id => UserCompany.select(:company_id).where(:user_id => User.current_id)) unless User.current_user == -1
    end
  }
  
  after_create do |obj|
    user = User.current_user
    user.tutorial_step << :hoshin
    user.save!
  end
  
  def taks_by_status(status)
    tasks.where(:status => status)
  end
  
  def backlog_tasks
    tasks.backlog.order(:lane_pos)
  end
  def active_tasks
    tasks.active.order(:lane_pos)
  end
  def completed_tasks
    tasks.completed.visible.order(:lane_pos)
  end
  def discarded_tasks
    tasks.discarded.visible.order(:lane_pos)
  end
  def outdated_tasks
    tasks.overdue
  end
  def outdated_indicators
    tasks.overdue
  end
  def blind_indicators
    tasks.overdue
  end
  def outdated_indicators
    tasks.overdue
  end
  
  def health_update!
    if self.readonly?
      logger.debug "====== Not updating read-only hoshin"
      return
    end
    neglected = Objective.unscoped.where(hoshin_id: id).neglected.count(:id)
    self.neglected_objectives_count = neglected
    
    outdated_ind = Indicator.unscoped.where(hoshin_id: id).overdue.count(:id)
    self.outdated_indicators_count = outdated_ind

    outdated_tsk = Task.unscoped.where(hoshin_id: id).overdue.count(:id)
    self.outdated_tasks_count = outdated_tsk
    
    blind = Objective.unscoped.where(hoshin_id: id).blind.count(:id)
    self.blind_objectives_count = blind  
  
    self.save!
  end
  
  after_update do |hoshin|
    if hoshin.company_id_changed?
      IndicatorHistory.joins(:indicator).where(:indicators => {:hoshin_id => id}).update_all(:company_id => company_id)
      Indicator.update_all({:company_id => company_id},{:hoshin_id => id})
      hoshin.tasks.update_all({:company_id => company_id},{:hoshin_id => id})
      hoshin.objectives.update_all({:company_id => company_id},{:hoshin_id => id})
      hoshin.areas.update_all({:company_id => company_id},{:hoshin_id => id})
      hoshin.goals.update_all({:company_id => company_id},{:hoshin_id => id})
    end
  end
  
  
  def users_with_pending_actions
    users = {}
    (objectives.neglected | objectives.blind | indicators.overdue | tasks.overdue).each {|o|
      if (users.has_key? o.responsible)
{'fraction_non_alphanumeric': 0.12396404809151394, 'fraction_numerical': 0.0028014474144974905, 'mean_word_length': 3.9583333333333335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 132, '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': '4291733', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2897, 'n_words': 620}