enumify 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3 -0
- data/lib/enumify/model.rb +11 -2
- data/lib/enumify/version.rb +1 -1
- data/spec/enumify/enum_spec.rb +11 -2
- metadata +4 -4
data/CHANGELOG.md
CHANGED
data/lib/enumify/model.rb
CHANGED
@@ -18,6 +18,7 @@ module Enumify
|
|
18
18
|
self.class_eval do
|
19
19
|
|
20
20
|
private
|
21
|
+
|
21
22
|
define_method "_set_#{parameter.to_s}" do |value, should_save|
|
22
23
|
|
23
24
|
value = value.to_sym
|
@@ -27,6 +28,7 @@ module Enumify
|
|
27
28
|
send("#{parameter.to_s}_changed", old, value) if respond_to?("#{parameter.to_s}_changed", true) and old != value and !old.nil?
|
28
29
|
return value
|
29
30
|
end
|
31
|
+
|
30
32
|
end
|
31
33
|
|
32
34
|
opts.each do |opt|
|
@@ -40,15 +42,22 @@ module Enumify
|
|
40
42
|
send("_set_#{parameter.to_s}", opt, true)
|
41
43
|
end
|
42
44
|
|
43
|
-
|
44
45
|
scope opt.to_sym, where(parameter.to_sym => opt.to_s)
|
46
|
+
end
|
47
|
+
|
48
|
+
# We want to first define all the "positive" scopes and only then define
|
49
|
+
# the "negation scopes", to make sure they don't override previous scopes
|
50
|
+
opts.each do |opt|
|
45
51
|
# We need to prefix the field with the table name since if this scope will
|
46
52
|
# be used in a joined query with other models that have the same enum field then
|
47
53
|
# it will fail on ambiguous column name.
|
48
|
-
|
54
|
+
unless respond_to?("not_#{opt}")
|
55
|
+
scope "not_#{opt}", where("#{self.table_name}.#{parameter} != ?", opt.to_s)
|
56
|
+
end
|
49
57
|
end
|
50
58
|
|
51
59
|
end
|
52
60
|
|
53
61
|
end
|
62
|
+
|
54
63
|
end
|
data/lib/enumify/version.rb
CHANGED
data/spec/enumify/enum_spec.rb
CHANGED
@@ -11,7 +11,7 @@ class OtherModel < ActiveRecord::Base
|
|
11
11
|
|
12
12
|
belongs_to :model
|
13
13
|
|
14
|
-
enum :status, [:active, :expired]
|
14
|
+
enum :status, [:active, :expired, :not_expired]
|
15
15
|
end
|
16
16
|
|
17
17
|
describe :Enumify do
|
@@ -26,6 +26,7 @@ describe :Enumify do
|
|
26
26
|
|
27
27
|
@active_obj = OtherModel.create!(:status => :active, :model => @obj)
|
28
28
|
@expired_obj = OtherModel.create!(:status => :expired, :model => @canceled_obj)
|
29
|
+
@not_expired_obj = OtherModel.create!(:status => :not_expired, :model => @canceled_obj)
|
29
30
|
end
|
30
31
|
|
31
32
|
describe "short hand methods" do
|
@@ -126,8 +127,16 @@ describe :Enumify do
|
|
126
127
|
end
|
127
128
|
|
128
129
|
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
|
130
|
+
OtherModel.joins(:model).not_active.should include(@expired_obj, @not_expired_obj)
|
130
131
|
end
|
132
|
+
|
133
|
+
it "should not override positive scopes" do
|
134
|
+
# We want here to verify that the not_expired scope return only the models with
|
135
|
+
# status == "not_expired" and not all the models with status != "expired",
|
136
|
+
# since negation scopes should not override the "positive" scopes.
|
137
|
+
OtherModel.not_expired.should == [@not_expired_obj]
|
138
|
+
end
|
139
|
+
|
131
140
|
end
|
132
141
|
|
133
142
|
end
|
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: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
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: 2012-
|
18
|
+
date: 2012-06-03 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|