acts_as_enum 1.2.1 → 1.3.0
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.
- checksums.yaml +4 -4
- data/README.rdoc +16 -0
- data/TODO +1 -3
- data/VERSION +1 -1
- data/lib/acts_as_enum.rb +13 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00e1befcc950cf4fc093bea43536a6c6c5f9e324
|
4
|
+
data.tar.gz: 339904c036a720d6195bf3dfeced23ce427e4ea9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3273e040dcb1806f533a0876490f2fd50bcb87248d4c08b1834168426c567098c56dbb9470f3cd243ad7233ed56267e0d5a4f35c55c2064e3eb18ddf3418d4b
|
7
|
+
data.tar.gz: 65df571ec89213facf734046207e55f79b6b4155f3d0cfc68541531cd7b1eeb6c3b566c6433b7555da9646553657c4b8597b60879504e6729c3b97f991c78a22
|
data/README.rdoc
CHANGED
@@ -23,12 +23,28 @@ or, to install as a plugin:
|
|
23
23
|
|
24
24
|
== Usage
|
25
25
|
|
26
|
+
Default
|
27
|
+
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
enum_attr :status, :in => %w(disable, enable)
|
30
|
+
end
|
31
|
+
|
26
32
|
Table column type as String
|
27
33
|
|
28
34
|
class User < ActiveRecord::Base
|
29
35
|
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
30
36
|
end
|
31
37
|
|
38
|
+
Or
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
acts_as_enum :status, :in => { 'disable' => '冻结', { 'enable', '激活' }
|
42
|
+
end
|
43
|
+
|
44
|
+
class User < ActiveRecord::Base
|
45
|
+
enum_attr :status, :in => { 'disable' => '冻结', { 'enable', '激活' }
|
46
|
+
end
|
47
|
+
|
32
48
|
Table column type as Integer
|
33
49
|
|
34
50
|
class User < ActiveRecord::Base
|
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
data/lib/acts_as_enum.rb
CHANGED
@@ -5,11 +5,18 @@
|
|
5
5
|
# options incldue :in, :prefix
|
6
6
|
# :in value as Array [ [value, label], ... ] or [ [special_method_name, value, label], ... ]
|
7
7
|
# :prefix value as true or false
|
8
|
+
#
|
9
|
+
# class User < ActiveRecord::Base
|
10
|
+
# acts_as_enum :status, :in => %w(disable, enable)
|
11
|
+
# end
|
8
12
|
#
|
9
13
|
# table column status type is Varchar or Varchar2
|
10
14
|
# class User < ActiveRecord::Base
|
11
15
|
# acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
12
16
|
# end
|
17
|
+
# class User < ActiveRecord::Base
|
18
|
+
# acts_as_enum :status, :in => { 'disable' => '冻结', { 'enable', '激活' }
|
19
|
+
# end
|
13
20
|
#
|
14
21
|
# and type is Integer or number of string
|
15
22
|
# NOTE: table column value must be match type, i.e. varchar: '1'; integer: 1
|
@@ -61,25 +68,20 @@ module ActsAsEnum
|
|
61
68
|
enum = enum.inject([]) { |arr, obj| arr << [obj] * 2 }
|
62
69
|
end
|
63
70
|
|
64
|
-
|
65
|
-
|
66
|
-
attr_options = enum.inject({}) do |hash, arr|
|
67
|
-
hash[is_key_value_enum ? arr.first : arr[1]] = arr.last.to_s
|
68
|
-
hash
|
71
|
+
attr_options = enum.each_with_object({}) do |arr, hash|
|
72
|
+
hash[arr[-2]] = arr.last.to_s
|
69
73
|
end
|
70
74
|
const_set(plural_upcase_attr, attr_options)
|
71
75
|
|
72
76
|
enum.each do |arr|
|
73
77
|
enum_name = arr.first.to_s.downcase
|
74
|
-
attr_value =
|
78
|
+
attr_value = arr[-2]
|
75
79
|
method_name = options[:prefix] ? "#{attr}_#{enum_name}" : enum_name
|
76
80
|
|
77
|
-
const_set(
|
81
|
+
const_set(method_name.upcase, attr_value)
|
78
82
|
|
79
|
-
if Rails.version =~ /^
|
80
|
-
scope method_name.to_sym, -> { where(
|
81
|
-
elsif Rails.version =~ /^3/
|
82
|
-
scope method_name.to_sym, where(["#{self.table_name}.#{attr} = ?", attr_value])
|
83
|
+
if Rails.version =~ /^[34]/
|
84
|
+
scope method_name.to_sym, -> { where(attr => attr_value) }
|
83
85
|
else
|
84
86
|
named_scope method_name.to_sym, :conditions => { attr.to_sym => attr_value }
|
85
87
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Liang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: For multiple values activerecord attributes. This gem have some very
|
14
14
|
useful methods and constants for attribute.
|
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
45
|
version: 1.3.4
|
46
46
|
requirements: []
|
47
47
|
rubyforge_project: acts_as_enum
|
48
|
-
rubygems_version: 2.
|
48
|
+
rubygems_version: 2.4.7
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
51
|
summary: Enum Attribute for Rails ActiveRecord
|