Module: ActiveRecord::ConnectionAdapters::SchemaStatements

Defined in:
lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_content_column(table_name, column_name, type, options = {}) ⇒ Object

Adds a column to both the primary and versioned table. Save needing two calls. This is only needed if your content block is versioned, otherwise add_column will work just fine.



82
83
84
85
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 82

def add_content_column(table_name, column_name, type, options={})
  add_column table_name, column_name, type, options
  add_column "#{table_name.to_s.singularize}_versions".to_sym, column_name, type, options
end

#create_content_table(table_name, options = {}) {|t| ... } ⇒ Object Also known as: create_versioned_table

Pass in “:versioned => false” in the options hash to create a non-versioned table.

Yields:

  • (t)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 6

def create_content_table(table_name, options={}, &block)

  #Do the primary table
  t = TableDefinition.new(self)
  t.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false

  unless options[:versioned] == false
    t.integer :version
    t.integer :lock_version, :default => 0
  end

  yield t

  # Blocks currently must have a name column, otherwise the UI fails in several places.
  # Some migrations may have already specified a name attribute, so we don't want to overwrite it here.
  t.string :name unless t[:name]

  t.boolean :published, :default => false
  t.boolean :deleted, :default => false
  t.boolean :archived, :default => false
  t.integer :created_by_id
  t.integer :updated_by_id
  t.timestamps
  
  create_table_from_definition(table_name, options, t)

  unless options[:versioned] == false
    #Do the versions table
    vt = TableDefinition.new(self)
    vt.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false

    vt.integer "#{table_name.to_s.singularize}_id".to_sym
    vt.integer :version
    yield vt

    # Create implicit name column in version table as well.
    vt.string :name unless vt[:name]

    vt.boolean :published, :default => false
    vt.boolean :deleted, :default => false
    vt.boolean :archived, :default => false        
    vt.string :version_comment
    vt.integer :created_by_id
    vt.integer :updated_by_id
    vt.timestamps

    create_table_from_definition("#{table_name.to_s.singularize}_versions".to_sym, options, vt)
  end
  
end

#create_table_from_definition(table_name, options, table_definition) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 63

def create_table_from_definition(table_name, options, table_definition)
  if options[:force] && table_exists?(table_name)
   drop_table(table_name, options)
  end

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
  create_sql << "#{quote_table_name(table_name)} ("
  create_sql << table_definition.to_sql
  create_sql << ") #{options[:options]}"
  execute create_sql            
end

#drop_versioned_table(table_name) ⇒ Object



75
76
77
78
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 75

def drop_versioned_table(table_name)
  drop_table "#{table_name.singularize}_versions".to_sym
  drop_table table_name
end