Sha256: 5f2b7b26be783dc9ddeb2ebbb899476962d35455e301009e0c2b1bd893008d7d

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 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_chain :tables, :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)
        klass = DBViewCTI::Names.table_to_class_name(table).constantize
        base_classes << klass if klass.respond_to?('cti_base_class?') && klass.cti_base_class?
      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.0.3 lib/db_view_cti/schema_dumper.rb
dbview_cti-0.0.2 lib/db_view_cti/schema_dumper.rb