power_enum 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -37,7 +37,10 @@ then run
37
37
 
38
38
  ## Gem Contents
39
39
 
40
- This package adds two mixins and a helper to Rails' ActiveRecord, as well as methods to migrations to simplify the creation of backing tables.
40
+ This package adds:
41
+ - Two mixins and a helper to ActiveRecord
42
+ - Methods to migrations to simplify the creation of backing tables
43
+ - A generator to make generating enums easy
41
44
 
42
45
  `acts_as_enumerated` provides capabilities to treat your model and its records as an enumeration.
43
46
  At a minimum, the database table for an acts\_as\_enumerated must contain an 'id' column and a column
@@ -54,16 +57,62 @@ cluttering up your models directory with acts\_as\_enumerated classes.
54
57
 
55
58
  ## How to use it
56
59
 
57
- In the following example, we'll look at a Booking that can have several types of statuses.
60
+ In the following example, we'll look at a Booking that can have several types of statuses, encapsulated by BookingStatus enums.
61
+
62
+ ### generator
63
+
64
+ Invoke the generator to create a basic enum:
65
+
66
+ `rails generate enum booking_status`
67
+
68
+ You should see output similar to this:
69
+
70
+ create app/models/booking_status.rb
71
+ create db/migrate/20110926012928_create_enum_booking_status.rb
72
+ invoke test_unit
73
+ create test/unit/booking_status_test.rb
74
+
75
+ That's all you need to get started. In many cases, no further work on the enum is necessary. You can run `rails generate enum --help`
76
+ to see a description of the generator options. Notice, that while a unit tests is generated by default, a fixture isn't. That is because
77
+ fixtures are not an ideal way to test acts\_as\_enumerated models. I generally prefer having a hook to seed the database from seeds.rb
78
+ from a pre-test Rake task.
58
79
 
59
80
  ### migration
60
81
 
82
+ If you're using Rails prior to 3.1, your migration file will look something like this:
83
+
84
+ class CreateEnumBookingStatus < ActiveRecord::Migration
85
+
86
+ def self.up
87
+ create_enum :booking_status
88
+ end
89
+
90
+ def self.down
91
+ create_enum :booking_status
92
+ end
93
+
94
+ end
95
+
96
+ If you're using Rails 3.1, it will look something like this:
97
+
98
+ class CreateEnumBookingStatus < ActiveRecord::Migration
99
+
100
+ def change
101
+ create_enum :booking_status
102
+ end
103
+
104
+ end
105
+
106
+ You can now customize it.
107
+
61
108
  create_enum :booking_status, :name_limit => 50
62
109
  # The above is equivalent to saying
63
110
  # create_table :booking_statuses do |t|
64
111
  # t.string :name, :limit => 50, :null => false
65
112
  # end
66
113
 
114
+ 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.
115
+
67
116
  create_table :bookings do |t|
68
117
  t.integer :status_id
69
118
 
@@ -229,8 +278,9 @@ the `has_enumerated` macro behaves more like an aggregation than an association.
229
278
  :on_lookup_failure => :optional_instance_method
230
279
  end
231
280
 
232
- By default, the foreign key is interpreted to be the name of your has\_enumerated field (in this case 'status') plus '\_id'. Additionally,
233
- the default value for `:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below.
281
+ By default, the foreign key is interpreted to be the name of your has\_enumerated field (in this case 'booking_status') plus '\_id'. Since we
282
+ chose to make the column name 'status\_id' for the sake of brevity, we must explicitly designate it. Additionally, the default value for
283
+ `:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below.
234
284
 
235
285
  With that, your Booking class will have the following methods defined:
