active_enum 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -0
- data/lib/active_enum/acts_as_enum.rb +1 -1
- data/lib/active_enum/base.rb +17 -2
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/acts_as_enum_spec.rb +14 -6
- data/spec/active_enum/base_spec.rb +18 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -35,6 +35,15 @@ Beware that if you change the order of values defined in an enum which don't hav
|
|
35
35
|
This could corrupt your data if the enum values have been stored in a model record, as they will no longer map to
|
36
36
|
the original enum.
|
37
37
|
|
38
|
+
You can also define the sort order of returned values
|
39
|
+
|
40
|
+
class Sex < ActiveEnum::Base
|
41
|
+
order :desc
|
42
|
+
|
43
|
+
value :id => 1, :name => 'Male'
|
44
|
+
value :id => 2, :name => 'Female'
|
45
|
+
end
|
46
|
+
|
38
47
|
Enum class usage
|
39
48
|
|
40
49
|
Sex[1] # => 'Male'
|
@@ -14,7 +14,7 @@ module ActiveEnum
|
|
14
14
|
named_scope :enum_values,
|
15
15
|
:select => "#{primary_key}, #{active_enum_options[:name_column]}",
|
16
16
|
:conditions => active_enum_options[:conditions],
|
17
|
-
:order => active_enum_options[:order]
|
17
|
+
:order => "#{primary_key} #{active_enum_options[:order]}"
|
18
18
|
end
|
19
19
|
|
20
20
|
def ids
|
data/lib/active_enum/base.rb
CHANGED
@@ -19,9 +19,15 @@ module ActiveEnum
|
|
19
19
|
check_duplicate(id, enum_value[:name])
|
20
20
|
|
21
21
|
@values << [id, enum_value[:name]]
|
22
|
-
|
22
|
+
sort_values!
|
23
23
|
end
|
24
24
|
|
25
|
+
# order enum values using :asc or :desc
|
26
|
+
#
|
27
|
+
def order(order)
|
28
|
+
@order = order
|
29
|
+
end
|
30
|
+
|
25
31
|
def all
|
26
32
|
@values || []
|
27
33
|
end
|
@@ -35,7 +41,7 @@ module ActiveEnum
|
|
35
41
|
end
|
36
42
|
|
37
43
|
def to_select
|
38
|
-
|
44
|
+
@values.map {|v| [v[1], v[0]] }
|
39
45
|
end
|
40
46
|
|
41
47
|
def [](index)
|
@@ -70,6 +76,15 @@ module ActiveEnum
|
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
79
|
+
def sort_values!
|
80
|
+
case (@order || :asc)
|
81
|
+
when :asc
|
82
|
+
@values.sort! {|a,b| a[0] <=> b[0] }
|
83
|
+
when :desc
|
84
|
+
@values.sort! {|a,b| b[0] <=> a[0] }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
73
88
|
end
|
74
89
|
|
75
90
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -6,23 +6,31 @@ end
|
|
6
6
|
|
7
7
|
describe ActiveEnum::ActsAsEnum do
|
8
8
|
before(:all) do
|
9
|
-
|
9
|
+
Person.create!(:first_name => 'Dave', :last_name => 'Smith')
|
10
|
+
Person.create!(:first_name => 'John', :last_name => 'Doe')
|
10
11
|
end
|
11
12
|
|
12
13
|
it 'return name column value when passing id to [] method' do
|
13
|
-
Person[
|
14
|
+
Person[1].should == 'Dave'
|
14
15
|
end
|
15
16
|
|
16
17
|
it 'return id column value when passing string name to [] method' do
|
17
|
-
Person['Dave'].should ==
|
18
|
-
Person['dave'].should ==
|
18
|
+
Person['Dave'].should == 1
|
19
|
+
Person['dave'].should == 1
|
19
20
|
end
|
20
21
|
|
21
22
|
it 'return id column value when passing symbol name to [] method' do
|
22
|
-
Person[:dave].should ==
|
23
|
+
Person[:dave].should == 1
|
23
24
|
end
|
24
25
|
|
25
26
|
it 'should return array for select helpers from to_select' do
|
26
|
-
Person.to_select.should == [[
|
27
|
+
Person.to_select.should == [['Dave', 1], ['John', 2]]
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return sorted array from order value for select helpers from to_select' do
|
31
|
+
Person.class_eval do
|
32
|
+
acts_as_enum :name_column => 'first_name', :order => :desc
|
33
|
+
end
|
34
|
+
Person.to_select.should == [['John', 2], ['Dave', 1]]
|
27
35
|
end
|
28
36
|
end
|
@@ -60,6 +60,15 @@ describe ActiveEnum::Base do
|
|
60
60
|
enum.all.first[0].should == 1
|
61
61
|
end
|
62
62
|
|
63
|
+
it 'should return sorted values by id using order setting from :all' do
|
64
|
+
enum = define_enum do
|
65
|
+
order :desc
|
66
|
+
value :id => 1, :name => 'Name 1'
|
67
|
+
value :id => 2, :name => 'Name 2'
|
68
|
+
end
|
69
|
+
enum.all.first[0].should == 2
|
70
|
+
end
|
71
|
+
|
63
72
|
it 'should return array of ids' do
|
64
73
|
enum = define_enum do
|
65
74
|
value :id => 1, :name => 'Name 1'
|
@@ -113,6 +122,15 @@ describe ActiveEnum::Base do
|
|
113
122
|
enum.to_select.should == [['Name 1',1], ['Name 2',2]]
|
114
123
|
end
|
115
124
|
|
125
|
+
it 'should return array sorted using order setting from to_select' do
|
126
|
+
enum = define_enum() do
|
127
|
+
order :desc
|
128
|
+
value :id => 1, :name => 'Name 1'
|
129
|
+
value :id => 2, :name => 'Name 2'
|
130
|
+
end
|
131
|
+
enum.to_select.should == [['Name 2',2], ['Name 1',1]]
|
132
|
+
end
|
133
|
+
|
116
134
|
def define_enum(&block)
|
117
135
|
Class.new(ActiveEnum::Base, &block)
|
118
136
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
@@ -9,7 +9,7 @@ autorequire: active_enum
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-24 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|