Module: DeletedAt::Views

Defined in:
lib/deleted_at/views.rb

Class Method Summary collapse

Class Method Details

.all_table(model) ⇒ Object



55
56
57
# File 'lib/deleted_at/views.rb', line 55

def self.all_table(model)
  "#{model.table_name}/all"
end

.all_table_exists?(model) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
# File 'lib/deleted_at/views.rb', line 25

def self.all_table_exists?(model)
  query = model.connection.execute <<-eos
    SELECT EXISTS (
      SELECT 1
      FROM   information_schema.tables
      WHERE    table_name = '#{all_table(model)}'
    );
  eos
  query.first['exists'] == 't'
end

.deleted_view(model) ⇒ Object



51
52
53
# File 'lib/deleted_at/views.rb', line 51

def self.deleted_view(model)
  "#{model.table_name}/deleted"
end

.deleted_view_exists?(model) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/deleted_at/views.rb', line 36

def self.deleted_view_exists?(model)
  query = model.connection.execute <<-eos
    SELECT EXISTS (
      SELECT 1
      FROM   information_schema.tables
      WHERE    table_name = '#{deleted_view(model)}'
    );
  eos
  query.first['exists'] == 't'
end

.install_deleted_view(model) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/deleted_at/views.rb', line 16

def self.install_deleted_view(model)
  return warn("You must install the all/present tables/views first!") unless all_table_exists?(model)
  table_name = deleted_view(model)
  model.connection.execute <<-eos
    CREATE OR REPLACE VIEW "#{table_name}"
    AS SELECT * FROM "#{all_table(model)}" WHERE #{model.deleted_at_column} IS NOT NULL;
  eos
end

.install_present_view(model) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/deleted_at/views.rb', line 4

def self.install_present_view(model)
  uninstall_present_view(model)
  all_table_name = all_table(model)
  present_table_name = present_view(model)

  model.connection.execute("ALTER TABLE \"#{present_table_name}\" RENAME TO \"#{all_table_name}\"")
  model.connection.execute <<-eos
    CREATE OR REPLACE VIEW "#{present_table_name}"
    AS SELECT * FROM "#{all_table_name}" WHERE #{model.deleted_at_column} IS NULL;
  eos
end

.present_view(model) ⇒ Object



47
48
49
# File 'lib/deleted_at/views.rb', line 47

def self.present_view(model)
  "#{model.table_name}"
end

.uninstall_deleted_view(model) ⇒ Object



68
69
70
# File 'lib/deleted_at/views.rb', line 68

def self.uninstall_deleted_view(model)
  model.connection.execute("DROP VIEW IF EXISTS \"#{deleted_view(model)}\"")
end

.uninstall_present_view(model) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/deleted_at/views.rb', line 59

def self.uninstall_present_view(model)
  # Legacy
  model.connection.execute("DROP VIEW IF EXISTS \"#{model.table_name}/present\"")
  # New
  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