acts_as_enum 1.0.2 → 1.1.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.
Files changed (4) hide show
  1. data/CHANGELOG +6 -0
  2. data/VERSION +1 -1
  3. data/lib/acts_as_enum.rb +32 -26
  4. metadata +6 -5
data/CHANGELOG CHANGED
@@ -19,3 +19,9 @@
19
19
 
20
20
  1.0.2
21
21
  Fixed a bug for #{boolean_column_attr}_name method
22
+
23
+ 1.1.0
24
+ Option :in support Hash Object
25
+
26
+ 1.1.1
27
+ Fix a bug for scope method
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.1
data/lib/acts_as_enum.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  # EnumAttr# == Example
2
- #
2
+ #
3
3
  # acts_as_enum(attr, options)
4
4
  # attr is model attribute
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
- #
8
+ #
9
9
  # table column status type is Varchar or Varchar2
10
10
  # class User < ActiveRecord::Base
11
11
  # acts_as_enum :status, :in => [ ['disable', '冻结'], ['enable', '激活'] ]
@@ -19,24 +19,24 @@
19
19
  # class User < ActiveRecord::Base
20
20
  # acts_as_enum :status, :in => [ ['disable', '0', '冻结'], ['enable', '1', '激活'] ]
21
21
  # end
22
- #
22
+ #
23
23
  # Also can usage alias enum_attr
24
24
  # enum_attr :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
25
- #
25
+ #
26
26
  # Will generate bellow:
27
- #
27
+ #
28
28
  # Constants: User::STATUSES(return hash { 0 => "冻结", 1 => "激活" }), User::DISABLE, User::ENABLE
29
- #
29
+ #
30
30
  # Named scopes: User.enable, User.disable
31
- #
32
- # Class methods: User.status_options => [["冻结", 0], ["激活", 1]]
33
- #
31
+ #
32
+ # Class methods: User.status_options => [["冻结", 0], ["激活", 1]]
33
+ #
34
34
  # Instance methods: user.status_name, user.enable?, user.disable?
35
- #
36
- #
37
- # If with option prefix is true:
35
+ #
36
+ #
37
+ # If with option prefix is true:
38
38
  # acts_as_enum :status, :in => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ], :prefix => true
39
- #
39
+ #
40
40
  # Will generate bellow:
41
41
  # User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
42
42
 
@@ -44,17 +44,23 @@ module ActsAsEnum
44
44
  def self.included(base)
45
45
  base.extend ClassMethods
46
46
  end
47
-
47
+
48
48
  module ClassMethods
49
49
  def acts_as_enum(attr, options = { :in => [], :prefix => false })
50
50
  attr = attr.to_s
51
51
  plural_upcase_attr = attr.pluralize.upcase
52
52
  enum = options[:in]
53
-
54
- rails "Can not load Rails" unless defined?(Rails)
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)
57
-
53
+
54
+ rails "Can not load Rails." unless defined?(Rails)
55
+ rails "Options :in can not be empty." if enum.blank?
56
+ rails "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
+
58
64
  is_key_value_enum = enum.first.size == 2 ? true : false
59
65
 
60
66
  # validates_inclusion_of attr, :in => enum.collect { |arr| arr[1] }, :allow_blank => true
@@ -64,18 +70,18 @@ module ActsAsEnum
64
70
  hash
65
71
  end
66
72
  const_set(plural_upcase_attr, attr_options)
67
-
73
+
68
74
  enum.each do |arr|
69
75
  enum_name = arr.first.to_s.downcase
70
76
  attr_value = is_key_value_enum ? arr.first : arr[1]
71
77
  method_name = options[:prefix] ? "#{attr}_#{enum_name}" : enum_name
72
-
78
+
73
79
  const_set("#{method_name}".upcase, attr_value)
74
-
80
+
75
81
  if Rails.version =~ /^2/
76
82
  named_scope method_name.to_sym, :conditions => { attr.to_sym => attr_value }
77
83
  else
78
- scope method_name.to_sym, where(["#{attr} = ?", attr_value])
84
+ scope method_name.to_sym, where(["#{self.table_name}.#{attr} = ?", attr_value])
79
85
  end
80
86
 
81
87
  class_eval(%Q{
@@ -85,19 +91,19 @@ module ActsAsEnum
85
91
  end
86
92
  })
87
93
  end
88
-
94
+
89
95
  class_eval(%Q{
90
96
  def self.#{attr}_options
91
97
  #{plural_upcase_attr}.inject([]){ |arr, obj| arr << obj.reverse }
92
98
  end
93
-
99
+
94
100
  def #{attr}_name
95
101
  return #{plural_upcase_attr}[#{attr}] if #{attr}.is_a?(FalseClass)
96
102
  #{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
97
103
  end
98
104
  })
99
105
  end
100
-
106
+
101
107
  alias enum_attr acts_as_enum
102
108
  end
103
109
  end
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - LiangWenKe
8
+ - Mike Liang
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-02 00:00:00.000000000Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: 主要应用于有枚举类型属性的Model,这个插件会帮我们生成一些常用到的方法。
14
+ description: For multiple values activerecord attributes. This gem have some very
15
+ useful methods and constants for attribute.
15
16
  email: liangwenke.com@gmail.com
16
17
  executables: []
17
18
  extensions: []
@@ -46,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
47
  version: 1.3.4
47
48
  requirements: []
48
49
  rubyforge_project: acts_as_enum
49
- rubygems_version: 1.8.10
50
+ rubygems_version: 1.8.24
50
51
  signing_key:
51
52
  specification_version: 3
52
53
  summary: Enum Attribute for Rails ActiveRecord