Sha256: 5cbdc8fdaf459f3e827162ee15cff2d75d3d4586f79d4603d9e18820c372cc9e

Contents?: true

Size: 1.73 KB

Versions: 149

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/pd"

module Renalware
  module PD
    class RegimeBag < ApplicationRecord
      extend Enumerize

      enumerize :role,
                in: [:ordinary, :last_fill, :additional_manual_exchange],
                default: :ordinary,
                scope: :having_role,
                predicates: true

      before_save :assign_days_per_week

      belongs_to :bag_type
      belongs_to :regime, touch: true

      validates :bag_type, presence: true
      validates :volume, presence: true

      validates :volume, numericality: {
        allow_nil: true,
        greater_than_or_equal_to: 100,
        less_than_or_equal_to: 10000
      }

      validate :must_select_one_day

      def initialize(attributes = nil, _options = {})
        super() # !! Rails5 upgrade was super
        Date::DAYNAME_SYMBOLS.each do |day|
          public_send(:"#{day}=", true)
        end
        self.attributes = attributes unless attributes.nil?
      end

      class << self
        RegimeBag.role.values.each do |role|
          define_method role.to_sym do
            having_role(role.to_s)
          end
        end
      end

      def days
        Date::DAYNAME_SYMBOLS.map do |day|
          public_send(day)
        end
      end

      def weekly_total_glucose_ml_per_bag
        days_per_week * volume
      end

      def days_per_week
        days.count(true)
      end

      def has_volume?
        volume.to_i.nonzero?
      end

      private

      def assign_days_per_week
        self.per_week = days_per_week
      end

      def must_select_one_day
        return unless days_per_week == 0

        errors.add(:days, "must be assigned at least one day of the week")
      end
    end
  end
end

Version data entries

149 entries across 149 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/pd/regime_bag.rb
renalware-core-2.1.0 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.167 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.166 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.165 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.164 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.163 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.162 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.161 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.160 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.159 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.158 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.157 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.156 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.155 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.153 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.152 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.151 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.149 app/models/renalware/pd/regime_bag.rb
renalware-core-2.0.148 app/models/renalware/pd/regime_bag.rb