Sha256: a0cd2de00143f23949452cea521051d4fdeb58d466dc8e5bcf86faf9c06ade3a
Contents?: true
Size: 1.86 KB
Versions: 31
Compression:
Stored size: 1.86 KB
Contents
module CanvasSync::Concerns module Account # Add support for the ancestry Gem to Accounts # # Requires `ancestry` to be added to the Gemfile and a migration to execute these steps: # add_column :accounts, :ancestry, :string # add_index :accounts, :ancestry # # Handles syncing any Ancestry changes after CanvasSync syncs Accounts. module Ancestry extend ActiveSupport::Concern include CanvasSync::Record CanvasSync::Record.define_feature self, default: ->{ column_names.include?("ancestry") rescue nil } included do has_ancestry before_save :relink_ancestry, if: :canvas_parent_account_id_changed? after_sync_import :ancestry_after_sync end class_methods do def ancestry_after_sync trails = {} includes(:canvas_parent).find_each do |account| parent = account.canvas_parent trail = trails[parent.canvas_id] if parent.present? if trail.present? account.ancestry = trail new_trail = "#{trail}/#{account.id.to_s}" elsif parent.present? account.parent = parent new_trail = "#{account.ancestry}/#{account.id.to_s}" else account.parent = parent new_trail = account.id.to_s end trails[account.canvas_id] = new_trail account.save! if account.changed? end end end def relink_ancestry self.parent = canvas_parent end def ensure_ancestry return unless canvas_parent_account_id.present? return if canvas_parent.present? self.canvas_parent = Account.find_or_fetch(canvas_parent_account_id) canvas_parent.save! canvas_parent.ensure_ancestry relink_ancestry save! if changed? end end end end
Version data entries
31 entries across 31 versions & 1 rubygems