enumify 0.0.1 → 0.0.2
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/CHANGELOG.md +4 -0
- data/Readme.md +15 -2
- data/enumify.gemspec +2 -1
- data/lib/enumify/model.rb +5 -0
- data/lib/enumify/version.rb +1 -1
- data/spec/enumify/enum_spec.rb +42 -6
- data/spec/spec_helper.rb +19 -1
- metadata +19 -5
data/CHANGELOG.md
CHANGED
data/Readme.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# Enumify
|
1
|
+
# Enumify [](http://travis-ci.org/yonbergman/enumify)
|
2
|
+
|
2
3
|
|
3
4
|
enumify adds an enum command to all ActiveRecord models which enables you to work with string attributes as if they were enums
|
4
5
|
|
@@ -46,6 +47,18 @@ All you need to do is add a x_changed method in your class and the enumify will
|
|
46
47
|
end
|
47
48
|
|
48
49
|
|
50
|
+
## Scopes
|
51
|
+
One last thing that the enumify gem does is created scope (formerly nested_scopes) so you can easly query by the enum
|
52
|
+
|
53
|
+
For example if you want to count all the events that are canceled you can just run
|
54
|
+
|
55
|
+
Event.canceled.count
|
56
|
+
|
57
|
+
In addition you can also use a negation scope to retrieve all the records that are not set to the given value.
|
58
|
+
For example to count all the events that are not canceled you can run
|
59
|
+
|
60
|
+
Event.not_canceled.count
|
61
|
+
|
49
62
|
---
|
50
63
|
|
51
|
-
Copyright (c) 2011 Yonatan Bergman, released under the MIT license
|
64
|
+
Copyright (c) 2011 Yonatan Bergman, released under the MIT license
|
data/enumify.gemspec
CHANGED
@@ -21,5 +21,6 @@ Gem::Specification.new do |s|
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency "rake"
|
23
23
|
s.add_development_dependency "rspec"
|
24
|
-
s.add_development_dependency "
|
24
|
+
s.add_development_dependency "activerecord"
|
25
|
+
s.add_development_dependency "sqlite3"
|
25
26
|
end
|
data/lib/enumify/model.rb
CHANGED
@@ -40,7 +40,12 @@ module Enumify
|
|
40
40
|
send("_set_#{parameter.to_s}", opt, true)
|
41
41
|
end
|
42
42
|
|
43
|
+
|
43
44
|
scope opt.to_sym, where(parameter.to_sym => opt.to_s)
|
45
|
+
# We need to prefix the field with the table name since if this scope will
|
46
|
+
# be used in a joined query with other models that have the same enum field then
|
47
|
+
# it will fail on ambiguous column name.
|
48
|
+
scope "not_#{opt}".to_sym, where("#{self.table_name}.#{parameter} != ?", opt.to_s)
|
44
49
|
end
|
45
50
|
|
46
51
|
end
|
data/lib/enumify/version.rb
CHANGED
data/spec/enumify/enum_spec.rb
CHANGED
@@ -1,19 +1,31 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class Model <
|
4
|
-
include ActiveModel::Validations
|
3
|
+
class Model < ActiveRecord::Base
|
5
4
|
extend Enumify::Model
|
6
|
-
def self.scope(name,hash={}) self end
|
7
|
-
def self.where(hash={}) self end
|
8
5
|
|
9
6
|
enum :status, [:available, :canceled, :completed]
|
10
|
-
|
7
|
+
end
|
8
|
+
|
9
|
+
class OtherModel < ActiveRecord::Base
|
10
|
+
extend Enumify::Model
|
11
|
+
|
12
|
+
belongs_to :model
|
13
|
+
|
14
|
+
enum :status, [:active, :expired]
|
11
15
|
end
|
12
16
|
|
13
17
|
describe :Enumify do
|
14
18
|
|
15
19
|
before(:each) do
|
16
|
-
|
20
|
+
Model.delete_all
|
21
|
+
OtherModel.delete_all
|
22
|
+
|
23
|
+
@obj = Model.create!(:status => :available)
|
24
|
+
@canceled_obj = Model.create!(:status => :canceled)
|
25
|
+
@completed_obj = Model.create!(:status => :completed)
|
26
|
+
|
27
|
+
@active_obj = OtherModel.create!(:status => :active, :model => @obj)
|
28
|
+
@expired_obj = OtherModel.create!(:status => :expired, :model => @canceled_obj)
|
17
29
|
end
|
18
30
|
|
19
31
|
describe "short hand methods" do
|
@@ -97,6 +109,30 @@ describe :Enumify do
|
|
97
109
|
|
98
110
|
end
|
99
111
|
|
112
|
+
describe "scopes" do
|
113
|
+
it "should return objects with given value" do
|
114
|
+
Model.available.should == [@obj]
|
115
|
+
Model.canceled.should == [@canceled_obj]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return objects with given value when joined with models who have the same enum field" do
|
119
|
+
OtherModel.joins(:model).active.should == [@active_obj]
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "negation scopes" do
|
123
|
+
|
124
|
+
it "should return objects that do not have the given value" do
|
125
|
+
Model.not_available.should include(@canceled_obj, @completed_obj)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should return objects that do not have the given value when joined with models who have the same enum field" do
|
129
|
+
OtherModel.joins(:model).not_active.should == [@expired_obj]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
100
136
|
it "class should have a CONST that holds all the available options of the enum" do
|
101
137
|
Model::STATUSES.should == [:available, :canceled, :completed]
|
102
138
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,20 @@
|
|
1
1
|
require 'enumify'
|
2
|
-
require '
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
def set_database
|
5
|
+
db_config = {:adapter => "sqlite3", :database => ":memory:"}
|
6
|
+
ActiveRecord::Base.establish_connection(db_config)
|
7
|
+
connection = ActiveRecord::Base.connection
|
8
|
+
|
9
|
+
connection.create_table :models do |t|
|
10
|
+
t.string :status
|
11
|
+
end
|
12
|
+
|
13
|
+
connection.create_table :other_models do |t|
|
14
|
+
t.string :status
|
15
|
+
t.references :model
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
set_database
|
20
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- yon
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-04-18 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -46,7 +46,7 @@ dependencies:
|
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name:
|
49
|
+
name: activerecord
|
50
50
|
prerelease: false
|
51
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
@@ -59,6 +59,20 @@ dependencies:
|
|
59
59
|
version: "0"
|
60
60
|
type: :development
|
61
61
|
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sqlite3
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
62
76
|
description: enumify adds an enum command to all ActiveRecord models which enables you to work with string attributes as if they were enums
|
63
77
|
email:
|
64
78
|
- yonatanbergman@gmail.com
|