Module: CompletenessFu::ActiveModelAdditions

Defined in:
lib/completeness-fu/active_model_additions.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/completeness-fu/active_model_additions.rb', line 8

def self.included(base)
  base.class_eval do
    def self.define_completeness_scoring(&checks_block)
      unless self.include?(ActiveModel::Naming)
        raise CompletenessFuError, 'please make sure ActiveModel::Naming is included so completeness_scoring can translate messages correctly'
      end

      class_inheritable_array :completeness_checks
      cattr_accessor :default_weighting
      cattr_accessor :model_weightings

      self.send :extend, ClassMethods
      self.send :include, InstanceMethods

      checks_results = CompletenessFu::ScoringBuilder.generate(self, &checks_block)

      self.default_weighting   = checks_results[:default_weighting]
      self.completeness_checks = checks_results[:completeness_checks]
      self.model_weightings    = checks_results[:model_weightings]

      if checks_results[:cache_score_details]
        unless self.include?(ActiveModel::Validations::Callbacks)
          raise CompletenessFuError, 'please make sure ActiveModel::Validations::Callbacks is included before define_completeness_scoring if you want to cache competeness scoring'
        end
        self.before_validation checks_results[:cache_score_details]
      end
    end
  end
end