power_enum 1.4.0 → 2.0.0.rc
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.
- checksums.yaml +8 -8
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.markdown +13 -12
- data/lib/generators/enum/enum_generator.rb +39 -11
- data/lib/generators/enum/enum_generator_helpers/migration_number.rb +13 -3
- data/lib/generators/enum/templates/model.rb.erb +1 -1
- data/lib/power_enum/enumerated.rb +22 -0
- data/lib/power_enum/reflection.rb +7 -1
- metadata +8 -8
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
M2Q1NDg3Yzc2MDU2NjZmM2VlNDUwZWVhNTMxMTAyMzA5M2M0MDgwOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDlkMWUzNTlkNDM1NWQ5YmEyNzk3Y2M0NzY3MzM5NWNlNWJiYjMzOA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzkzZTAyNGIwNGExMWIzMzhhNzBlNWYzNmZlMWYwMzA3MzQyN2ZmYmVlNTA1
|
10
|
+
MWNkZjNiYjYxYzk3NzMyNjkxMDQyNDM1ZDU0ZjEwMTliNjhiNjhhZDlmNzdj
|
11
|
+
ZTQ4YmQ2NTI0NThlYTZiMDQyODY3ZTQ1NTFiNzg0ZjBjODk5ZDE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTM4OWI0NWQ5MjBmYThkYTlmZjVkYzVkM2JkMjE2MWRjNjY0NzRmNzM5ODdk
|
14
|
+
MTEzMmU4NGQzOTM4MjNhNDJmNGE1NTIwNTdiZWY1NTc5YTFhYWQ1YWFiNTgy
|
15
|
+
YzUwODNmZTM4MmQ3ODQ2NzU0Mzg3YjI2NDNjM2M4NTRmYzA5MDQ=
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/README.markdown
CHANGED
@@ -1,19 +1,16 @@
|
|
1
1
|
# Power Enum
|
2
2
|
|
3
|
-
https://github.com/albertosaurus/
|
3
|
+
https://github.com/albertosaurus/power_enum_2
|
4
4
|
|
5
|
-
[](https://travis-ci.org/albertosaurus/power_enum_2)
|
6
|
+
[](https://codeclimate.com/github/albertosaurus/power_enum_2)
|
7
7
|
|
8
|
-
Enumerations for Rails
|
8
|
+
Enumerations for Rails 4 Done Right.
|
9
9
|
|
10
10
|
## Versions
|
11
11
|
|
12
|
-
* PowerEnum
|
13
|
-
* PowerEnum
|
14
|
-
|
15
|
-
NOTICE: Version 1.0 removes support from Rails 3.0, as that version of Rails has no longer been supported for
|
16
|
-
some time. The last version to support Rails 3.0 was 0.11.1.
|
12
|
+
* PowerEnum 2.X (this version) supports Rails 4.
|
13
|
+
* PowerEnum 1.X supports Rails 3.1/3.2, available here: https://github.com/albertosaurus/power_enum
|
17
14
|
|
18
15
|
## What is this?:
|
19
16
|
|
@@ -31,6 +28,8 @@ At it's most basic level, it allows you to say things along the lines of:
|
|
31
28
|
```ruby
|
32
29
|
# Create a provisional booking
|
33
30
|
booking = Booking.new( :status => BookingStatus[:provisional] )
|
31
|
+
# This also works
|
32
|
+
booking = Booking.new( :status => :provisional )
|
34
33
|
# Set the booking status to 'confirmed'
|
35
34
|
booking.status = :confirmed
|
36
35
|
booking = Booking.create( :status => :rejected )
|
@@ -50,14 +49,16 @@ See "How to use it" below for more information.
|
|
50
49
|
|
51
50
|
## Requirements
|
52
51
|
|
53
|
-
|
54
|
-
|
52
|
+
### PowerEnum 2.X
|
53
|
+
|
54
|
+
* Ruby 1.9.3, 2.0, JRuby 1.7+ (Ruby 1.9.3 or 2.0 required for development)
|
55
|
+
* Rails 4.0
|
55
56
|
|
56
57
|
## Installation
|
57
58
|
|
58
59
|
### READ THIS FIRST
|
59
60
|
|
60
|
-
|
61
|
+
This gem is signed. The public key is available
|
61
62
|
here: https://github.com/albertosaurus/power_enum (look for gem-public\_cert.pem). Hence, if
|
62
63
|
you can get an error like the following if you're installing in `HighSecurity` mode.
|
63
64
|
|
@@ -2,12 +2,12 @@
|
|
2
2
|
# Released under the MIT license. See LICENSE for details.
|
3
3
|
|
4
4
|
# Generator for PowerEnum
|
5
|
-
class EnumGenerator < Rails::Generators::
|
5
|
+
class EnumGenerator < Rails::Generators::Base
|
6
6
|
require File.expand_path('../enum_generator_helpers/migration_number', __FILE__)
|
7
|
-
include
|
8
|
-
extend EnumGeneratorHelpers::MigrationNumber
|
7
|
+
include EnumGeneratorHelpers::MigrationNumber
|
9
8
|
|
10
9
|
source_root File.expand_path('../templates', __FILE__)
|
10
|
+
argument :enum_name, :type => :string
|
11
11
|
class_option :migration, :type => :boolean, :default => true, :desc => 'Generate migration for the enum'
|
12
12
|
class_option :fixture, :type => :boolean, :default => false, :desc => 'Generate fixture for the enum'
|
13
13
|
|
@@ -18,17 +18,45 @@ class EnumGenerator < Rails::Generators::NamedBase
|
|
18
18
|
|
19
19
|
# Generates the migration to create the enum table.
|
20
20
|
def generate_migration
|
21
|
-
migration_template
|
22
|
-
end
|
23
|
-
|
24
|
-
# Do not pluralize enumeration names
|
25
|
-
def pluralize_table_names?
|
26
|
-
false
|
21
|
+
template migration_template, "db/migrate/#{migration_file_name}.rb" if options.migration?
|
27
22
|
end
|
28
23
|
|
29
24
|
hook_for :test_framework, :as => :model do |enum_generator_instance, test_generator_class|
|
30
25
|
# Need to do this because I'm changing the default value of the 'fixture' option.
|
31
|
-
|
32
|
-
|
26
|
+
enum_generator_instance.invoke( test_generator_class, [enum_generator_instance.enum_name], { 'fixture' => enum_generator_instance.options.fixture? } )
|
27
|
+
end
|
28
|
+
|
29
|
+
no_tasks do
|
30
|
+
|
31
|
+
# Returns the file name of the enum model without the .rb extension.
|
32
|
+
def file_name
|
33
|
+
enum_name.underscore
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the class name of the enum.
|
37
|
+
def enum_class_name
|
38
|
+
file_name.camelize
|
39
|
+
end
|
40
|
+
|
41
|
+
# Derives the name for the migration, something like 'create_enum_fruit'
|
42
|
+
def migration_name
|
43
|
+
"create_enum_#{file_name}"
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the class name of our migration
|
47
|
+
def migration_class_name
|
48
|
+
migration_name.camelize
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generates and returns the filename of our migration
|
52
|
+
def migration_file_name
|
53
|
+
"#{next_migration_number}_#{migration_name}"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the name of the template file for the migration.
|
57
|
+
def migration_template
|
58
|
+
'rails31_migration.rb.erb'
|
59
|
+
end
|
60
|
+
|
33
61
|
end
|
34
62
|
end
|
@@ -2,13 +2,23 @@
|
|
2
2
|
module EnumGeneratorHelpers
|
3
3
|
# Helper methods to figure out the migration number.
|
4
4
|
module MigrationNumber
|
5
|
+
|
6
|
+
# Returns the number of the last migration.
|
7
|
+
# @return [Fixnum]
|
8
|
+
def current_migration_number
|
9
|
+
dirname = "#{Rails.root}/db/migrate/[0-9]*_*.rb"
|
10
|
+
Dir.glob(dirname).collect do |file|
|
11
|
+
File.basename(file).split("_").first.to_i
|
12
|
+
end.max.to_i
|
13
|
+
end
|
14
|
+
|
5
15
|
# Returns the next upcoming migration number. Sadly, Rails has no API for
|
6
16
|
# this, so we're reduced to copying from ActiveRecord::Generators::Migration
|
7
17
|
# @return [Fixnum]
|
8
|
-
def next_migration_number
|
18
|
+
def next_migration_number
|
9
19
|
# Lifted directly from ActiveRecord::Generators::Migration
|
10
20
|
# Unfortunately, no API is provided by Rails at this time.
|
11
|
-
next_migration_number = current_migration_number
|
21
|
+
next_migration_number = current_migration_number + 1
|
12
22
|
if ActiveRecord::Base.timestamped_migrations
|
13
23
|
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
14
24
|
else
|
@@ -17,4 +27,4 @@ module EnumGeneratorHelpers
|
|
17
27
|
end
|
18
28
|
|
19
29
|
end
|
20
|
-
end
|
30
|
+
end
|
@@ -83,12 +83,34 @@ module PowerEnum::Enumerated
|
|
83
83
|
self.acts_enumerated_name_column = get_name_column(options)
|
84
84
|
|
85
85
|
unless self.is_a? PowerEnum::Enumerated::EnumClassMethods
|
86
|
+
preserve_query_aliases
|
86
87
|
extend_enum_class_methods( options )
|
87
88
|
end
|
88
89
|
end
|
89
90
|
|
91
|
+
# Rails 4 delegates all the finder methods to 'all'. PowerEnum overrides 'all'. Hence,
|
92
|
+
# the need to re-alias the query methods.
|
93
|
+
def preserve_query_aliases
|
94
|
+
class << self
|
95
|
+
alias_method :__all, :all
|
96
|
+
|
97
|
+
# From ActiveRecord::Querying
|
98
|
+
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :__all
|
99
|
+
delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :__all
|
100
|
+
delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, :to => :__all
|
101
|
+
delegate :find_by, :find_by!, :to => :__all
|
102
|
+
delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :__all
|
103
|
+
delegate :find_each, :find_in_batches, :to => :__all
|
104
|
+
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
|
105
|
+
:where, :preload, :eager_load, :includes, :from, :lock, :readonly,
|
106
|
+
:having, :create_with, :uniq, :distinct, :references, :none, :unscope, :to => :__all
|
107
|
+
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :__all
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
90
111
|
# Injects the class methods into model
|
91
112
|
def extend_enum_class_methods(options) #:nodoc:
|
113
|
+
|
92
114
|
extend PowerEnum::Enumerated::EnumClassMethods
|
93
115
|
|
94
116
|
class_eval do
|
@@ -47,7 +47,7 @@ module PowerEnum::Reflection
|
|
47
47
|
|
48
48
|
# See ActiveRecore::Reflection::MacroReflection
|
49
49
|
def initialize( name, options, active_record )
|
50
|
-
super :has_enumerated, name, options, active_record
|
50
|
+
super :has_enumerated, name, nil, options, active_record
|
51
51
|
end
|
52
52
|
|
53
53
|
# Returns the class name of the enum
|
@@ -106,5 +106,11 @@ module PowerEnum::Reflection
|
|
106
106
|
true
|
107
107
|
end
|
108
108
|
|
109
|
+
# An array of arrays of scopes. Each item in the outside array corresponds
|
110
|
+
# to a reflection in the #chain.
|
111
|
+
def scope_chain
|
112
|
+
scope ? [[scope]] : [[]]
|
113
|
+
end
|
114
|
+
|
109
115
|
end
|
110
116
|
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:
|
4
|
+
version: 2.0.0.rc
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trevor Squires
|
@@ -39,7 +39,7 @@ cert_chain:
|
|
39
39
|
amZaYUtWM2wwYTFkR2wyUVNMa2pScFFaRDFiRCtVSDdnT3F5N1piZGNzUkJM
|
40
40
|
NEg3VTV6VQpibEtkZEg2dXhDckRTTTdLYWJrelNPVmYKLS0tLS1FTkQgQ0VS
|
41
41
|
VElGSUNBVEUtLS0tLQo=
|
42
|
-
date:
|
42
|
+
date: 2013-06-30 00:00:00.000000000 Z
|
43
43
|
dependencies:
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
45
|
name: rails
|
@@ -47,14 +47,14 @@ dependencies:
|
|
47
47
|
requirements:
|
48
48
|
- - ~>
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
version:
|
50
|
+
version: 4.0.0
|
51
51
|
type: :runtime
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
55
|
- - ~>
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version:
|
57
|
+
version: 4.0.0
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
60
|
requirement: !ruby/object:Gem::Requirement
|
@@ -163,8 +163,7 @@ files:
|
|
163
163
|
- LICENSE
|
164
164
|
- README.markdown
|
165
165
|
homepage: http://github.com/albertosaurus/power_enum
|
166
|
-
licenses:
|
167
|
-
- MIT
|
166
|
+
licenses: []
|
168
167
|
metadata: {}
|
169
168
|
post_install_message:
|
170
169
|
rdoc_options: []
|
@@ -177,9 +176,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
177
176
|
version: '0'
|
178
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
178
|
requirements:
|
180
|
-
- - ! '
|
179
|
+
- - ! '>'
|
181
180
|
- !ruby/object:Gem::Version
|
182
|
-
version:
|
181
|
+
version: 1.3.1
|
183
182
|
requirements: []
|
184
183
|
rubyforge_project:
|
185
184
|
rubygems_version: 2.0.3
|
@@ -188,3 +187,4 @@ specification_version: 4
|
|
188
187
|
summary: Allows you to treat instances of your ActiveRecord models as though they
|
189
188
|
were an enumeration of values
|
190
189
|
test_files: []
|
190
|
+
has_rdoc:
|
metadata.gz.sig
CHANGED
Binary file
|