Sha256: e02b34925cd4fe9a6ed4722bdb2fa6ce2f9cc3ec00e9bdca900bcb2d49f58c3a

Contents?: true

Size: 1.31 KB

Versions: 28

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
        model_name.human
      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

28 entries across 28 versions & 1 rubygems

Version Path
effective_cpd-1.7.2 app/models/effective/cpd_special_rule.rb
effective_cpd-1.7.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.7.0 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.5 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.4 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.3 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.2 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.6.0 app/models/effective/cpd_special_rule.rb
effective_cpd-1.5.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.5.0 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.8 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.7 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.6 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.5 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.4 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.3 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.2 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.1 app/models/effective/cpd_special_rule.rb
effective_cpd-1.4.0 app/models/effective/cpd_special_rule.rb