acts_as_enum 0.2.0 → 1.0.1
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 +3 -0
- data/README.rdoc +13 -1
- data/VERSION +1 -1
- data/lib/acts_as_enum.rb +30 -11
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -7,11 +7,23 @@
|
|
7
7
|
|
8
8
|
== Usage
|
9
9
|
|
10
|
+
Table column type as Varchar or Varchar2
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
13
|
+
end
|
14
|
+
|
15
|
+
Table column type as Integer
|
10
16
|
class User < ActiveRecord::Base
|
11
17
|
acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
12
18
|
end
|
13
19
|
|
20
|
+
Table column type as Varchar and Number
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
23
|
+
end
|
24
|
+
|
14
25
|
Also can usage alias enum_attr
|
26
|
+
enum_attr :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
15
27
|
enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
16
28
|
|
17
29
|
|
@@ -27,7 +39,7 @@
|
|
27
39
|
|
28
40
|
|
29
41
|
If with option prefix is true:
|
30
|
-
acts_as_enum :status, :in => [ ['disable',
|
42
|
+
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ], :prefix => true
|
31
43
|
|
32
44
|
Will generate bellow:
|
33
45
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.1
|
data/lib/acts_as_enum.rb
CHANGED
@@ -3,10 +3,22 @@
|
|
3
3
|
# acts_as_enum(attr, options)
|
4
4
|
# attr is model attribute
|
5
5
|
# options incldue :in, :prefix
|
6
|
+
# :in value as Array [ [value, label], ... ] or [ [special_method_name, value, label], ... ]
|
7
|
+
# :prefix value as true or false
|
6
8
|
#
|
9
|
+
# table column status type is Varchar or Varchar2
|
10
|
+
# class User < ActiveRecord::Base
|
11
|
+
# acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# and type is Integer or number of string
|
15
|
+
# NOTE: table column value must be match type, i.e. varchar: '1'; integer: 1
|
7
16
|
# class User < ActiveRecord::Base
|
8
17
|
# acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
9
18
|
# end
|
19
|
+
# class User < ActiveRecord::Base
|
20
|
+
# acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
21
|
+
# end
|
10
22
|
#
|
11
23
|
# Also can usage alias enum_attr
|
12
24
|
# enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
@@ -27,29 +39,36 @@
|
|
27
39
|
#
|
28
40
|
# Will generate bellow:
|
29
41
|
# User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
30
|
-
|
42
|
+
|
31
43
|
module ActsAsEnum
|
32
44
|
def self.included(base)
|
33
45
|
base.extend ClassMethods
|
34
46
|
end
|
35
47
|
|
36
48
|
module ClassMethods
|
37
|
-
def acts_as_enum(attr,
|
49
|
+
def acts_as_enum(attr, options = { :in => [], :prefix => false })
|
38
50
|
attr = attr.to_s
|
39
51
|
plural_upcase_attr = attr.pluralize.upcase
|
40
|
-
enum =
|
52
|
+
enum = options[:in]
|
41
53
|
|
42
54
|
rails "Can not load Rails" unless defined?(Rails)
|
43
|
-
rails "Options in can not be empty" if enum.blank?
|
44
|
-
rails "Options in must be an array object" unless enum.is_a?(Array)
|
55
|
+
rails "Options :in can not be empty" if enum.blank?
|
56
|
+
rails "Options :in must be an array object" unless enum.is_a?(Array)
|
45
57
|
|
58
|
+
is_key_value_enum = enum.first.size == 2 ? true : false
|
59
|
+
|
46
60
|
# validates_inclusion_of attr, :in => enum.collect { |arr| arr[1] }, :allow_blank => true
|
47
61
|
|
48
|
-
|
62
|
+
attr_options = enum.inject({}) do |hash, arr|
|
63
|
+
hash[is_key_value_enum ? arr.first : arr[1]] = arr.last.to_s
|
64
|
+
hash
|
65
|
+
end
|
66
|
+
const_set(plural_upcase_attr, attr_options)
|
49
67
|
|
50
68
|
enum.each do |arr|
|
51
|
-
enum_name
|
52
|
-
|
69
|
+
enum_name = arr.first.to_s.downcase
|
70
|
+
attr_value = is_key_value_enum ? arr.first : arr[1]
|
71
|
+
method_name = options[:prefix] ? "#{attr}_#{enum_name}" : enum_name
|
53
72
|
|
54
73
|
const_set("#{method_name}".upcase, attr_value)
|
55
74
|
|
@@ -58,10 +77,11 @@ module ActsAsEnum
|
|
58
77
|
else
|
59
78
|
scope method_name.to_sym, where(["#{attr} = ?", attr_value])
|
60
79
|
end
|
61
|
-
|
80
|
+
|
62
81
|
class_eval(%Q{
|
63
82
|
def #{method_name}?
|
64
|
-
#{attr}.to_s == #{
|
83
|
+
# #{attr}.to_s == #{method_name.upcase}
|
84
|
+
#{attr}.to_s == "#{attr_value}"
|
65
85
|
end
|
66
86
|
})
|
67
87
|
end
|
@@ -72,7 +92,6 @@ module ActsAsEnum
|
|
72
92
|
end
|
73
93
|
|
74
94
|
def #{attr}_name
|
75
|
-
# #{plural_upcase_attr}.detect { |arr| arr[1] == #{attr} }[0] unless #{attr}.blank?
|
76
95
|
#{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
|
77
96
|
end
|
78
97
|
})
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: acts_as_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- LiangWenKe
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-27 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: "\xE4\xB8\xBB\xE8\xA6\x81\xE5\xBA\x94\xE7\x94\xA8\xE4\xBA\x8E\xE6\x9C\x89\xE6\x9E\x9A\xE4\xB8\xBE\xE7\xB1\xBB\xE5\x9E\x8B\xE5\xB1\x9E\xE6\x80\xA7\xE7\x9A\x84Model\xEF\xBC\x8C\xE8\xBF\x99\xE4\xB8\xAA\xE6\x8F\x92\xE4\xBB\xB6\xE4\xBC\x9A\xE5\xB8\xAE\xE6\x88\x91\xE4\xBB\xAC\xE7\x94\x9F\xE6\x88\x90\xE4\xB8\x80\xE4\xBA\x9B\xE5\xB8\xB8\xE7\x94\xA8\xE5\x88\xB0\xE7\x9A\x84\xE6\x96\xB9\xE6\xB3\x95\xE3\x80\x82"
|