Sha256: 43708c078ffa48285a8a47d4bdb45b7a804daaf1a9662b1212c7627e3c125fc7

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literals: true

module Jobshop
  class Inspection::BooleanCriterion < ApplicationRecord
    self.primary_keys = %i[ organization_id report_id criterion_id criterion_type ]

    after_initialize { self.criterion_id ||= SecureRandom.uuid if new_record? }
    before_save(on: :create) { criterion.save unless criterion.persisted? }

    belongs_to :organization

    has_one :criterion, as: :criterion, autosave: true, dependent: :destroy,
      foreign_key: %i[ organization_id report_id criterion_id criterion_type ],
      inverse_of: :criterion

    belongs_to :report, class_name: "Jobshop::Inspection::Report",
      foreign_key: %i[ organization_id report_id ]

    def criterion
      super || build_criterion
    end

    delegate :name, :name=, :position, :position=, to: :criterion

    def specification
      @specification ||= "#{condition}\nPASS/FAIL"
    end

    def tolerance
      @tolerance ||= condition
    end

    def unit
      "boolean".freeze
    end

    def pass?(value)
      ![ false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF" ].include?(value)
    end

    # Generate a random value that has a 90% chance of being in spec.
    # TODO: This needs to go in the tests somewhere, not really in the model.
    def random
      rand(100) < 10
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.163 app/models/jobshop/inspection/boolean_criterion.rb