Sha256: ae5cdccf936ec2275f4c69c02dd08e349d21d346f8f9e5ea53314056c0949796

Contents?: true

Size: 993 Bytes

Versions: 130

Compression:

Stored size: 993 Bytes

Contents

# Concern that can be used when dealing with Legacy code.
#
# It automatically aliases `canvas_id` -> `canvas_<model>_id` or vice-versa.
module CanvasSync::Concerns
  module LegacyColumns
    extend ActiveSupport::Concern

    class_methods do
      def inherited(subclass)
        super.tap do
          legacy_column_apply(subclass)
        end
      end

      private

      def legacy_column_apply(cls)
        return if cls.abstract_class
        cid_column = "canvas_#{cls.name.downcase}_id"
        column_names = cls.columns.map(&:name)
        return if column_names.include?('canvas_id') && column_names.include?(cid_column)
        if column_names.include?('canvas_id')
          cls.alias_attribute(cid_column.to_sym, :canvas_id)
        elsif column_names.include?(cid_column)
          cls.alias_attribute(:canvas_id, cid_column.to_sym)
        end
      rescue ActiveRecord::StatementInvalid
      end
    end

    included do
      legacy_column_apply(self)
    end
  end
end

Version data entries

130 entries across 130 versions & 1 rubygems

Version Path
canvas_sync-0.17.23.beta1 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.20 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.19 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.18 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.17.beta1 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.16 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.15 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.14 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.13 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.12 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.10.beta2 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.10.beta1 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.8 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.8.beta4 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.8.beta3 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.8.beta2 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.8.beta1 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.7 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.7.beta1 lib/canvas_sync/concerns/legacy_columns.rb
canvas_sync-0.17.6 lib/canvas_sync/concerns/legacy_columns.rb