Sha256: 65fb198b4691e6a496ee723c8cb1d24dfdfc002883a96fac2caf1ce25e4ceea4

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 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
    self.once_completed ||= all_children_passed?
  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!
    all_children_passed?
  end

  def once_completed?
    self.once_completed || completed?
  end

  def self.delete_all_for!(content, organization)
    where(content: content, organization: organization).delete_all
  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

  def all_children_passed?
    children_passed_count == children_count
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
mumuki-domain-8.1.3 app/models/indicator.rb
mumuki-domain-8.1.2 app/models/indicator.rb
mumuki-domain-8.1.1 app/models/indicator.rb
mumuki-domain-8.1.0 app/models/indicator.rb
mumuki-domain-8.0.0 app/models/indicator.rb
mumuki-domain-7.12.2 app/models/indicator.rb
mumuki-domain-7.12.1 app/models/indicator.rb
mumuki-domain-7.12.0 app/models/indicator.rb
mumuki-domain-7.11.1 app/models/indicator.rb
mumuki-laboratory-7.11.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-c92f2a7212e2/app/models/indicator.rb
mumuki-laboratory-7.11.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-2e15c3330133/app/models/indicator.rb
mumuki-domain-7.11.0 app/models/indicator.rb