module DeletedAt::Legacy

Public Class Methods

install(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 10
def self.install(model)
  return false unless Core.has_deleted_at_column?(model)

  install_present_view(model)
  install_deleted_view(model)
end
uninstall(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 3
def self.uninstall(model)
  return false unless Core.has_deleted_at_column?(model)

  uninstall_deleted_view(model)
  uninstall_present_view(model)
end

Private Class Methods

all_table(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 74
def self.all_table(model)
  "#{model.table_name}/all"
end
all_table_exists?(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 44
    def self.all_table_exists?(model)
      query = model.connection.execute <<-SQL
        SELECT EXISTS (
          SELECT true
          FROM   information_schema.tables
          WHERE  table_name = '#{all_table(model)}'
        ) AS exists;
      SQL
      query.first['exists']
    end
deleted_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 70
def self.deleted_view(model)
  "#{model.table_name}/deleted"
end
deleted_view_exists?(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 55
    def self.deleted_view_exists?(model)
      query = model.connection.execute <<-SQL
        SELECT EXISTS (
          SELECT true
          FROM   information_schema.tables
          WHERE  table_name = '#{deleted_view(model)}'
        ) AS exists;
      SQL
      query.first['exists']
    end
install_deleted_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 32
    def self.install_deleted_view(model)
      return DeletedAt.logger.warn("You must install the all/present tables/views first!") unless all_table_exists?(model)
      table_name = deleted_view(model)

      while_spoofing_table_name(model, all_table(model)) do
        model.connection.execute <<-SQL
          CREATE OR REPLACE VIEW "#{table_name}"
          AS #{ model.select('*').where.not(model.deleted_at_column => nil).to_sql }
        SQL
      end
    end
install_present_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 19
    def self.install_present_view(model)
      # uninstall_present_view(model)
      present_table_name = present_view(model)

      while_spoofing_table_name(model, all_table(model)) do
        model.connection.execute("ALTER TABLE \"#{present_table_name}\" RENAME TO \"#{model.table_name}\"")
        model.connection.execute <<-SQL
          CREATE OR REPLACE VIEW "#{present_table_name}"
          AS #{ model.select('*').where(model.deleted_at_column => nil).to_sql }
        SQL
      end
    end
present_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 66
def self.present_view(model)
  "#{model.table_name}"
end
uninstall_deleted_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 84
def self.uninstall_deleted_view(model)
  model.connection.execute("DROP VIEW IF EXISTS \"#{deleted_view(model)}\"")
end
uninstall_present_view(model) click to toggle source
# File lib/deleted_at/legacy.rb, line 78
def self.uninstall_present_view(model)
  return unless all_table_exists?(model)
  model.connection.execute("DROP VIEW IF EXISTS \"#{present_view(model)}\"")
  model.connection.execute("ALTER TABLE \"#{all_table(model)}\" RENAME TO \"#{present_view(model)}\"")
end
while_spoofing_table_name(model, new_name) { || ... } click to toggle source
# File lib/deleted_at/legacy.rb, line 88
def self.while_spoofing_table_name(model, new_name, &block)
  old_name = model.table_name
  model.table_name = new_name
  yield
  model.table_name = old_name
end