Sha256: 749bf6b0b2d389d965574bc621de3720ce91a6eb3491d0e99e67dfe6f6fe67ef

Contents?: true

Size: 1.31 KB

Versions: 25

Compression:

Stored size: 1.31 KB

Contents

module Effective
  class CpdSpecialRule < ActiveRecord::Base
    belongs_to :cpd_cycle

    has_many :cpd_special_rule_mates, dependent: :destroy, inverse_of: :cpd_special_rule
    has_many :cpd_rules, -> { CpdRule.sorted }, through: :cpd_special_rule_mates

    if respond_to?(:log_changes)
      log_changes
    end

    CATEGORIES = ['cumulative max credits']

    effective_resource do
      category  :string # Special rule tyoes

      # For cumulative max credits
      max_credits_per_cycle :decimal

      timestamps
    end

    scope :deep, -> { includes(:cpd_special_rule_mates, cpd_rules: [:ruleable]) }
    scope :sorted, -> { order(:id) }

    before_validation do
      self.category ||= CATEGORIES.first
    end

    validates :category, presence: true, inclusion: { in: CATEGORIES }

    with_options(if: -> { cumulative_max_credits? }) do
      validates :max_credits_per_cycle, presence: true, numericality: { greater_than: 0.0 }
    end

    def to_s
      if cumulative_max_credits?
        "Cumulative max #{max_credits_per_cycle} credits"
      else
        'cpd special rule'
      end
    end

    def cumulative_max_credits?
      category == 'cumulative max credits'
    end

    # Right now this is going to be just Effective::CpdCategory objects
    def ruleables
      cpd_rules.map(&:ruleable)
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
effective_cpd-0.6.9 app/models/effective/cpd_special_rule.rb
effective_cpd-1.2.4 app/models/effective/cpd_special_rule.rb
effective_cpd-1.2.3 app/models/effective/cpd_special_rule.rb
effective_cpd-1.2.2 app/models/effective/cpd_special_rule.rb
effective_cpd-1.2.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.2.0 app/models/effective/cpd_special_rule.rb
effective_cpd-1.1.3 app/models/effective/cpd_special_rule.rb
effective_cpd-1.1.2 app/models/effective/cpd_special_rule.rb
effective_cpd-1.1.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.1.0 app/models/effective/cpd_special_rule.rb
effective_cpd-1.0.0 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.8 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.7 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.6 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.5 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.4 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.3 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.2 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.1 app/models/effective/cpd_special_rule.rb
effective_cpd-0.6.0 app/models/effective/cpd_special_rule.rb