Module: PowerEnum::Schema::AbstractAdapter
- Defined in:
- lib/power_enum/schema/schema_statements.rb
Overview
Implementation of the PowerEnum extensions to the migrations DSL.
Instance Method Summary collapse
-
#create_enum(enum_name, options = {}, &block) ⇒ Object
Creates a new enum table.
-
#remove_enum(enum_name) ⇒ Object
Drops the enum table.
Instance Method Details
#create_enum(enum_name, options = {}, &block) ⇒ Object
Creates a new enum table. enum_name will be automatically pluralized.
Supported options
[:name_column] Specify the column name for name of the enum. By default it's :name. This can be a String or a Symbol [:description] Set this to true to have a 'description' column generated. [:name_limit] Set this define the limit of the name column. [:desc_limit] Set this to define the limit of the description column. [:active] Set this to true to have a boolean 'active' column generated. The 'active' column will have the options of NOT NULL and DEFAULT TRUE. [:timestamps] Set this to true to have timestamp columns (created_at and updated_at) generated. [:table_options] A hash of options to pass to the 'create_table' method.
You can also pass in a block that takes a table object as an argument, like create_table.
Examples
Basic Enum
create_enum :connector_type is the equivalent of create_table :connector_types do |t| t.string :name, :null => false end add_index :connector_types, [:name], :unique => true
Advanced Enum
create_enum :connector_type, :name_column => :connector, :name_limit => 50, :description => true, :desc_limit => 100, :active => true, :timestamps => true, :table_options => => :foo is the equivalent of create_table :connector_types, :primary_key => :foo do |t| t.string :connector, :limit => 50, :null => false t.string :description, :limit => 100 t.boolean :active, :null => false, :default => true t.timestamps end add_index :connector_types, [:connector], :unique => true
Customizing Enum with a block
create_enum :connector_type, :description => true do |t| t.boolean :has_sound end is the equivalent of create_table :connector_types do |t| t.string :name, :null => false t.string :description t.boolean :has_sound end add_index :connector_types, [:connector], :unique => true
Notice that a unique index is automatically created in each case on the proper name column.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/power_enum/schema/schema_statements.rb', line 90 def create_enum(enum_name, = {}, &block) enum_table_name = enum_name.pluralize # For compatibility with PgPower/PgSaurus schema_name = [:schema] enum_table_name = "#{schema_name}.#{enum_table_name}" if schema_name name_column = [:name_column] || :name generate_description = !![:description] generate_active = !![:active] = !![:timestamps] name_limit = [:name_limit] desc_limit = [:desc_limit] = [:table_options] || {} create_table enum_table_name, ** do |t| t.string name_column, :limit => name_limit, :null => false if generate_description t.string :description, :limit => desc_limit end if generate_active t.boolean :active, :null => false, :default => true end if t. end if block_given? yield t end end add_index enum_table_name, [name_column], :unique => true end |
#remove_enum(enum_name) ⇒ Object
Drops the enum table. enum_name will be automatically pluralized.
Example
remove_enum :connector_type is the equivalent of drop_table :connector_types
131 132 133 |
# File 'lib/power_enum/schema/schema_statements.rb', line 131 def remove_enum(enum_name) drop_table enum_name.pluralize end |