const_enum 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,10 +5,10 @@ gem 'activerecord', '>= 3.0.0'
5
5
  # Add dependencies to develop your gem here.
6
6
  # Include everything needed to run rake, tests, features, etc.
7
7
  group :development do
8
- gem 'rspec', '~> 2.8.0'
9
- gem 'rdoc', '~> 3.12'
10
- gem 'bundler', '~> 1.0.0'
11
- gem 'jeweler', '~> 1.8.3'
8
+ gem 'rspec', '>= 2.8.0'
9
+ gem 'rdoc', '>= 3.12'
10
+ gem 'bundler', '>= 1.0.0'
11
+ gem 'jeweler', '>= 1.8.3'
12
12
  gem 'simplecov'
13
13
  gem 'activerecord', '>= 3.0.0'
14
14
  gem 'sqlite3-ruby', :require => 'sqlite3'
@@ -48,7 +48,7 @@ Gemfileに以下行を追加して、bundle installコマンドを実行して
48
48
  taro.status_enable? # true
49
49
  taro.status_disable? # false
50
50
 
51
- # {ATTR}_was_{CONST}? , #{ATTR}_{CONST}_wasメソッドが定義される
51
+ # was_{ATTR}_{CONST}? , #{ATTR}_{CONST}_wasメソッドが定義される
52
52
  taro.status = User::STATUS::DISABLE
53
53
  taro.status_enable? # false
54
54
  taro.was_status_enable? # true
@@ -75,8 +75,8 @@ validates :inclusionで利用することができる。
75
75
  constメソッドで定義される定数クラスはEnumerableモジュールをインクルードしているため、
76
76
  collection_selectにそのまま渡すことができる。
77
77
  値はvalue、ラベル名labelメソッドを指定する。
78
-
79
- form_for newhoge do |f|
78
+ user = User.new
79
+ form_for user do |f|
80
80
  f.collection_select(:status, User::STATUS, :value, :label)
81
81
  end
82
82
 
@@ -92,7 +92,7 @@ collection_selectにそのまま渡すことができる。
92
92
 
93
93
  === prefix: NamedScope、属性値テストメソッドの名前を変更する
94
94
 
95
- constメソッドにprefixオプションを与えることで、作成されるNamedScope、属性値検証メソッドの
95
+ constメソッドにprefixオプションを指定することで、作成されるNamedScope、属性値検証メソッドの
96
96
  名前(プレフィクス)を変更することができる。
97
97
  定数値名がモデル内で衝突しない場合は、prefixに空文字を与えるのが使いやすい。
98
98
 
@@ -110,7 +110,7 @@ constメソッドにprefixオプションを与えることで、作成される
110
110
 
111
111
  === scope: NamedScopeを作成しない
112
112
 
113
- 値が直接DBにない場合、scopeオプションにfalseを与え、
113
+ 値が直接DBにない場合、scopeオプションでfalseを指定すると、
114
114
  定数値を返すメソッドを定義することで各種メソッドを利用できる。
115
115
 
116
116
  class Entry < ActiveRecord::Base
@@ -159,9 +159,20 @@ constメソッドにprefixオプションを与えることで、作成される
159
159
 
160
160
  === predicate: 属性値テストメソッドを作成しない
161
161
 
162
- predicatefalseを与えると、属性値テストメソッドの作成が行われない。
162
+ predicateオプションでfalseを指定すると、属性値テストメソッドの作成が行われない。
163
+
164
+ === i18n: ラベル名にi18nを利用しない
163
165
 
166
+ i18nオプションでfalseを指定すると、constメソッド内の定数値定義の後にラベル名を与えることでラベル名を定義することができる。
164
167
 
168
+ # app/models/user.rb
169
+ class User < ActiveRecord::Base
170
+ const :STATUS, :i18n => false do
171
+ ENABLE 1, "有効"
172
+ DISABLE 0, "無効"
173
+ end
174
+ end
175
+
165
176
  === extensions: 定数オブジェクトに追加のメソッドを定義する
166
177
 
167
178
  ブロック内にメソッド定義を書くと、STAUTS[]やSTATUS.eachの際に取得できる定数オブジェクトに対してメソッドが定義される。
@@ -186,20 +197,37 @@ collection_selectの際に表示したいラベル名を複数パターン用意
186
197
  end
187
198
 
188
199
  # 選択肢に上で定義したcodeメソッドの値を使用する
189
- form_for enable_piyo do |f|
200
+ user = User.new
201
+ form_for user do |f|
190
202
  f.collection_select(:status, User::STATUS, :value, :code)
191
203
  f.collection_select(:status, User::STATUS, :value, :label2)
192
204
  end
193
205
 
194
206
  == その他
195
207
 
208
+ === I18nを利用しない
209
+
210
+ ConstEnum::ActiveRecord.i18nにfalseを設定すると、
211
+ constメソッド内でラベル名を定義することができる。
212
+ constメソッドのi18nオプションと同様
213
+
214
+ # config/initializers/const_enum.rb
215
+ ConstEnum::ActiveRecord.i18n = false
216
+
217
+ # app/models/user.rb
218
+ class User < ActiveRecord::Base
219
+ const :STATUS, :prefix => '' do
220
+ ENABLE 1, "有効"
221
+ DISABLE 0, "無効"
222
+ end
223
+ end
224
+
196
225
  === ラベル名を取得するメソッド名を変更する
197
226
 
198
- ConstEnum::ActiveRecord.label_suffixオプションを設定することで、
227
+ ConstEnum::ActiveRecord.label_suffixを設定することで、
199
228
  ラベル名を取得すためのメソッドの名前(サフィックス)を変更することができる。
200
229
 
201
230
  # config/initializers/const_enum.rb
