acts_as_enum 1.1.4 → 1.2.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/MIT-LICENSE +20 -20
- data/README.rdoc +79 -79
- data/Rakefile +23 -23
- data/TODO +4 -4
- data/VERSION +1 -1
- data/init.rb +1 -1
- data/lib/acts_as_enum.rb +114 -113
- data/lib/tasks/acts_as_enum_tasks.rake +4 -4
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 774afd9f15ea2a3e7a335f25ee07212c66bba092
|
4
|
+
data.tar.gz: c811e0a69f15cd3d4347acf4a79465dd682d1357
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a2404ad07b4479119bdab0f8693c06a03b0156b726f59c578dd1b99e9125b01d5d24bfff7d9367d4f5d6f4cd4c2a8da383a625fea2c0a2ffe60b9f29589b5d9
|
7
|
+
data.tar.gz: 017a4e0ed511de815a8065f320c64a0b0af342b61158abeb98ff4e1aa117f1adc6d4b714eb15a6f533bb9f7b5684d75003d2f5abdf98db4f3dd8bee8433c0bab
|
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2010 [name of plugin creator]
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
== ActsAsEnum
|
2
|
-
|
3
|
-
主要应用于有枚举类型属性的Model,这个插件会帮我们生成一些常用到的方法。
|
4
|
-
|
5
|
-
如:枚举数组常量,每个元素的常量和布尔方法,属性的名字,Named scope方法
|
6
|
-
|
7
|
-
|
8
|
-
== Getting Started
|
9
|
-
|
10
|
-
In your Gemfile:
|
11
|
-
|
12
|
-
gem 'acts_as_enum'# Last officially released gem
|
13
|
-
# gem "acts_as_enum", :git => "git://github.com/liangwenke/acts_as_enum.git" # Track git repo
|
14
|
-
|
15
|
-
or, to install as a gem:
|
16
|
-
|
17
|
-
gem install acts_as_enum
|
18
|
-
|
19
|
-
or, to install as a plugin:
|
20
|
-
|
21
|
-
rails plugin install git://github.com/liangwenke/acts_as_enum.git
|
22
|
-
|
23
|
-
|
24
|
-
== Usage
|
25
|
-
|
26
|
-
Table column type as String
|
27
|
-
|
28
|
-
class User < ActiveRecord::Base
|
29
|
-
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
30
|
-
end
|
31
|
-
|
32
|
-
Table column type as Integer
|
33
|
-
|
34
|
-
class User < ActiveRecord::Base
|
35
|
-
acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
36
|
-
end
|
37
|
-
|
38
|
-
Table column type as String and Number
|
39
|
-
|
40
|
-
class User < ActiveRecord::Base
|
41
|
-
acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
42
|
-
end
|
43
|
-
|
44
|
-
Table column type as Boolean
|
45
|
-
|
46
|
-
class User < ActiveRecord::Base
|
47
|
-
acts_as_enum :status, :in => [ ['disable', false, '冻结'], ['enable', true, '激活'] ]
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
Also can usage alias enum_attr
|
52
|
-
|
53
|
-
enum_attr :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
54
|
-
enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
55
|
-
|
56
|
-
|
57
|
-
Will generate bellow:
|
58
|
-
|
59
|
-
Constants: User::STATUSES, User::DISABLE, User::ENABLE
|
60
|
-
|
61
|
-
Named scopes: User.enable, User.disable
|
62
|
-
|
63
|
-
Class methods: User.status_options
|
64
|
-
|
65
|
-
Instance methods: user.status_name, user.enable?, user.disable
|
66
|
-
|
67
|
-
|
68
|
-
If with option prefix is true:
|
69
|
-
|
70
|
-
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ], :prefix => true
|
71
|
-
|
72
|
-
Will generate bellow:
|
73
|
-
|
74
|
-
User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
75
|
-
|
76
|
-
|
77
|
-
== Note
|
78
|
-
|
79
|
-
Copyright (c) 2010 liangwenke.com@gmail.com, released under the MIT license
|
1
|
+
== ActsAsEnum
|
2
|
+
|
3
|
+
主要应用于有枚举类型属性的Model,这个插件会帮我们生成一些常用到的方法。
|
4
|
+
|
5
|
+
如:枚举数组常量,每个元素的常量和布尔方法,属性的名字,Named scope方法
|
6
|
+
|
7
|
+
|
8
|
+
== Getting Started
|
9
|
+
|
10
|
+
In your Gemfile:
|
11
|
+
|
12
|
+
gem 'acts_as_enum'# Last officially released gem
|
13
|
+
# gem "acts_as_enum", :git => "git://github.com/liangwenke/acts_as_enum.git" # Track git repo
|
14
|
+
|
15
|
+
or, to install as a gem:
|
16
|
+
|
17
|
+
gem install acts_as_enum
|
18
|
+
|
19
|
+
or, to install as a plugin:
|
20
|
+
|
21
|
+
rails plugin install git://github.com/liangwenke/acts_as_enum.git
|
22
|
+
|
23
|
+
|
24
|
+
== Usage
|
25
|
+
|
26
|
+
Table column type as String
|
27
|
+
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
30
|
+
end
|
31
|
+
|
32
|
+
Table column type as Integer
|
33
|
+
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
36
|
+
end
|
37
|
+
|
38
|
+
Table column type as String and Number
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
42
|
+
end
|
43
|
+
|
44
|
+
Table column type as Boolean
|
45
|
+
|
46
|
+
class User < ActiveRecord::Base
|
47
|
+
acts_as_enum :status, :in => [ ['disable', false, '冻结'], ['enable', true, '激活'] ]
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
Also can usage alias enum_attr
|
52
|
+
|
53
|
+
enum_attr :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
|
54
|
+
enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
55
|
+
|
56
|
+
|
57
|
+
Will generate bellow:
|
58
|
+
|
59
|
+
Constants: User::STATUSES, User::DISABLE, User::ENABLE
|
60
|
+
|
61
|
+
Named scopes: User.enable, User.disable
|
62
|
+
|
63
|
+
Class methods: User.status_options
|
64
|
+
|
65
|
+
Instance methods: user.status_name, user.enable?, user.disable?, user.enable!, user.disable!
|
66
|
+
|
67
|
+
|
68
|
+
If with option prefix is true:
|
69
|
+
|
70
|
+
acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ], :prefix => true
|
71
|
+
|
72
|
+
Will generate bellow:
|
73
|
+
|
74
|
+
User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
75
|
+
|
76
|
+
|
77
|
+
== Note
|
78
|
+
|
79
|
+
Copyright (c) 2010 liangwenke.com@gmail.com, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
|
5
|
-
desc 'Default: run unit tests.'
|
6
|
-
task :default => :test
|
7
|
-
|
8
|
-
desc 'Test the acts_as_enum plugin.'
|
9
|
-
Rake::TestTask.new(:test) do |t|
|
10
|
-
t.libs << 'lib'
|
11
|
-
t.libs << 'test'
|
12
|
-
t.pattern = 'test/**/*_test.rb'
|
13
|
-
t.verbose = true
|
14
|
-
end
|
15
|
-
|
16
|
-
desc 'Generate documentation for the acts_as_enum plugin.'
|
17
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
-
rdoc.rdoc_dir = 'rdoc'
|
19
|
-
rdoc.title = 'ActsAsEnum'
|
20
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
-
rdoc.rdoc_files.include('README.rdoc')
|
22
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
-
end
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the acts_as_enum plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the acts_as_enum plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'ActsAsEnum'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/TODO
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
会继续添加如下功能
|
2
|
-
|
3
|
-
1) 将会支持in里面的数组元素是符号类型
|
4
|
-
|
1
|
+
会继续添加如下功能
|
2
|
+
|
3
|
+
1) 将会支持in里面的数组元素是符号类型
|
4
|
+
|
5
5
|
2) 将会支参数in是Hash类型
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'acts_as_enum'
|
1
|
+
require 'acts_as_enum'
|
data/lib/acts_as_enum.rb
CHANGED
@@ -1,113 +1,114 @@
|
|
1
|
-
# EnumAttr# == Example
|
2
|
-
#
|
3
|
-
# acts_as_enum(attr, options)
|
4
|
-
# attr is model attribute
|
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
|
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
|
16
|
-
# class User < ActiveRecord::Base
|
17
|
-
# acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
18
|
-
# end
|
19
|
-
# class User < ActiveRecord::Base
|
20
|
-
# acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# Also can usage alias enum_attr
|
24
|
-
# enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
25
|
-
#
|
26
|
-
# Will generate bellow:
|
27
|
-
#
|
28
|
-
# Constants: User::STATUSES(return hash { 0 => "冻结", 1 => "激活" }), User::DISABLE, User::ENABLE
|
29
|
-
#
|
30
|
-
# Named scopes: User.enable, User.disable
|
31
|
-
#
|
32
|
-
# Class methods: User.status_options => [["冻结", 0], ["激活", 1]]
|
33
|
-
#
|
34
|
-
# Instance methods: user.status_name, user.enable?, user.disable
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# If with option prefix is true:
|
38
|
-
# acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ], :prefix => true
|
39
|
-
#
|
40
|
-
# Will generate bellow:
|
41
|
-
# User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
42
|
-
|
43
|
-
module ActsAsEnum
|
44
|
-
def self.included(base)
|
45
|
-
base.extend ClassMethods
|
46
|
-
end
|
47
|
-
|
48
|
-
module ClassMethods
|
49
|
-
def acts_as_enum(attr, options = { :in => [], :prefix => false })
|
50
|
-
attr = attr.to_s
|
51
|
-
plural_upcase_attr = attr.pluralize.upcase
|
52
|
-
enum = options[:in]
|
53
|
-
|
54
|
-
raise "Can not load Rails." unless defined?(Rails)
|
55
|
-
raise "Options :in can not be empty." if enum.blank?
|
56
|
-
raise "Options :in must be an object of Array or Hash." unless enum.is_a?(Array) or enum.is_a?(Hash)
|
57
|
-
|
58
|
-
if enum.is_a?(Hash)
|
59
|
-
enum = enum.to_a
|
60
|
-
elsif enum.is_a?(Array) and enum.first.is_a?(String)
|
61
|
-
enum = enum.inject([]) { |arr, obj| arr << [obj] * 2 }
|
62
|
-
end
|
63
|
-
|
64
|
-
is_key_value_enum = enum.first.size == 2 ? true : false
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
scope method_name.to_sym,
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
#{plural_upcase_attr}[#{attr}]
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
|
1
|
+
# EnumAttr# == Example
|
2
|
+
#
|
3
|
+
# acts_as_enum(attr, options)
|
4
|
+
# attr is model attribute
|
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
|
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
|
16
|
+
# class User < ActiveRecord::Base
|
17
|
+
# acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
18
|
+
# end
|
19
|
+
# class User < ActiveRecord::Base
|
20
|
+
# acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# Also can usage alias enum_attr
|
24
|
+
# enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
25
|
+
#
|
26
|
+
# Will generate bellow:
|
27
|
+
#
|
28
|
+
# Constants: User::STATUSES(return hash { 0 => "冻结", 1 => "激活" }), User::DISABLE, User::ENABLE
|
29
|
+
#
|
30
|
+
# Named scopes: User.enable, User.disable
|
31
|
+
#
|
32
|
+
# Class methods: User.status_options => [["冻结", 0], ["激活", 1]]
|
33
|
+
#
|
34
|
+
# Instance methods: user.status_name, user.enable?, user.disable?, user.enable!(update user.status to 1) and user.disable!(update user.status to 1)
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# If with option prefix is true:
|
38
|
+
# acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ], :prefix => true
|
39
|
+
#
|
40
|
+
# Will generate bellow:
|
41
|
+
# User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
42
|
+
|
43
|
+
module ActsAsEnum
|
44
|
+
def self.included(base)
|
45
|
+
base.extend ClassMethods
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def acts_as_enum(attr, options = { :in => [], :prefix => false })
|
50
|
+
attr = attr.to_s
|
51
|
+
plural_upcase_attr = attr.pluralize.upcase
|
52
|
+
enum = options[:in]
|
53
|
+
|
54
|
+
raise "Can not load Rails." unless defined?(Rails)
|
55
|
+
raise "Options :in can not be empty." if enum.blank?
|
56
|
+
raise "Options :in must be an object of Array or Hash." unless enum.is_a?(Array) or enum.is_a?(Hash)
|
57
|
+
|
58
|
+
if enum.is_a?(Hash)
|
59
|
+
enum = enum.to_a
|
60
|
+
elsif enum.is_a?(Array) and enum.first.is_a?(String)
|
61
|
+
enum = enum.inject([]) { |arr, obj| arr << [obj] * 2 }
|
62
|
+
end
|
63
|
+
|
64
|
+
is_key_value_enum = enum.first.size == 2 ? true : false
|
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
|
69
|
+
end
|
70
|
+
const_set(plural_upcase_attr, attr_options)
|
71
|
+
|
72
|
+
enum.each do |arr|
|
73
|
+
enum_name = arr.first.to_s.downcase
|
74
|
+
attr_value = is_key_value_enum ? arr.first : arr[1]
|
75
|
+
method_name = options[:prefix] ? "#{attr}_#{enum_name}" : enum_name
|
76
|
+
|
77
|
+
const_set("#{method_name}".upcase, attr_value)
|
78
|
+
|
79
|
+
if Rails.version =~ /^4/
|
80
|
+
scope method_name.to_sym, -> { where(["#{self.table_name}.#{attr} = ?", attr_value]) }
|
81
|
+
elsif Rails.version =~ /^3/
|
82
|
+
scope method_name.to_sym, where(["#{self.table_name}.#{attr} = ?", attr_value])
|
83
|
+
else
|
84
|
+
named_scope method_name.to_sym, :conditions => { attr.to_sym => attr_value }
|
85
|
+
end
|
86
|
+
|
87
|
+
class_eval do
|
88
|
+
define_method "#{method_name}?" do
|
89
|
+
self[attr] == attr_value
|
90
|
+
end
|
91
|
+
|
92
|
+
define_method "#{method_name}!" do
|
93
|
+
update_attribute(attr, attr_value) # use update_attribute method to skip validations
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class_eval(%Q{
|
99
|
+
def self.#{attr}_options
|
100
|
+
#{plural_upcase_attr}.inject([]){ |arr, obj| arr << obj.reverse }
|
101
|
+
end
|
102
|
+
|
103
|
+
def #{attr}_name
|
104
|
+
return #{plural_upcase_attr}[#{attr}] if #{attr}.is_a?(FalseClass)
|
105
|
+
#{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
|
106
|
+
end
|
107
|
+
})
|
108
|
+
end
|
109
|
+
|
110
|
+
alias enum_attr acts_as_enum
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
ActiveRecord::Base.send(:include, ActsAsEnum)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# desc "Explaining what the task does"
|
2
|
-
# task :acts_as_enum do
|
3
|
-
# # Task goes here
|
4
|
-
# end
|
1
|
+
# desc "Explaining what the task does"
|
2
|
+
# task :acts_as_enum do
|
3
|
+
# # Task goes here
|
4
|
+
# 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.2.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: 2014-08-11 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.
|
@@ -17,14 +17,14 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- lib/acts_as_enum.rb
|
21
|
-
- lib/tasks/acts_as_enum_tasks.rake
|
22
20
|
- MIT-LICENSE
|
23
|
-
- Rakefile
|
24
21
|
- README.rdoc
|
22
|
+
- Rakefile
|
25
23
|
- TODO
|
26
24
|
- VERSION
|
27
25
|
- init.rb
|
26
|
+
- lib/acts_as_enum.rb
|
27
|
+
- lib/tasks/acts_as_enum_tasks.rake
|
28
28
|
homepage: http://github.com/liangwenke/acts_as_enum
|
29
29
|
licenses:
|
30
30
|
- MIT
|
@@ -35,17 +35,17 @@ require_paths:
|
|
35
35
|
- lib
|
36
36
|
required_ruby_version: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 1.3.4
|
46
46
|
requirements: []
|
47
47
|
rubyforge_project: acts_as_enum
|
48
|
-
rubygems_version: 2.
|
48
|
+
rubygems_version: 2.2.2
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
51
|
summary: Enum Attribute for Rails ActiveRecord
|