Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Enumerations for Rails Done Right.

## Versions

* PowerEnum 4.0.X (this version) supports Rails 6.X, and Rails 7.0 (Experimental)
* PowerEnum 5.0.X (this version) supports Rails 7.X
* PowerEnum 4.0.X supports Rails 6.X, and Rails 7.0 (Experimental)
* PowerEnum 3.X supports Rails 4.2, Rails 5.X and Rails 6.0
* PowerEnum 2.X supports Rails 4.X and Rails 5.0
* PowerEnum 1.X supports Rails 3.1/3.2, available here: https://github.com/albertosaurus/power_enum
Expand Down Expand Up @@ -121,7 +122,7 @@ rails generate enum booking_status
You should see output similar to this:

create app/models/booking_status.rb
create db/migrate/20110926012928_create_enum_booking_status.rb
create db/migrate/20110926012928_create_power_enum_booking_status.rb
invoke test_unit
create test/unit/booking_status_test.rb

Expand All @@ -138,7 +139,7 @@ When you open your migration file, it will look something like this:
class CreateEnumBookingStatus < ActiveRecord::Migration

def change
create_enum :booking_status
create_power_enum :booking_status
end

end
Expand All @@ -147,15 +148,13 @@ end
You can now customize it.

```ruby
create_enum :booking_status, :name_limit => 50
create_power_enum :booking_status, :name_limit => 50
# The above is equivalent to saying
# create_table :booking_statuses do |t|
# t.string :name, :limit => 50, :null => false
# end
```

**WARNING - This conflicts with PostgreSQL enum support in Rails 7+ and will be renamed in future versions.**

Now, when you create your Booking model, your migration should create a reference column for status id's and a foreign
key relationship to the booking\_statuses table.

Expand Down Expand Up @@ -187,9 +186,7 @@ end

There are two methods added to Rails migrations:

##### create\_enum(enum\_name, options = {}, &block)

**WARNING - This conflicts with PostgreSQL enum support in Rails 7+ and will be renamed in future versions.**
##### create\_power\_enum(enum\_name, options = {}, &block)

Creates a new enum table. `enum_name` will be automatically pluralized. The following options are supported:

Expand All @@ -207,7 +204,7 @@ You can also pass in a block that takes a table object as an argument, like `cre
Example:

```ruby
create_enum :booking_status
create_power_enum :booking_status
```

is the equivalent of
Expand All @@ -222,7 +219,7 @@ add_index :booking_statuses, [:name], :unique => true
In a more complex case:

```ruby
create_enum :booking_status,
create_power_enum :booking_status,
:name_column => :booking_name,
:name_limit => 50,
:description => true,
Expand All @@ -247,7 +244,7 @@ add_index :booking_statuses, [:booking_name], :unique => true
You can also customize the creation process by using a block:

```ruby
create_enum :booking_status do |t|
create_power_enum :booking_status do |t|
t.boolean :first_booking, :null => false
end
```
Expand All @@ -264,14 +261,14 @@ add_index :booking_statuses, [:name], :unique => true

Notice that a unique index is automatically created on the specified name column.

##### remove\_enum(enum\_name)
##### remove\_power\_enum(enum\_name)

Drops the enum table. `enum_name` will be automatically pluralized.

Example:

```ruby
remove_enum :booking_status
remove_power_enum :booking_status
```

is the equivalent of
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.1
5.0.0
2 changes: 1 addition & 1 deletion lib/generators/enum/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Example:

This will create:
BookingStatus model: app/models/booking_status.rb
Migration to create tables: db/migrate/20110925195833_create_enum_booking_status.rb
Migration to create tables: db/migrate/20110925195833_create_power_enum_booking_status.rb
Unit test for model: test/unit/booking_status_test.rb
2 changes: 1 addition & 1 deletion lib/generators/enum/enum_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def generate_model
# Generates the migration to create the enum table.
def generate_migration
@description = options.description?
migration_template 'rails31_migration.rb.erb', "db/migrate/create_enum_#{table_name}.rb" if options.migration?
migration_template 'rails31_migration.rb.erb', "db/migrate/create_power_enum_#{table_name}.rb" if options.migration?
end