202
- require 'const_enum'
203
231
  ConstEnum::ActiveRecord.label_suffix = '_name' # ラベル名を返すメソッドにつけるサフィックスの指定
204
232
 
205
233
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "const_enum"
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["yuki teraoka"]
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/const_enum/with_i18n.rb",
33
33
  "spec/active_record/active_record_spec.rb",
34
34
  "spec/const_enum_spec.rb",
35
+ "spec/labels.yml",
35
36
  "spec/spec_helper.rb",
36
37
  "spec/support/database_cleaner.rb",
37
38
  "spec/support/models.rb"
@@ -47,20 +48,20 @@ Gem::Specification.new do |s|
47
48
 
48
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
50
  s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
50
- s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
51
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
52
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
51
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
52
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
53
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
54
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
54
55
  s.add_development_dependency(%q<simplecov>, [">= 0"])
55
56
  s.add_development_dependency(%q<activerecord>, [">= 3.0.0"])
56
57
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
57
58
  s.add_development_dependency(%q<database_cleaner>, [">= 0"])
58
59
  else
59
60
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
60
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
61
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
62
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
61
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
62
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
63
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
64
65
  s.add_dependency(%q<simplecov>, [">= 0"])
65
66
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
66
67
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -68,10 +69,10 @@ Gem::Specification.new do |s|
68
69
  end
69
70
  else
70
71
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
71
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
72
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
73
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
74
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
72
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
73
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
74
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
75
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
75
76
  s.add_dependency(%q<simplecov>, [">= 0"])
76
77
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
77
78
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ require "active_support/core_ext"
2
3
  require "active_support/core_ext/module"
3
4
  require "active_support/core_ext/array"
4
5
 
@@ -11,15 +11,13 @@ module ConstEnum
11
11
  self.i18n_options = DEFAULT_OPTIONS.dup
12
12
  end
13
13
 
14
- module InstanceMethods
15
- def label
16
- I18n.t(build_key(@attributes.second || key.to_s.downcase))
17
- end
18
-
19
- private
20
- def build_key(key)
21
- "#{self.class.i18n_options[:key]}.#{key}"
22
- end
14
+ def label
15
+ I18n.t(build_key(@attributes.second || key.to_s.downcase))
16
+ end
17
+
18
+ private
19
+ def build_key(key)
20
+ "#{self.class.i18n_options[:key]}.#{key}"
23
21
  end
24
22
 
25
23
  module ClassMethods
@@ -0,0 +1,6 @@
1
+ en:
2
+ labels:
3
+ user:
4
+ status:
5
+ active: Active
6
+ resigned: Resigned
@@ -33,8 +33,7 @@ class CreateAllTables < ActiveRecord::Migration
33
33
  end
34
34
  end
35
35
 
36
- I18n.backend = I18n::Backend::KeyValue.new({})
37
- I18n.backend.store_translations :en, :labels =>{:user =>{:status => { :active => "Active", :resigned => "Resigned" }}}
36
+ I18n.load_path << File.join(File.dirname(__FILE__), '..', 'labels.yml')
38
37
 
39
38
  ActiveRecord::Migration.verbose = false
40
39
  CreateAllTables.up
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: const_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &22300760 !ruby/object:Gem::Requirement
16
+ requirement: &18530540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,54 +21,54 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *22300760
24
+ version_requirements: *18530540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &22299900 !ruby/object:Gem::Requirement
27
+ requirement: &18526180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
- - - ~>
30
+ - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: 2.8.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *22299900
35
+ version_requirements: *18526180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rdoc
38
- requirement: &22298640 !ruby/object:Gem::Requirement
38
+ requirement: &18524280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ~>
41
+ - - ! '>='
42
42
  - !ruby/object:Gem::Version
43
43
  version: '3.12'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *22298640
46
+ version_requirements: *18524280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &22297820 !ruby/object:Gem::Requirement
49
+ requirement: &18548740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
- - - ~>
52
+ - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *22297820
57
+ version_requirements: *18548740
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &22296680 !ruby/object:Gem::Requirement
60
+ requirement: &18545880 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
- - - ~>
63
+ - - ! '>='
64
64
  - !ruby/object:Gem::Version
65
65
  version: 1.8.3
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *22296680
68
+ version_requirements: *18545880
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &22187480 !ruby/object:Gem::Requirement
71
+ requirement: &18544100 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *22187480
79
+ version_requirements: *18544100
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activerecord
82
- requirement: &22186400 !ruby/object:Gem::Requirement
82
+ requirement: &18542940 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 3.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *22186400
90
+ version_requirements: *18542940
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: sqlite3-ruby
93
- requirement: &22185500 !ruby/object:Gem::Requirement
93
+ requirement: &18541260 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *22185500
101
+ version_requirements: *18541260
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: database_cleaner
104
- requirement: &22184380 !ruby/object:Gem::Requirement
104
+ requirement: &18565700 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *22184380
112
+ version_requirements: *18565700
113
113
  description: define ActiveRecord constants with DSL.and more!
114
114
  email: info@techscore.com
115
115
  executables: []
@@ -133,6 +133,7 @@ files:
133
133
  - lib/const_enum/with_i18n.rb
134
134
  - spec/active_record/active_record_spec.rb
135
135
  - spec/const_enum_spec.rb
136
+ - spec/labels.yml
136
137
  - spec/spec_helper.rb
137
138
  - spec/support/database_cleaner.rb
138
139
  - spec/support/models.rb
@@ -151,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
152
  version: '0'
152
153
  segments:
153
154
  - 0
154
- hash: -4219554421679490713
155
+ hash: 2667874335032134180
155
156
  required_rubygems_version: !ruby/object:Gem::Requirement
156
157
  none: false
157
158
  requirements: