power_enum 0.11.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
https://github.com/albertosaurus/power_enum
|
4
4
|
|
5
|
-
Enumerations for Rails 3.
|
5
|
+
Enumerations for Rails 3.1/3.2 Done Right.
|
6
|
+
|
7
|
+
NOTICE: Version 1.0 removes support from Rails 3.0, as that version of Rails has no longer been supported for
|
8
|
+
some time. The last version to support Rails 3.0 was 0.11.1.
|
6
9
|
|
7
10
|
## What is this?:
|
8
11
|
|
@@ -40,7 +43,7 @@ See "How to use it" below for more information.
|
|
40
43
|
## Requirements
|
41
44
|
|
42
45
|
* Ruby 1.8.7, 1.9.2, 1.9.3, JRuby 1.6+
|
43
|
-
* Rails 3.
|
46
|
+
* Rails 3.1, 3.2
|
44
47
|
|
45
48
|
## Installation
|
46
49
|
|
@@ -100,23 +103,7 @@ from a pre-test Rake task.
|
|
100
103
|
|
101
104
|
### migration
|
102
105
|
|
103
|
-
|
104
|
-
|
105
|
-
```ruby
|
106
|
-
class CreateEnumBookingStatus < ActiveRecord::Migration
|
107
|
-
|
108
|
-
def self.up
|
109
|
-
create_enum :booking_status
|
110
|
-
end
|
111
|
-
|
112
|
-
def self.down
|
113
|
-
remove_enum :booking_status
|
114
|
-
end
|
115
|
-
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
|
-
If you're using Rails 3.1 or later, it will look something like this:
|
106
|
+
When you open your migration file, it will look something like this:
|
120
107
|
|
121
108
|
```ruby
|
122
109
|
class CreateEnumBookingStatus < ActiveRecord::Migration
|
@@ -150,6 +137,8 @@ end
|
|
150
137
|
|
151
138
|
# It's highly recommended to add a foreign key constraint here.
|
152
139
|
# Ideally, you would use a gem of some sort to handle this.
|
140
|
+
# I have been using PgPower https://rubygems.org/gems/pg_power with much
|
141
|
+
# success.
|
153
142
|
execute "ALTER TABLE bookings ADD 'bookings_bookings_status_id_fk'"\
|
154
143
|
" FOREIGN KEY (status_id) REFERENCES booking_statuses (id);"
|
155
144
|
```
|
@@ -762,7 +751,7 @@ And finally run tests:
|
|
762
751
|
* Initial Version Copyright (c) 2005 Trevor Squires
|
763
752
|
* Rails 3 Updates Copyright (c) 2010 Pivotal Labs
|
764
753
|
* Initial Test Suite Copyright (c) 2011 Sergey Potapov
|
765
|
-
* Subsequent Updates Copyright (c) 2011-
|
754
|
+
* Subsequent Updates Copyright (c) 2011-2013 Arthur Shagall
|
766
755
|
|
767
756
|
Released under the MIT License. See the LICENSE file for more details.
|
768
757
|
|
@@ -8,10 +8,12 @@ class EnumGenerator < Rails::Generators::Base
|
|
8
8
|
class_option :migration, :type => :boolean, :default => true, :desc => 'Generate migration for the enum'
|
9
9
|
class_option :fixture, :type => :boolean, :default => false, :desc => 'Generate fixture for the enum'
|
10
10
|
|
11
|
+
# Generates the enum ActiveRecord model.
|
11
12
|
def generate_model
|
12
13
|
template 'model.rb.erb', "app/models/#{file_name}.rb"
|
13
14
|
end
|
14
|
-
|
15
|
+
|
16
|
+
# Generates the migration to create the enum table.
|
15
17
|
def generate_migration
|
16
18
|
template migration_template, "db/migrate/#{migration_file_name}.rb" if options.migration?
|
17
19
|
end
|
@@ -23,21 +25,26 @@ class EnumGenerator < Rails::Generators::Base
|
|
23
25
|
|
24
26
|
no_tasks do
|
25
27
|
|
28
|
+
# Returns the file name of the enum model without the .rb extension.
|
26
29
|
def file_name
|
27
30
|
enum_name.underscore
|
28
31
|
end
|
29
|
-
|
32
|
+
|
33
|
+
# Returns the class name of the enum.
|
30
34
|
def enum_class_name
|
31
35
|
file_name.camelize
|
32
36
|
end
|
33
|
-
|
37
|
+
|
38
|
+
# Returns the number of the last migration.
|
34
39
|
def current_migration_number
|
35
40
|
dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
|
36
41
|
Dir.glob(dirname).collect do |file|
|
37
42
|
File.basename(file).split("_").first.to_i
|
38
43
|
end.max.to_i
|
39
44
|
end
|
40
|
-
|
45
|
+
|
46
|
+
# Returns the next upcoming migration number. Sadly, Rails has no API for
|
47
|
+
# this, so we're reduced to copying from ActiveRecord::Generators::Migration
|
41
48
|
def next_migration_number
|
42
49
|
# Lifted directly from ActiveRecord::Generators::Migration
|
43
50
|
# Unfortunately, no API is provided by Rails at this time.
|
@@ -48,21 +55,25 @@ class EnumGenerator < Rails::Generators::Base
|
|
48
55
|
"%.3d" % next_migration_number
|
49
56
|
end
|
50
57
|
end
|
51
|
-
|
58
|
+
|
59
|
+
# Derives the name for the migration, something like 'create_enum_fruit'
|
52
60
|
def migration_name
|
53
61
|
"create_enum_#{file_name}"
|
54
62
|
end
|
55
|
-
|
63
|
+
|
64
|
+
# Returns the class name of our migration
|
56
65
|
def migration_class_name
|
57
66
|
migration_name.camelize
|
58
67
|
end
|
59
|
-
|
68
|
+
|
69
|
+
# Generates and returns the filename of our migration
|
60
70
|
def migration_file_name
|
61
71
|
"#{next_migration_number}_#{migration_name}"
|
62
72
|
end
|
63
|
-
|
73
|
+
|
74
|
+
# Returns the name of the template file for the migration.
|
64
75
|
def migration_template
|
65
|
-
|
76
|
+
'rails31_migration.rb.erb'
|
66
77
|
end
|
67
78
|
|
68
79
|
end
|
@@ -85,6 +85,7 @@ module PowerEnum::HasEnumerated # :nodoc:
|
|
85
85
|
:default,
|
86
86
|
:create_scope )
|
87
87
|
|
88
|
+
# Add a reflection for the enumerated attribute.
|
88
89
|
reflection = PowerEnum::Reflection::EnumerationReflection.new(part_id, options, self)
|
89
90
|
self.reflections = self.reflections.merge(part_id => reflection)
|
90
91
|
|
@@ -212,6 +213,7 @@ module PowerEnum::HasEnumerated # :nodoc:
|
|
212
213
|
|
213
214
|
end #has_enumerated
|
214
215
|
|
216
|
+
# If the lookup failure handler is a method name, wraps it in a lambda.
|
215
217
|
def get_lookup_failure_handler(failure_opt) # :nodoc:
|
216
218
|
if failure_opt.nil?
|
217
219
|
nil
|
@@ -1,6 +1,9 @@
|
|
1
1
|
# Copyright (c) 2011 Artem Kuzko
|
2
|
+
# Copyright (c) 2013 Zach Belzer
|
3
|
+
# Copyright (c) 2013 Arthur Shagall
|
2
4
|
# Released under the MIT license. See LICENSE for details.
|
3
5
|
|
6
|
+
# Used to patch ActiveRecord reflections.
|
4
7
|
module PowerEnum::Reflection
|
5
8
|
extend ActiveSupport::Concern
|
6
9
|
|
@@ -33,6 +36,7 @@ module PowerEnum::Reflection
|
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
39
|
+
# Reflection class for enum reflections. See ActiveRecord::Reflection
|
36
40
|
class EnumerationReflection < ActiveRecord::Reflection::MacroReflection
|
37
41
|
attr_reader :counter_cache_column
|
38
42
|
|
data/lib/testing/rspec.rb
CHANGED
@@ -59,6 +59,7 @@ if defined? RSpec
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
# Returns the class of <tt>enum</tt>, or enum if it's a class.
|
62
63
|
def get_enum_class(enum)
|
63
64
|
if enum.is_a?(Class)
|
64
65
|
enum
|
@@ -67,6 +68,7 @@ if defined? RSpec
|
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
71
|
+
# Validates the given enum.
|
70
72
|
def validate_enum(enum_class, item)
|
71
73
|
case item
|
72
74
|
when String, Symbol, Fixnum
|
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.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,24 +12,24 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2013-01-
|
15
|
+
date: 2013-01-14 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
19
19
|
requirement: !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
|
-
- -
|
22
|
+
- - ~>
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: 3.
|
24
|
+
version: '3.1'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
27
|
version_requirements: !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ~>
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 3.
|
32
|
+
version: '3.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: jeweler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- lib/generators/enum/USAGE
|
118
118
|
- lib/generators/enum/enum_generator.rb
|
119
119
|
- lib/generators/enum/templates/model.rb.erb
|
120
|
-
- lib/generators/enum/templates/rails30_migration.rb.erb
|
121
120
|
- lib/generators/enum/templates/rails31_migration.rb.erb
|
122
121
|
- lib/generators/virtual_enumerations_initializer/USAGE
|
123
122
|
- lib/generators/virtual_enumerations_initializer/templates/virtual_enumerations.rb.erb
|
@@ -145,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
144
|
version: '0'
|
146
145
|
segments:
|
147
146
|
- 0
|
148
|
-
hash:
|
147
|
+
hash: 796684312632035672
|
149
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
149
|
none: false
|
151
150
|
requirements:
|