236
286
 
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Create the Rails files for a PowerEnum enum.
3
+
4
+ Example:
5
+ rails generate enum booking_status
6
+
7
+ This will create:
8
+ BookingStatus model: app/models/booking_status.rb
9
+ Migration to create tables: db/migrate/db/migrate/20110925195833_create_enum_booking_status.rb
10
+ Unit test for model: test/unit/booking_status_test.rb
@@ -0,0 +1,59 @@
1
+ # Generator for PowerEnum
2
+ class EnumGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :enum_name, :type => :string
5
+ class_option :migration, :type => :boolean, :default => true, :desc => 'Generate migration for the enum'
6
+ class_option :fixture, :type => :boolean, :default => false, :desc => 'Generate fixture for the enum'
7
+
8
+ def generate_model
9
+ template 'model.rb.erb', "app/models/#{file_name}.rb"
10
+ end
11
+
12
+ def generate_migration
13
+ template migration_template, "db/migrate/#{migration_file_name}.rb" if options.migration?
14
+ end
15
+
16
+ hook_for :test_framework, :as => :model do |enum_generator_instance, test_generator_class|
17
+ # Need to do this because I'm changing the default value of the 'fixture' option.
18
+ enum_generator_instance.invoke( test_generator_class, [enum_generator_instance.enum_name], { 'fixture' => enum_generator_instance.options.fixture? } )
19
+ end
20
+
21
+ no_tasks do
22
+
23
+ def file_name
24
+ enum_name.underscore
25
+ end
26
+
27
+ def enum_class_name
28
+ file_name.camelize
29
+ end
30
+
31
+ def current_migration_number
32
+ dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
33
+ Dir.glob(dirname).collect do |file|
34
+ File.basename(file).split("_").first.to_i
35
+ end.max.to_i
36
+ end
37
+
38
+ def next_migration_number
39
+ ActiveRecord::Migration.next_migration_number( current_migration_number + 1 )
40
+ end
41
+
42
+ def migration_name
43
+ "create_enum_#{file_name}"
44
+ end
45
+
46
+ def migration_class_name
47
+ migration_name.camelize
48
+ end
49
+
50
+ def migration_file_name
51
+ "#{next_migration_number}_#{migration_name}"
52
+ end
53
+
54
+ def migration_template
55
+ Rails.version < '3.1' ? 'rails30_migration.rb.erb' : 'rails31_migration.rb.erb'
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ class <%= enum_class_name %> < ActiveRecord::Base
2
+ acts_as_enumerated
3
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_enum :<%=enum_name%>
5
+ end
6
+
7
+ def self.down
8
+ remove_enum :<%=enum_name%>
9
+ end
10
+
11
+ end
@@ -0,0 +1,7 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_enum :<%=enum_name%>
5
+ end
6
+
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,11 +12,11 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2011-09-25 00:00:00.000000000Z
15
+ date: 2011-09-26 00:00:00.000000000Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rails
19
- requirement: &28153380 !ruby/object:Gem::Requirement
19
+ requirement: &17555480 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ! '>='
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: 3.0.0
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *28153380
27
+ version_requirements: *17555480
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: jeweler
30
- requirement: &28152380 !ruby/object:Gem::Requirement
30
+ requirement: &17555000 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ! '>='
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: '0'
36
36
  type: :development
37
37
  prerelease: false
38
- version_requirements: *28152380
38
+ version_requirements: *17555000
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: rspec
41
- requirement: &28151860 !ruby/object:Gem::Requirement
41
+ requirement: &17554500 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ! '>='
@@ -46,10 +46,10 @@ dependencies:
46
46
  version: '0'
47
47
  type: :development
48
48
  prerelease: false
49
- version_requirements: *28151860
49
+ version_requirements: *17554500
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: sqlite3
52
- requirement: &28151280 !ruby/object:Gem::Requirement
52
+ requirement: &17553880 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - ! '>='
@@ -57,7 +57,18 @@ dependencies:
57
57
  version: '0'
58
58
  type: :development
59
59
  prerelease: false
60
- version_requirements: *28151280
60
+ version_requirements: *17553880
61
+ - !ruby/object:Gem::Dependency
62
+ name: genspec
63
+ requirement: &17553200 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: *17553200
61
72
  description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
62
73
  as though they were an enumeration of values.
63
74
 
@@ -81,6 +92,11 @@ files:
81
92
  - lib/active_record/acts/enumerated.rb
82
93
  - lib/active_record/aggregations/has_enumerated.rb
83
94
  - lib/active_record/virtual_enumerations.rb
95
+ - lib/generators/enum/USAGE
96
+ - lib/generators/enum/enum_generator.rb
97
+ - lib/generators/enum/templates/model.rb.erb
98
+ - lib/generators/enum/templates/rails30_migration.rb.erb
99
+ - lib/generators/enum/templates/rails31_migration.rb.erb
84
100
  - lib/power_enum.rb
85
101
  - lib/power_enum/migration/command_recorder.rb
86
102
  - lib/power_enum/schema/schema_statements.rb