# Do not pluralize enumeration names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def next_migration_number(dirname)
# Lifted directly from ActiveRecord::Generators::Migration
# Unfortunately, no API is provided by Rails at this time.
next_migration_number = current_migration_number(dirname) + 1
if ActiveRecord::Base.timestamped_migrations
if ActiveRecord.timestamped_migrations
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
else
"%.3d" % next_migration_number
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/enum/templates/rails31_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class <%= migration_class_name %> < ActiveRecord::Migration<%= Rails.version =~ /^4\.2\.*/ ? "" : "[5.0]" %>

def change
create_enum :<%=file_name%><%= ", description: true" if @description %>
create_power_enum :<%=file_name%><%= ", description: true" if @description %>
end

end
22 changes: 11 additions & 11 deletions lib/power_enum/migration/command_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ module PowerEnum::Migration # :nodoc:

# Extensions for CommandRecorder
module CommandRecorder
# Records create_enum
def create_enum(*args)
record(:create_enum, args)
# Records create_power_enum
def create_power_enum(*args)
record(:create_power_enum, args)
end

# Records remove_enum
def remove_enum(*args)
record(:remove_enum, args)
# Records remove_power_enum
def remove_power_enum(*args)
record(:remove_power_enum, args)
end

# The inversion of create_enum is remove_enum
# @param [Array] args Arguments to create_enum
# @return [Array] [:remove_enum, [enum_name]]
def invert_create_enum(args)
# The inversion of create_power_enum is remove_power_enum
# @param [Array] args Arguments to create_power_enum
# @return [Array] [:remove_power_enum, [enum_name]]
def invert_create_power_enum(args)
enum_name = args[0]
[:remove_enum, [enum_name]]
[:remove_power_enum, [enum_name]]
end
end

Expand Down
16 changes: 7 additions & 9 deletions lib/power_enum/schema/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ module SchemaStatements

def self.included(base) # :nodoc:
base::AbstractAdapter.class_eval do
# This squashes the "#create_enum" in the PostgreSQL adapter in Rails 7+.
# .../activerecord-7.0.X.Y/lib/active_record/connection_adapters/postgresql_adapter.rb
prepend PowerEnum::Schema::AbstractAdapter
include PowerEnum::Schema::AbstractAdapter
end
end

Expand Down Expand Up @@ -42,15 +40,15 @@ module AbstractAdapter
#
# ===== Examples
# ====== Basic Enum
# create_enum :connector_type
# create_power_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,
# create_power_enum :connector_type, :name_column => :connector,
# :name_limit => 50,
# :description => true,
# :desc_limit => 100,
Expand All @@ -67,7 +65,7 @@ module AbstractAdapter
# add_index :connector_types, [:connector], :unique => true
#
# ====== Customizing Enum with a block
# create_enum :connector_type, :description => true do |t|
# create_power_enum :connector_type, :description => true do |t|
# t.boolean :has_sound
# end
# is the equivalent of
Expand All @@ -79,7 +77,7 @@ module AbstractAdapter
# add_index :connector_types, [:connector], :unique => true
#
# Notice that a unique index is automatically created in each case on the proper name column.
def create_enum(enum_name, options = {}, &block)
def create_power_enum(enum_name, options = {}, &block)
enum_table_name = enum_name.pluralize

# For compatibility with PgPower/PgSaurus
Expand Down Expand Up @@ -117,10 +115,10 @@ def create_enum(enum_name, options = {}, &block)
# Drops the enum table. +enum_name+ will be automatically pluralized.
#
# ===== Example
# remove_enum :connector_type
# remove_power_enum :connector_type
# is the equivalent of
# drop_table :connector_types
def remove_enum(enum_name)
def remove_power_enum(enum_name)
drop_table enum_name.pluralize
end

