Sha256: 63f9768d9c5cd56a5ac878a87953e475250a44a5898da79ad813ffe08c5fc54e

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

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

    after_initialize do
      self[:criterion_id] ||= SecureRandom.uuid if new_record?
    end

    belongs_to :organization
    has_one :criterion, as: :criterion, autosave: true, dependent: :destroy,
      foreign_key: [ :organization_id, :report_id, :criterion_id, :criterion_type ]
    belongs_to :report, class_name: "Jobshop::Inspection::Report",
      foreign_key: [ :organization_id, :report_id ], inverse_of: :criteria

    alias_method :criterion_without_build, :criterion
    def criterion_with_build
      criterion_without_build || build_criterion(criterion_id: criterion_id, report: report)
    end; alias_method :criterion, :criterion_with_build

    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.157 app/models/jobshop/inspection/boolean_criterion.rb