Sha256: 5fe903a2434470df3b27c08ba275a1fd2bccfb72f9bb73c79f8c41e0306bfceb

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# this file is inspired by schema_dumper.rb in the foreigner gem 
# ( https://github.com/matthuhiggins/foreigner )
module DBViewCTI
  module SchemaDumper
    extend ActiveSupport::Concern

    included do
      alias_method :tables_without_cti_views, :tables
      alias_method :tables, :tables_with_cti_views
    end

    def tables_with_cti_views(stream)
      tables_without_cti_views(stream)
      base_classes = []
      @connection.tables.sort.each do |table|
        next if ignore_table?(table)
        begin
          klass = DBViewCTI::Names.table_to_class_name(table).constantize
          base_classes << klass if klass.respond_to?('cti_base_class?') && klass.cti_base_class?
        rescue NameError
          # do nothing
        end
      end
      base_classes.each do |klass|
        dump_cti_hierarchy(klass, stream)
      end
    end

    private
    
      def ignore_table?(table)
        ['schema_migrations', ignore_tables].flatten.any? do |ignored|
          case ignored
          when String; table == ignored
          when Regexp; table =~ ignored
          else
            raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
          end
        end
      end
      
      def dump_cti_hierarchy(base_class, stream)
        base_class.cti_all_descendants.each do |class_name|
          stream.puts("  cti_create_view('#{class_name}')")
        end
        stream.puts('')
      end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dbview_cti-0.2.3 lib/db_view_cti/schema_dumper.rb
dbview_cti-0.2.2 lib/db_view_cti/schema_dumper.rb