Sha256: c812efe981bca28d9954d46615c5a8ee127f02c314c0a88203f0b79c181b58ef

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 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, 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

10 entries across 10 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/models/ecom/core/task_template.rb
ecom_core-1.3.13 app/models/ecom/core/task_template.rb
ecom_core-1.3.12 app/models/ecom/core/task_template.rb
ecom_core-1.3.11 app/models/ecom/core/task_template.rb
ecom_core-1.3.10 app/models/ecom/core/task_template.rb
ecom_core-1.3.9 app/models/ecom/core/task_template.rb
ecom_core-1.3.8 app/models/ecom/core/task_template.rb
ecom_core-1.3.7 app/models/ecom/core/task_template.rb
ecom_core-1.3.6 app/models/ecom/core/task_template.rb
ecom_core-1.3.5 app/models/ecom/core/task_template.rb