simple_enum 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/.travis.yml +7 -3
- data/Appraisals +14 -0
- data/Gemfile +7 -1
- data/Gemfile.lock +57 -0
- data/README.rdoc +48 -4
- data/Rakefile +3 -3
- data/gemfiles/rails-3.0.gemfile +11 -0
- data/gemfiles/rails-3.0.gemfile.lock +56 -0
- data/gemfiles/rails-3.1.gemfile +11 -0
- data/gemfiles/rails-3.1.gemfile.lock +58 -0
- data/gemfiles/rails-3.2.gemfile +11 -0
- data/gemfiles/rails-3.2.gemfile.lock +58 -0
- data/lib/simple_enum.rb +35 -16
- data/lib/simple_enum/enum_hash.rb +24 -14
- data/lib/simple_enum/mongoid.rb +52 -0
- data/lib/simple_enum/validation.rb +1 -1
- data/lib/simple_enum/version.rb +1 -1
- data/locales/en.yml +4 -0
- data/simple_enum.gemspec +8 -6
- data/test/array_conversions_test.rb +9 -11
- data/test/class_methods_test.rb +20 -34
- data/test/dirty_attributes_test.rb +4 -5
- data/test/enum_hash_test.rb +16 -21
- data/test/finders_test.rb +13 -9
- data/test/locales.yml +17 -9
- data/test/mongoid_test.rb +38 -0
- data/test/object_backed_test.rb +17 -23
- data/test/orm/active_record.rb +99 -0
- data/test/orm/common.rb +23 -0
- data/test/orm/mongoid.rb +101 -0
- data/test/poro_test.rb +20 -0
- data/test/prefixes_test.rb +5 -7
- data/test/simple_enum_test.rb +68 -55
- data/test/test_helper.rb +22 -49
- data/test/without_shortcuts_test.rb +8 -9
- metadata +101 -39
- data/lib/simple_enum/object_support.rb +0 -37
- data/test/object_support_test.rb +0 -29
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
appraise "rails-3.0" do
|
2
|
+
gem 'activesupport', '~> 3.0.9'
|
3
|
+
gem 'activerecord', '~> 3.0.9'
|
4
|
+
end
|
5
|
+
|
6
|
+
appraise "rails-3.1" do
|
7
|
+
gem 'activesupport', '~> 3.1.0'
|
8
|
+
gem 'activerecord', '~> 3.1.0'
|
9
|
+
end
|
10
|
+
|
11
|
+
appraise "rails-3.2" do
|
12
|
+
gem 'activesupport', '~> 3.2.0'
|
13
|
+
gem 'activerecord', '~> 3.2.0'
|
14
|
+
end
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
simple_enum (1.5.0)
|
5
|
+
activesupport (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.0)
|
11
|
+
activesupport (= 3.2.0)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.0)
|
14
|
+
activemodel (= 3.2.0)
|
15
|
+
activesupport (= 3.2.0)
|
16
|
+
arel (~> 3.0.0)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.0)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (3.0.0)
|
25
|
+
bson (1.5.2)
|
26
|
+
bson (1.5.2-java)
|
27
|
+
bson_ext (1.5.2)
|
28
|
+
bson (= 1.5.2)
|
29
|
+
builder (3.0.0)
|
30
|
+
i18n (0.6.0)
|
31
|
+
jdbc-sqlite3 (3.7.2)
|
32
|
+
minitest (2.10.1)
|
33
|
+
mongo (1.5.2)
|
34
|
+
bson (= 1.5.2)
|
35
|
+
mongoid (2.4.1)
|
36
|
+
activemodel (~> 3.1)
|
37
|
+
mongo (~> 1.3)
|
38
|
+
tzinfo (~> 0.3.22)
|
39
|
+
multi_json (1.0.4)
|
40
|
+
rake (0.9.2.2)
|
41
|
+
sqlite3 (1.3.5)
|
42
|
+
tzinfo (0.3.31)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
java
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
activerecord (>= 3.0.0)
|
50
|
+
appraisal (>= 0.4)
|
51
|
+
bson_ext
|
52
|
+
jdbc-sqlite3
|
53
|
+
minitest (>= 2.3.0)
|
54
|
+
mongoid (~> 2.0)
|
55
|
+
rake (>= 0.9.2)
|
56
|
+
simple_enum!
|
57
|
+
sqlite3
|
data/README.rdoc
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
= SimpleEnum - unobtrusive enum-like fields for ActiveRecord
|
2
2
|
|
3
3
|
A Rails plugin which brings easy-to-use enum-like functionality to
|
4
|
-
ActiveRecord models (now compatible with rails 3.1, ruby 1.9 and jruby).
|
4
|
+
ActiveRecord and Mongoid models (now compatible with rails 3.1, ruby 1.9 and jruby).
|
5
5
|
|
6
6
|
Since version 1.4, simple_enum is no longer compatible with activerecord 2.x, use
|
7
7
|
version 1.3.2 instead: <https://github.com/lwe/simple_enum/tree/v1.3.2>.
|
8
8
|
|
9
9
|
*Note*: a recent search on github for `enum` turned out, that there are many, many similar solutions.
|
10
10
|
|
11
|
-
== Quick start
|
11
|
+
== ActiveRecord Quick start
|
12
12
|
|
13
13
|
Add this to a model:
|
14
14
|
|
@@ -16,7 +16,7 @@ Add this to a model:
|
|
16
16
|
as_enum :gender, :female => 1, :male => 0
|
17
17
|
end
|
18
18
|
|
19
|
-
Then create the
|
19
|
+
Then create the required `gender_cd` column using migrations:
|
20
20
|
|
21
21
|
class AddGenderColumnToUser < ActiveRecord::Migration
|
22
22
|
def self.up
|
@@ -28,6 +28,24 @@ Then create the new column using migrations:
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
== Mongoid Quick start
|
32
|
+
|
33
|
+
Add this to an initializer
|
34
|
+
|
35
|
+
# load mongoid support
|
36
|
+
require 'simple_enum/mongoid'
|
37
|
+
|
38
|
+
Add this to a model:
|
39
|
+
|
40
|
+
class User
|
41
|
+
include Mongoid::Document
|
42
|
+
include SimpleEnum::Mongoid
|
43
|
+
|
44
|
+
as_enum :gender, :female => 1, :male => 0
|
45
|
+
end
|
46
|
+
|
47
|
+
== Working with enums
|
48
|
+
|
31
49
|
*Done*. Now it's possible to pull some neat tricks on the new column, yet
|
32
50
|
the original db column (+gender_cd+) is still intact and not touched by
|
33
51
|
any fancy metaclass or similar.
|
@@ -64,6 +82,14 @@ useful when creating queries, displaying option elements or similar:
|
|
64
82
|
*Disclaimer*: if you _ever_ decide to reorder this array, beaware that any previous mapping is lost. So it's recommended
|
65
83
|
to create mappings (that might change) using hashes instead of arrays. For stuff like gender it might be probably perfectly
|
66
84
|
fine to use arrays though.
|
85
|
+
* Want to use `SimpleEnum` in an ActiveModel, or other class, just do:
|
86
|
+
|
87
|
+
class MyModel
|
88
|
+
include SimpleEnum
|
89
|
+
attr_accessor :gender_cd
|
90
|
+
as_enum :gender, [:male, :female]
|
91
|
+
end
|
92
|
+
|
67
93
|
* Maybe you've columns named differently than the proposed <tt>{column}_cd</tt> naming scheme, feel free to use any column name
|
68
94
|
by providing an option:
|
69
95
|
|
@@ -71,6 +97,15 @@ useful when creating queries, displaying option elements or similar:
|
|
71
97
|
as_enum :gender, [:male, :female], :column => 'sex'
|
72
98
|
end
|
73
99
|
|
100
|
+
* Need to provide custom options for the mongoid field, or skip the automatically generated field?
|
101
|
+
|
102
|
+
# skip field generation
|
103
|
+
field :gender_cd # <- create field manually (!)
|
104
|
+
as_enum :gender, [:male, :female], :field => false
|
105
|
+
|
106
|
+
# custom field options (directly passed to Mongoid::Document#field)
|
107
|
+
as_enum :gender, [:male, :female], :field => { :type => Integer, :default => 1 }
|
108
|
+
|
74
109
|
* To make it easier to create dropdowns with values use:
|
75
110
|
|
76
111
|
<%= select(:user, :gender, User.genders.keys) %>
|
@@ -99,7 +134,8 @@ useful when creating queries, displaying option elements or similar:
|
|
99
134
|
The <tt>:prefix</tt> option not only takes a boolean value as an argument, but instead can also be supplied a custom
|
100
135
|
prefix (i.e. any string or symbol), so with <tt>:prefix => 'foo'</tt> all shortcut methods would look like: <tt>foo_<symbol>...</tt>
|
101
136
|
*Note*: if the <tt>:slim => true</tt> is defined, this option has no effect whatsoever (because no shortcut methods are generated).
|
102
|
-
* Sometimes it might be useful to disable the generation of the shortcut methods (<tt><symbol>?</tt>, <tt><symbol>!</tt> and <tt>Klass.<symbol></tt>),
|
137
|
+
* Sometimes it might be useful to disable the generation of the shortcut methods (<tt><symbol>?</tt>, <tt><symbol>!</tt> and <tt>Klass.<symbol></tt>),
|
138
|
+
to do so just add the option <tt>:slim => true</tt>:
|
103
139
|
|
104
140
|
class User < ActiveRecord::Base
|
105
141
|
as_enum :gender, [:male, :female], :slim => true
|
@@ -171,6 +207,14 @@ deprecated starting with version 1.4.1, e.g.
|
|
171
207
|
# GOOD
|
172
208
|
as_enum :project_status, [:active, :inactive, :archived], :column => "status"
|
173
209
|
|
210
|
+
Do not use states named after existing, or well known method names, like `new` or `create`, e.g.
|
211
|
+
|
212
|
+
# BAD, conflicts with Rails ActiveRecord Methods (!)
|
213
|
+
as_enum :handle, [:new, :create, :update]
|
214
|
+
|
215
|
+
# BETTER, prefixes all methods
|
216
|
+
as_enum :handle, [:new, :create, :update], :prefix => true
|
217
|
+
|
174
218
|
Searching for certain values by using the finder methods:
|
175
219
|
|
176
220
|
User.where(:gender_cd => User.female)
|
data/Rakefile
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'bundler'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'appraisal'
|
3
4
|
require 'rake/testtask'
|
4
5
|
|
5
6
|
include Rake::DSL
|
@@ -9,9 +10,8 @@ Bundler::GemHelper.install_tasks
|
|
9
10
|
desc 'Default: run unit tests.'
|
10
11
|
task :default => :test
|
11
12
|
|
12
|
-
desc '
|
13
|
+
desc 'Run unit tests, use ORM=...'
|
13
14
|
Rake::TestTask.new(:test) do |t|
|
14
|
-
t.libs << 'lib'
|
15
15
|
t.libs << 'test'
|
16
16
|
t.test_files = Dir.glob('test/**/*_test.rb')
|
17
17
|
t.verbose = true
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "jdbc-sqlite3", :platform=>:jruby
|
6
|
+
gem "sqlite3", :platform=>:ruby
|
7
|
+
gem "bson_ext", :platform=>:ruby
|
8
|
+
gem "activesupport", "~> 3.0.9"
|
9
|
+
gem "activerecord", "~> 3.0.9"
|
10
|
+
|
11
|
+
gemspec :path=>"../"
|
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/lukas/Projects/_github/simple_enum
|
3
|
+
specs:
|
4
|
+
simple_enum (1.5.0)
|
5
|
+
activesupport (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.11)
|
11
|
+
activesupport (= 3.0.11)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
activerecord (3.0.11)
|
15
|
+
activemodel (= 3.0.11)
|
16
|
+
activesupport (= 3.0.11)
|
17
|
+
arel (~> 2.0.10)
|
18
|
+
tzinfo (~> 0.3.23)
|
19
|
+
activesupport (3.0.11)
|
20
|
+
appraisal (0.4.0)
|
21
|
+
bundler
|
22
|
+
rake
|
23
|
+
arel (2.0.10)
|
24
|
+
bson (1.5.2)
|
25
|
+
bson (1.5.2-java)
|
26
|
+
bson_ext (1.5.2)
|
27
|
+
bson (= 1.5.2)
|
28
|
+
builder (2.1.2)
|
29
|
+
i18n (0.5.0)
|
30
|
+
jdbc-sqlite3 (3.7.2)
|
31
|
+
minitest (2.10.1)
|
32
|
+
mongo (1.5.2)
|
33
|
+
bson (= 1.5.2)
|
34
|
+
mongoid (2.2.5)
|
35
|
+
activemodel (~> 3.0)
|
36
|
+
mongo (~> 1.3)
|
37
|
+
tzinfo (~> 0.3.22)
|
38
|
+
rake (0.9.2.2)
|
39
|
+
sqlite3 (1.3.5)
|
40
|
+
tzinfo (0.3.31)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
java
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
activerecord (~> 3.0.9)
|
48
|
+
activesupport (~> 3.0.9)
|
49
|
+
appraisal (>= 0.4)
|
50
|
+
bson_ext
|
51
|
+
jdbc-sqlite3
|
52
|
+
minitest (>= 2.3.0)
|
53
|
+
mongoid (~> 2.0)
|
54
|
+
rake (>= 0.9.2)
|
55
|
+
simple_enum!
|
56
|
+
sqlite3
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "jdbc-sqlite3", :platform=>:jruby
|
6
|
+
gem "sqlite3", :platform=>:ruby
|
7
|
+
gem "bson_ext", :platform=>:ruby
|
8
|
+
gem "activesupport", "~> 3.1.0"
|
9
|
+
gem "activerecord", "~> 3.1.0"
|
10
|
+
|
11
|
+
gemspec :path=>"../"
|
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/lukas/Projects/_github/simple_enum
|
3
|
+
specs:
|
4
|
+
simple_enum (1.5.0)
|
5
|
+
activesupport (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.1.3)
|
11
|
+
activesupport (= 3.1.3)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activerecord (3.1.3)
|
15
|
+
activemodel (= 3.1.3)
|
16
|
+
activesupport (= 3.1.3)
|
17
|
+
arel (~> 2.2.1)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activesupport (3.1.3)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (2.2.1)
|
25
|
+
bson (1.5.2)
|
26
|
+
bson (1.5.2-java)
|
27
|
+
bson_ext (1.5.2)
|
28
|
+
bson (= 1.5.2)
|
29
|
+
builder (3.0.0)
|
30
|
+
i18n (0.6.0)
|
31
|
+
jdbc-sqlite3 (3.7.2)
|
32
|
+
minitest (2.10.1)
|
33
|
+
mongo (1.5.2)
|
34
|
+
bson (= 1.5.2)
|
35
|
+
mongoid (2.4.1)
|
36
|
+
activemodel (~> 3.1)
|
37
|
+
mongo (~> 1.3)
|
38
|
+
tzinfo (~> 0.3.22)
|
39
|
+
multi_json (1.0.4)
|
40
|
+
rake (0.9.2.2)
|
41
|
+
sqlite3 (1.3.5)
|
42
|
+
tzinfo (0.3.31)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
java
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
activerecord (~> 3.1.0)
|
50
|
+
activesupport (~> 3.1.0)
|
51
|
+
appraisal (>= 0.4)
|
52
|
+
bson_ext
|
53
|
+
jdbc-sqlite3
|
54
|
+
minitest (>= 2.3.0)
|
55
|
+
mongoid (~> 2.0)
|
56
|
+
rake (>= 0.9.2)
|
57
|
+
simple_enum!
|
58
|
+
sqlite3
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "jdbc-sqlite3", :platform=>:jruby
|
6
|
+
gem "sqlite3", :platform=>:ruby
|
7
|
+
gem "bson_ext", :platform=>:ruby
|
8
|
+
gem "activesupport", "~> 3.2.0"
|
9
|
+
gem "activerecord", "~> 3.2.0"
|
10
|
+
|
11
|
+
gemspec :path=>"../"
|
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/lukas/Projects/_github/simple_enum
|
3
|
+
specs:
|
4
|
+
simple_enum (1.5.0)
|
5
|
+
activesupport (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.0)
|
11
|
+
activesupport (= 3.2.0)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.0)
|
14
|
+
activemodel (= 3.2.0)
|
15
|
+
activesupport (= 3.2.0)
|
16
|
+
arel (~> 3.0.0)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.0)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (3.0.0)
|
25
|
+
bson (1.5.2)
|
26
|
+
bson (1.5.2-java)
|
27
|
+
bson_ext (1.5.2)
|
28
|
+
bson (= 1.5.2)
|
29
|
+
builder (3.0.0)
|
30
|
+
i18n (0.6.0)
|
31
|
+
jdbc-sqlite3 (3.7.2)
|
32
|
+
minitest (2.10.1)
|
33
|
+
mongo (1.5.2)
|
34
|
+
bson (= 1.5.2)
|
35
|
+
mongoid (2.4.1)
|
36
|
+
activemodel (~> 3.1)
|
37
|
+
mongo (~> 1.3)
|
38
|
+
tzinfo (~> 0.3.22)
|
39
|
+
multi_json (1.0.4)
|
40
|
+
rake (0.9.2.2)
|
41
|
+
sqlite3 (1.3.5)
|
42
|
+
tzinfo (0.3.31)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
java
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
activerecord (~> 3.2.0)
|
50
|
+
activesupport (~> 3.2.0)
|
51
|
+
appraisal (>= 0.4)
|
52
|
+
bson_ext
|
53
|
+
jdbc-sqlite3
|
54
|
+
minitest (>= 2.3.0)
|
55
|
+
mongoid (~> 2.0)
|
56
|
+
rake (>= 0.9.2)
|
57
|
+
simple_enum!
|
58
|
+
sqlite3
|