Module: ActiveRecord::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb
Instance Method Summary collapse
-
#add_content_column(table_name, column_name, type, options = {}) ⇒ Object
Adds a column to both the primary and versioned table.
-
#content_table_exists?(table_name) ⇒ Boolean
Will namespace the content table.
-
#create_content_table(table_name, options = {}) {|t| ... } ⇒ Object
(also: #create_versioned_table)
Pass in “:versioned => false” in the options hash to create a non-versioned table.
- #create_table_from_definition(table_name, options, table_definition) ⇒ Object
- #drop_versioned_table(table_name) ⇒ Object (also: #drop_content_table)
-
#prefix(table_name) ⇒ Object
Applies the current CMS prefix to the given table name.
- #remove_content_column(table_name, column_name) ⇒ Object
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.
105 106 107 108 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 105 def add_content_column(table_name, column_name, type, ={}) add_column table_name, column_name, type, add_column "#{table_name.to_s.singularize}_versions".to_sym, column_name, type, end |
#content_table_exists?(table_name) ⇒ Boolean
Will namespace the content table
116 117 118 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 116 def content_table_exists?(table_name) table_exists?(Cms::Namespacing.prefixed_table_name(table_name)) 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.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 17 def create_content_table(table_name, ={}, &block) defaults = { versioned: true, prefix: true } = defaults.merge() if [:prefix] table_name = Cms::Namespacing.prefixed_table_name(table_name) end #Create the primary table t = TableDefinition.new(self) t.primary_key([:primary_key] || Base.get_primary_key(table_name)) unless [:id] == false unless [: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. create_table_from_definition(table_name, , t) unless [:versioned] == false #Do the versions table vt = TableDefinition.new(self) vt.primary_key([:primary_key] || Base.get_primary_key(table_name)) unless [:id] == false vt.integer :original_record_id 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. create_table_from_definition("#{table_name.to_s.singularize}_versions".to_sym, , vt) end end |
#create_table_from_definition(table_name, options, table_definition) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 83 def create_table_from_definition(table_name, , table_definition) if [:force] && table_exists?(table_name) drop_table(table_name, ) end create_sql = "CREATE#{' TEMPORARY' if [:temporary]} TABLE " create_sql << "#{quote_table_name(table_name)} (" create_sql << table_definition.to_sql create_sql << ") #{[:options]}" execute create_sql end |
#drop_versioned_table(table_name) ⇒ Object Also known as: drop_content_table
95 96 97 98 99 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 95 def drop_versioned_table(table_name) table_name = Cms::Namespacing.prefixed_table_name(table_name) drop_table "#{table_name.singularize}_versions".to_sym drop_table table_name end |
#prefix(table_name) ⇒ Object
Applies the current CMS prefix to the given table name. Useful for where you need CMS related migrations using stock rails migrations (like add_index or create_table) but still want to dynamically apply the CMS prefixes.
8 9 10 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 8 def prefix(table_name) Cms::Namespacing.prefixed_table_name(table_name) end |
#remove_content_column(table_name, column_name) ⇒ Object
110 111 112 113 |
# File 'lib/cms/extensions/active_record/connection_adapters/abstract/schema_statements.rb', line 110 def remove_content_column(table_name, column_name) remove_column table_name, column_name remove_column "#{table_name.to_s.singularize}_versions".to_sym, column_name end |