Sha256: 50cbce6a6aebe95241209f64e6ad5bd6fa70e4d57591d7033288d3c4937e99a7

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

module Ecom
  module Core
    class TaskTemplate < ApplicationRecord
      has_ancestry

      belongs_to :task_template_type

      has_many :task_template_inspection_checklists
      has_many :inspection_checklists, through: :task_template_inspection_checklists
      has_many :project_work_item_templates
      has_many :work_product_templates, through: :project_work_item_templates
      has_and_belongs_to_many :resource_types, join_table: 'ecom_core_task_templates_resource_types'

      validates :name, :code, :takeoff_fields, :task_completion_detail, presence: true
      validates :code, uniqueness: true
      validates :percentage_contribution,
                numericality: {
                  greater_than_or_equal_to: 0,
                  less_than_or_equal_to: 100,
                  only_integer: true
                },
                allow_nil: true

      delegate(:name, to: :task_template_type, prefix: true)

      validate :percentage_contribution_sum_validator

      def full_name
        parent_name = parent&.name
        return name unless parent_name

        "#{name} - #{parent_name}"
      end

      def crew_types
        resource_types.where(type: 'Ecom::Core::CrewType')
      end

      def material_types
        resource_types.where(type: 'Ecom::Core::MaterialType')
      end

      def equipment_types
        resource_types.where(type: 'Ecom::Core::EquipmentType')
      end

      def percentage_contribution_sum_validator
        return if percentage_contribution.nil?

        sum = siblings.sum(:percentage_contribution)
        errors.add(:task, 'Sum of percentage contributions has to be 100') if sum > 100
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecom_core-1.3.4 app/models/ecom/core/task_template.rb
ecom_core-1.3.3 app/models/ecom/core/task_template.rb