Expand Down
2 changes: 1 addition & 1 deletion power_enum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '> 1.7'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3.1'
spec.add_development_dependency 'rails', '>= 6.0', '< 8'
spec.add_development_dependency 'rails', '>= 7.0', '< 8'

spec.add_runtime_dependency 'railties', '>= 6.0', '< 8'
spec.add_runtime_dependency 'activerecord', '>= 6.0', '< 8'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateConnectorTypes < ActiveRecord::Migration[4.2]
def change
create_enum :connector_type,
create_power_enum :connector_type,
:description => true,
:name_limit => 50,
:active => true,
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20120823014841_create_enum_color.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateEnumColor < ActiveRecord::Migration[4.2]

def change
create_enum :color
create_power_enum :color
end

end
4 changes: 2 additions & 2 deletions spec/dummy/db/migrate/20120909203515_create_enum_fruit.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateEnumFruit < ActiveRecord::Migration[4.2]

def up
create_enum :fruit, :name_column => :fruit_name, :description => :true
create_power_enum :fruit, :name_column => :fruit_name, :description => :true

ActiveRecord::Base.connection.execute "INSERT INTO fruits (fruit_name, description) VALUES ('apple', 'Apple');"
ActiveRecord::Base.connection.execute "INSERT INTO fruits (fruit_name, description) VALUES ('pear', 'Pear');"
Expand All @@ -10,7 +10,7 @@ def up
end

def down
remove_enum :fruit
remove_power_enum :fruit
end

end
4 changes: 2 additions & 2 deletions spec/dummy/db/migrate/20120909235526_create_virtual_enum.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class CreateVirtualEnum < ActiveRecord::Migration[4.2]
def up
create_enum :virtual_enum
create_power_enum :virtual_enum

ActiveRecord::Base.connection.execute "INSERT INTO virtual_enums (name) VALUES ('virtual_enum');"
end

def down
remove_enum :virtual_enum
remove_power_enum :virtual_enum
end
end
2 changes: 1 addition & 1 deletion spec/functional/create_enum_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe "create_enum migration methods" do
describe "create_power_enum migration methods" do

it "connector type should be defined" do
expect{
Expand Down
16 changes: 8 additions & 8 deletions spec/migration/command_recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class CommandRecorderStub
CommandRecorderStub.new
}

it '.create_enum' do
recorder.should_receive(:record).with(:create_enum, [])
it '.create_power_enum' do
recorder.should_receive(:record).with(:create_power_enum, [])

recorder.create_enum
recorder.create_power_enum
end

it '.remove_enum' do
recorder.should_receive(:record).with(:remove_enum, [])
it '.remove_power_enum' do
recorder.should_receive(:record).with(:remove_power_enum, [])

recorder.remove_enum
recorder.remove_power_enum
end

it '.invert_create_enum' do
recorder.invert_create_enum([:foo]).should eq([:remove_enum, [:foo]])
it '.invert_create_power_enum' do
recorder.invert_create_power_enum([:foo]).should eq([:remove_power_enum, [:foo]])
end
end
8 changes: 4 additions & 4 deletions spec/migration/schema_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def add_index(table_name, *args)
{ :foo => 1, :bar => 2, :baz => 3 }
}

it 'should create the enum table on \'create_enum\'' do
adapter_stub.create_enum(
it 'should create the enum table on \'create_power_enum\'' do
adapter_stub.create_power_enum(
'test_enum',
:name_column => 'name_column',
:description => true,
Expand All @@ -88,10 +88,10 @@ def add_index(table_name, *args)

end

it 'should drop the enum table on \'remove_enum\'' do
it 'should drop the enum table on \'remove_power_enum\'' do
adapter_stub.should_receive(:drop_table).with('test_enums')

adapter_stub.remove_enum('test_enum')
adapter_stub.remove_power_enum('test_enum')
end

end