Sha256: 1cdccaa1cb03725b1389bb740fbe2181e85d2a94bab44808f991724171c8ba8e

Contents?: true

Size: 1.64 KB

Versions: 32

Compression:

Stored size: 1.64 KB

Contents

class Indicator < Progress
  belongs_to :user
  belongs_to :content, polymorphic: true
  belongs_to :organization

  has_many :indicators, foreign_key: :parent_id, class_name: 'Indicator'
  has_many :assignments, foreign_key: :parent_id

  def propagate_up!(&block)
    instance_eval &block
    parent&.instance_eval { propagate_up! &block }
  end

  def self.dirty_by_content_change!(content)
    where(content: content).update_all dirty_by_content_change: true
  end

  def dirty_by_submission!
    propagate_up! { update! dirty_by_submission: true }
  end

  def rebuild!
    if dirty_by_content_change?
      propagate_up! do
        refresh_children_count!
        refresh_children_passed_count!
        clean!
        save!
      end
    elsif dirty_by_submission?
      refresh_children_passed_count!
      clean!
      save!
    end
  end

  def clean!
    self.dirty_by_submission = false
    self.dirty_by_content_change = false
  end

  def refresh_children_count!
    self.children_count = content.structural_children.count
  end

  def refresh_children_passed_count!
    self.children_passed_count = children.count(&:completed?)
  end

  def completion_percentage
    rebuild!
    children_passed_count.fdiv children_count
  end

  def completed?
    rebuild!
    children_passed_count == children_count
  end

  private

  def children
    indicators.presence || assignments
  end

  %i(children_count children_passed_count).each do |selector|
    define_method selector do
      send "refresh_#{selector}!" unless self[selector]

      self[selector]
    end
  end

  def parent_content
    content.usage_in_organization(organization).structural_parent
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
mumuki-domain-7.10.1 app/models/indicator.rb
mumuki-domain-7.10.0 app/models/indicator.rb
mumuki-laboratory-7.9.2 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-7082a83493eb/app/models/indicator.rb
mumuki-domain-7.9.2 app/models/indicator.rb
mumuki-laboratory-7.9.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-b162dc91aa77/app/models/indicator.rb
mumuki-domain-7.9.1 app/models/indicator.rb
mumuki-laboratory-7.9.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-520f859ca8af/app/models/indicator.rb
mumuki-domain-7.9.0 app/models/indicator.rb
mumuki-domain-7.7.3 app/models/indicator.rb
mumuki-domain-7.8.1 app/models/indicator.rb
mumuki-domain-7.8.0 app/models/indicator.rb
mumuki-domain-7.7.2 app/models/indicator.rb
mumuki-domain-7.7.1 app/models/indicator.rb
mumuki-laboratory-7.7.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-cb7a9018bb94/app/models/indicator.rb
mumuki-laboratory-7.7.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-cb7a9018bb94/app/models/indicator.rb
mumuki-domain-7.7.0 app/models/indicator.rb
mumuki-domain-7.6.1 app/models/indicator.rb
mumuki-domain-7.6.0 app/models/indicator.rb
mumuki-laboratory-7.5.2 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-ad16a5930cec/app/models/indicator.rb
mumuki-laboratory-7.5.1 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-a059418e5fd8/app/models/indicator.rb