Module: MigrationComments

Defined in:
lib/migration_comments.rb,
lib/migration_comments/version.rb,
lib/migration_comments/annotate_models.rb,
lib/migration_comments/schema_formatter.rb

Defined Under Namespace

Modules: ActiveRecord, AnnotateModels, SchemaFormatter

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Class Method Details

.setupObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/migration_comments.rb', line 23

def self.setup
  if ::ActiveRecord::VERSION::MAJOR == 2
    warn "[DEPRECATION] MigrationComments support for ActiveRecord v2.3 is deprecated and will be removed in a future version."
  end

  base_names = %w(SchemaDumper) +
    %w(ColumnDefinition Column Table TableDefinition AbstractAdapter).map{|name| "ConnectionAdapters::#{name}"}

  base_names.each do |base_name|
    ar_class = "ActiveRecord::#{base_name}".constantize
    mc_class = "MigrationComments::ActiveRecord::#{base_name}".constantize
    unless ar_class.ancestors.include?(mc_class)
      ar_class.__send__(:include, mc_class)
    end
  end

  # Rails 4 introduces some new models that were not previously present
  optimistic_models = []
  optimistic_models << "ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation"
  optimistic_models << "ActiveRecord::ConnectionAdapters::AlterTable"
  optimistic_models.each do |model_name|
    begin
      model = model_name.constantize
      mc_class = "MigrationComments::#{model_name}".constantize
      model.module_eval do
        model.send(:include, mc_class)
      end
    rescue Exception => ex
    end
  end

  adapters = %w(PostgreSQL Mysql Mysql2)
  adapters << (::ActiveRecord::VERSION::MAJOR <= 3 ? "SQLite" : "SQLite3")
  adapters.each do |adapter|
    begin
      require("active_record/connection_adapters/#{adapter.downcase}_adapter")
      adapter_class = ('::ActiveRecord::ConnectionAdapters::' << "#{adapter}Adapter").constantize
      mc_class = ('MigrationComments::ActiveRecord::ConnectionAdapters::' << "#{adapter}Adapter").constantize
      adapter_class.module_eval do
        adapter_class.__send__(:include, mc_class)
      end
    rescue Exception => ex
    end
  end

  # annotations are not required for this gem, but if they exist they should be updated
  begin
    begin # first try to load from the 'annotate' gem
      require 'annotate/annotate_models'
    rescue Exception => ex
      # continue as it may be already accessible through a plugin
    end
    gem_class = AnnotateModels
    # don't require this until after the original AnnotateModels loads to avoid namespace confusion
    require 'migration_comments/annotate_models'
    mc_class = MigrationComments::AnnotateModels
    unless gem_class.ancestors.include?(mc_class)
      gem_class.__send__(:include, mc_class)
    end
  rescue Exception => ex
    # if we got here, don't bother installing comments into annotations
  end
end