Sha256: 196a00ab303a9f5f39f3d3c806827bb710e1c9f3f9e53e9c970d982344b47acf

Contents?: true

Size: 1.85 KB

Versions: 22

Compression:

Stored size: 1.85 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") }

      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

22 entries across 22 versions & 1 rubygems

Version Path
canvas_sync-0.19.0.beta10 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta9 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta8 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta7 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta6 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta5 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta4 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta3 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta2 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.19.0.beta1 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.12 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.10 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.9 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.8 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.7 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.6 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.5 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.3 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.2 lib/canvas_sync/concerns/account/ancestry.rb
canvas_sync-0.18.1 lib/canvas_sync/concerns/account/ancestry.rb