Sha256: efc7834a9384d90c22d18f524765c469446364a87d96326685dd49b687c6ad15

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module Ecom
  module Core
    class TaskTemplate < ApplicationRecord
      has_ancestry

      belongs_to :task_template_type
      belongs_to :unit_of_measure

      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

4 entries across 4 versions & 1 rubygems

Version Path
ecom_core-1.3.2 app/models/ecom/core/task_template.rb
ecom_core-1.3.1 app/models/ecom/core/task_template.rb
ecom_core-1.3.0 app/models/ecom/core/task_template.rb
ecom_core-1.2.36 app/models/ecom/core/task_template.rb