<commit_before>module Submittable
  def self.included(klass)
    klass.belongs_to :assignment
    klass.belongs_to :old_assignment, class_name: 'Assignment'
    klass.has_many :assignment_student_visibilities, :through => :assignment

    klass.scope :visible_to_students_in_course_with_da, lambda { |user_ids, course_ids|
      klass.without_assignment_in_course(course_ids)
        .union(klass.joins_assignment_student_visibilities(user_ids, course_ids))
    }

    klass.scope :without_assignment_in_course, lambda { |course_ids|
      klass.where(context_id: course_ids, context_type: "Course").where(assignment_id: nil)
    }

    klass.scope :joins_assignment_student_visibilities, lambda { |user_ids, course_ids|
      klass.joins(:assignment_student_visibilities)
        .where(assignment_student_visibilities: { user_id: user_ids, course_id: course_ids })
    }
  end

  def sync_assignment
    if (a = self.assignment)
      a.title = self.title
      name = self.class.name.underscore
      a.submission_types = name
      a.saved_by = name.to_sym
      a.workflow_state = self.published? ? "published" : "unpublished"
    end
  end

  def for_assignment?
    name = self.class.name.underscore
    self.assignment && self.assignment.submission_types =~ /#{name}/
  end

  def restore(from=nil)
    self.workflow_state = 'unpublished'
    self.save

    if from != :assignment && self.for_assignment?
      name = self.class.name.underscore
      self.assignment.restore(name.to_sym)
    end
  end

  def unlink!(type)
    @saved_by = type
    self.assignment = nil
    self.destroy
  end

  def restore_old_assignment
    return nil unless self.old_assignment && self.old_assignment.deleted?
    self.old_assignment.workflow_state = 'published'
    name = self.class.name.underscore
    self.old_assignment.saved_by = name.to_sym
    self.old_assignment.save(:validate => false)
    self.old_assignment
  end

  def update_assignment
    if self.deleted?
      self.assignment.destroy if self.for_assignment? && !self.assignment.deleted?
    else
      if !self.assignment_id && @old_assignment_id
        self.context_module_tags.each(&:confirm_valid_module_requirements)
      end
      if @old_assignment_id
        Assignment.where(
          id: @old_assignment_id,
          context: self.context,
          submission_types: 'wiki_page'
        ).update_all(workflow_state: 'deleted', updated_at: Time.now.utc)
      elsif self.assignment && @saved_by != :assignment
        self.sync_assignment
        self.assignment.save<commit_msg>rails 4.2: fix wiki_page_spec<commit_after>module Submittable
  def self.included(klass)
    klass.belongs_to :assignment
    klass.belongs_to :old_assignment, class_name: 'Assignment'
    klass.has_many :assignment_student_visibilities, :through => :assignment

    klass.scope :visible_to_students_in_course_with_da, lambda { |user_ids, course_ids|
      klass.without_assignment_in_course(course_ids)
        .union(klass.joins_assignment_student_visibilities(user_ids, course_ids))
    }

    klass.scope :without_assignment_in_course, lambda { |course_ids|
      klass.where(context_id: course_ids, context_type: "Course").where(assignment_id: nil)
    }

    klass.scope :joins_assignment_student_visibilities, lambda { |user_ids, course_ids|
      klass.joins(:assignment_student_visibilities)
        .where(assignment_student_visibilities: { user_id: user_ids, course_id: course_ids })
    }
  end

  def sync_assignment
    if (a = self.assignment)
      a.title = self.title
      name = self.class.name.underscore
      a.submission_types = name
      a.saved_by = name.to_sym
      a.workflow_state = self.published? ? "published" : "unpublished"
    end
  end

  def for_assignment?
    name = self.class.name.underscore
    self.assignment && self.assignment.submission_types =~ /#{name}/
  end

  def restore(from=nil)
    self.workflow_state = 'unpublished'
    self.save

    if from != :assignment && self.for_assignment? && self.assignment.deleted?
      name = self.class.name.underscore
      self.assignment.restore(name.to_sym)
    end
  end

  def unlink!(type)
    @saved_by = type
    self.assignment = nil
    self.destroy
  end

  def restore_old_assignment
    return nil unless self.old_assignment && self.old_assignment.deleted?
    self.old_assignment.workflow_state = 'published'
    name = self.class.name.underscore
    self.old_assignment.saved_by = name.to_sym
    self.old_assignment.save(:validate => false)
    self.old_assignment
  end

  def update_assignment
    if self.deleted?
      self.assignment.destroy if self.for_assignment? && !self.assignment.deleted?
    else
      if !self.assignment_id && @old_assignment_id
        self.context_module_tags.each(&:confirm_valid_module_requirements)
      end
      if @old_assignment_id
        Assignment.where(
          id: @old_assignment_id,
          context: self.context,
          submission_types: 'wiki_page'
        ).update_all(workflow_state: 'deleted', updated_at: Time.now.utc)
      elsif self.assignment && @saved_by != :assignment
        self.clear_changes_information unless CANVAS_RAILS4_0 # needed to prevent an infinite loop in rails 4.2
        self.sync_assignment
        self.assignment.save
{'fraction_non_alphanumeric': 0.09187547456340167, 'fraction_numerical': 0.0011389521640091116, 'mean_word_length': 3.450168918918919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5066933', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1737, 'n_words': 299}