Sha256: 1f9308ccb370f54cd4365601138a3cb400dc53b3f4440ef82f6e03a741e3a9e9

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 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

1 entries across 1 versions & 1 rubygems

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