active_enum 0.9.7 → 0.9.8
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/README.rdoc +53 -4
- data/lib/active_enum.rb +2 -2
- data/lib/active_enum/base.rb +5 -1
- data/lib/active_enum/extensions.rb +9 -8
- data/lib/active_enum/form_helpers/simple_form.rb +13 -1
- data/lib/active_enum/railtie.rb +1 -1
- data/lib/active_enum/storage/abstract_store.rb +8 -0
- data/lib/active_enum/storage/i18n_store.rb +36 -0
- data/lib/active_enum/storage/memory_store.rb +11 -11
- data/lib/active_enum/version.rb +1 -1
- data/lib/generators/active_enum/install_generator.rb +1 -2
- data/lib/generators/active_enum/locale_generator.rb +21 -0
- data/lib/generators/active_enum/templates/{active_enum.rb → config.rb} +1 -1
- data/lib/generators/active_enum/templates/locale.yml +31 -0
- data/spec/active_enum/base_spec.rb +33 -15
- data/spec/active_enum/storage/i18n_store_spec.rb +184 -0
- data/spec/active_enum/storage/memory_store_spec.rb +3 -3
- data/spec/support/i18n.yml +10 -0
- metadata +12 -5
data/README.rdoc
CHANGED
@@ -119,7 +119,7 @@ Access the enum values and the enum class using the attribute method with a symb
|
|
119
119
|
user.sex(:id) # => 1
|
120
120
|
user.sex(:name) # => 'Male'
|
121
121
|
user.sex(:enum) # => Sex
|
122
|
-
user.sex(:symbol) # =>
|
122
|
+
user.sex(:symbol) # => ♂ ( Can use any meta data key )
|
123
123
|
|
124
124
|
You can set the default to return the enum name value for enumerated attribute
|
125
125
|
|
@@ -195,8 +195,57 @@ Or, SimpleForm:
|
|
195
195
|
The input type will be automatically detected for enumerated attributes. You can override with the :as option as normal.
|
196
196
|
|
197
197
|
|
198
|
-
==
|
199
|
-
|
200
|
-
|
198
|
+
== Storage Backends
|
199
|
+
|
200
|
+
The design allows pluggable backends to be used for stories and retrieving the enum values. At present there are two
|
201
|
+
available, memory or I18n. To change the storage engine you alter the storage config value like so:
|
202
|
+
|
203
|
+
ActiveEnum.setup do |config|
|
204
|
+
config.storage = :i18n
|
205
|
+
end
|
206
|
+
|
207
|
+
The memory store is default obviously just stores the values in memory.
|
208
|
+
|
209
|
+
|
210
|
+
=== I18n Storage
|
211
|
+
|
212
|
+
The I18n storage backend stores the ids and names is memory still, but retrieves the name text translation any call to
|
213
|
+
the enum public methods.
|
214
|
+
|
215
|
+
To set up the locale file, run
|
216
|
+
|
217
|
+
rails g active_enum:locale
|
218
|
+
|
219
|
+
This generates the YML locale template in your default language as configured in the application.rb.
|
220
|
+
|
221
|
+
Here are some examples of defining enum translations
|
222
|
+
|
223
|
+
class Sex < ActiveEnum::Base
|
224
|
+
value 1 => 'male'
|
225
|
+
value 2 => 'female'
|
226
|
+
end
|
227
|
+
|
228
|
+
becomes
|
229
|
+
|
230
|
+
sex:
|
231
|
+
male: Male
|
232
|
+
female: Female
|
233
|
+
|
234
|
+
For namesapced enums in a model
|
235
|
+
|
236
|
+
class Person < ActiveRecord::Base
|
237
|
+
enumerate :sex do
|
238
|
+
value 1 => 'male'
|
239
|
+
value 2 => 'female'
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
nest the translations under the underscored model name
|
244
|
+
|
245
|
+
person:
|
246
|
+
sex:
|
247
|
+
male: Male
|
248
|
+
female: Female
|
249
|
+
|
201
250
|
|
202
251
|
Copyright (c) 2009 Adam Meehan, released under the MIT license
|
data/lib/active_enum.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'active_enum/base'
|
2
2
|
require 'active_enum/extensions'
|
3
3
|
require 'active_enum/storage/abstract_store'
|
4
|
+
require 'active_enum/version'
|
5
|
+
require 'active_enum/railtie' if defined?(Rails)
|
4
6
|
|
5
7
|
module ActiveEnum
|
6
|
-
autoload :VERSION, 'active_enum/version'
|
7
|
-
|
8
8
|
mattr_accessor :enum_classes
|
9
9
|
@@enum_classes = []
|
10
10
|
|
data/lib/active_enum/base.rb
CHANGED
@@ -49,7 +49,7 @@ module ActiveEnum
|
|
49
49
|
|
50
50
|
# Return enum values in an array suitable to pass to a Rails form select helper.
|
51
51
|
def to_select
|
52
|
-
|
52
|
+
store.values.map {|v| [v[1], v[0]] }
|
53
53
|
end
|
54
54
|
|
55
55
|
# Access id or name value. Pass an id number to retrieve the name or
|
@@ -64,6 +64,10 @@ module ActiveEnum
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def include?(value)
|
68
|
+
!self[value].nil?
|
69
|
+
end
|
70
|
+
|
67
71
|
# Access any meta data defined for a given id or name. Returns a hash.
|
68
72
|
def meta(index)
|
69
73
|
row = if index.is_a?(Fixnum)
|
@@ -83,7 +83,7 @@ module ActiveEnum
|
|
83
83
|
value = super()
|
84
84
|
return if value.nil? && arg.nil?
|
85
85
|
|
86
|
-
enum
|
86
|
+
enum = self.class.active_enum_for(:#{attribute})
|
87
87
|
case arg
|
88
88
|
when :id
|
89
89
|
value if enum[value]
|
@@ -125,14 +125,15 @@ module ActiveEnum
|
|
125
125
|
# user.sex?(:male)
|
126
126
|
#
|
127
127
|
def define_active_enum_question_method(attribute)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
128
|
+
class_eval <<-DEF
|
129
|
+
def #{attribute}?(*args)
|
130
|
+
if args.first
|
131
|
+
#{attribute} == self.class.active_enum_for(:#{attribute})[args.first]
|
132
|
+
else
|
133
|
+
super()
|
134
|
+
end
|
134
135
|
end
|
135
|
-
|
136
|
+
DEF
|
136
137
|
end
|
137
138
|
|
138
139
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'simple_form/version'
|
2
|
+
|
1
3
|
module ActiveEnum
|
2
4
|
module FormHelpers
|
3
5
|
module SimpleForm
|
@@ -12,7 +14,7 @@ module ActiveEnum
|
|
12
14
|
|
13
15
|
end
|
14
16
|
|
15
|
-
|
17
|
+
module InputExtension
|
16
18
|
|
17
19
|
def initialize(*args)
|
18
20
|
super
|
@@ -29,6 +31,16 @@ module ActiveEnum
|
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
34
|
+
if SimpleForm::VERSION < '2.0.0'
|
35
|
+
class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionInput
|
36
|
+
include ActiveEnum::FormHelpers::SimpleForm::InputExtension
|
37
|
+
end
|
38
|
+
else
|
39
|
+
class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
|
40
|
+
include ActiveEnum::FormHelpers::SimpleForm::InputExtension
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
32
44
|
SimpleForm::FormBuilder.class_eval do
|
33
45
|
include ActiveEnum::FormHelpers::SimpleForm::BuilderExtension
|
34
46
|
|
data/lib/active_enum/railtie.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module ActiveEnum
|
2
2
|
module Storage
|
3
3
|
autoload :MemoryStore, "active_enum/storage/memory_store"
|
4
|
+
autoload :I18nStore, "active_enum/storage/i18n_store"
|
4
5
|
|
5
6
|
class NotImplemented < StandardError; end
|
6
7
|
|
@@ -22,8 +23,15 @@ module ActiveEnum
|
|
22
23
|
end
|
23
24
|
|
24
25
|
def values
|
26
|
+
_values
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def _values
|
25
32
|
raise NotImplemented
|
26
33
|
end
|
34
|
+
|
27
35
|
end
|
28
36
|
end
|
29
37
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module ActiveEnum
|
4
|
+
module Storage
|
5
|
+
class I18nStore < MemoryStore
|
6
|
+
def get_by_id(id)
|
7
|
+
row = _values.assoc(id)
|
8
|
+
[ id, translate(row[1]) ] if row
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_by_name(name)
|
12
|
+
row = _values.rassoc(name.to_s)
|
13
|
+
[ row[0], translate(row[1]) ] if row
|
14
|
+
end
|
15
|
+
|
16
|
+
def values
|
17
|
+
_values.map { |(id, name)| get_by_id(id) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_duplicate(id, name)
|
21
|
+
if _values.assoc(id) || _values.rassoc(name.to_s)
|
22
|
+
raise ActiveEnum::DuplicateValue
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def i18n_scope
|
27
|
+
@i18n_scope ||= [ :active_enum ] + @enum.name.split("::").map { |nesting| nesting.underscore.to_sym }
|
28
|
+
end
|
29
|
+
|
30
|
+
def translate(key)
|
31
|
+
I18n.translate key, :scope => i18n_scope
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,22 +1,19 @@
|
|
1
1
|
module ActiveEnum
|
2
2
|
module Storage
|
3
3
|
class MemoryStore < AbstractStore
|
4
|
+
|
4
5
|
def set(id, name, meta=nil)
|
5
6
|
check_duplicate id, name
|
6
|
-
|
7
|
+
_values << [id, name.to_s, meta].compact
|
7
8
|
sort!
|
8
9
|
end
|
9
10
|
|
10
11
|
def get_by_id(id)
|
11
|
-
|
12
|
+
_values.assoc(id)
|
12
13
|
end
|
13
14
|
|
14
15
|
def get_by_name(name)
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def values
|
19
|
-
@values ||= []
|
16
|
+
_values.rassoc(name.to_s) || _values.rassoc(name.to_s.titleize)
|
20
17
|
end
|
21
18
|
|
22
19
|
def check_duplicate(id, name)
|
@@ -26,15 +23,18 @@ module ActiveEnum
|
|
26
23
|
end
|
27
24
|
|
28
25
|
def sort!
|
29
|
-
return if @order == :natural
|
30
|
-
|
31
26
|
case @order
|
32
27
|
when :asc
|
33
|
-
|
28
|
+
_values.sort! { |a,b| a[0] <=> b[0] }
|
34
29
|
when :desc
|
35
|
-
|
30
|
+
_values.sort! { |a,b| b[0] <=> a[0] }
|
36
31
|
end
|
37
32
|
end
|
33
|
+
|
34
|
+
def _values
|
35
|
+
@_values ||= []
|
36
|
+
end
|
37
|
+
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -3,10 +3,9 @@ module ActiveEnum
|
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
4
|
desc "Copy ActiveEnum default files"
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
6
|
-
class_option :template_engine
|
7
6
|
|
8
7
|
def copy_initializers
|
9
|
-
copy_file '
|
8
|
+
copy_file 'config.rb', 'config/initializers/active_enum.rb'
|
10
9
|
end
|
11
10
|
|
12
11
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveEnum
|
2
|
+
module Generators
|
3
|
+
class LocaleGenerator < Rails::Generators::Base
|
4
|
+
desc "Copy ActiveEnum locale file for I18n storage"
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
class_option :lang, :type => :string, :default => 'en', :desc => "Language for locale file"
|
7
|
+
|
8
|
+
def copy_initializers
|
9
|
+
template 'locale.yml', locale_full_path
|
10
|
+
end
|
11
|
+
|
12
|
+
def locale_filename
|
13
|
+
"active_enum.#{options[:lang]}.yml"
|
14
|
+
end
|
15
|
+
|
16
|
+
def locale_full_path
|
17
|
+
"config/locales/#{locale_filename}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#{@lang}:
|
2
|
+
active_enum:
|
3
|
+
|
4
|
+
# Place enum translations for each name defined in the enum
|
5
|
+
#
|
6
|
+
# class Sex < ActiveEnum::Base
|
7
|
+
# value 1 => 'male'
|
8
|
+
# value 2 => 'female'
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# is defined as
|
12
|
+
#
|
13
|
+
# sex:
|
14
|
+
# male: Male
|
15
|
+
# female: Female
|
16
|
+
#
|
17
|
+
# For namesapced enums in a model
|
18
|
+
#
|
19
|
+
# class Person < ActiveRecord::Base
|
20
|
+
# enumerate :sex do
|
21
|
+
# value 1 => 'male'
|
22
|
+
# value 2 => 'female'
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Nest the translations under the underscored model name
|
27
|
+
#
|
28
|
+
# person:
|
29
|
+
# sex:
|
30
|
+
# male: Male
|
31
|
+
# female: Female
|
@@ -76,21 +76,21 @@ describe ActiveEnum::Base do
|
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'should raise error if the id is a duplicate' do
|
79
|
-
|
79
|
+
expect {
|
80
80
|
define_enum do
|
81
81
|
value :id => 1, :name => 'Name 1'
|
82
82
|
value :id => 1, :name => 'Name 2'
|
83
83
|
end
|
84
|
-
|
84
|
+
}.to raise_error(ActiveEnum::DuplicateValue)
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'should raise error if the name is a duplicate' do
|
88
|
-
|
88
|
+
expect {
|
89
89
|
define_enum do
|
90
90
|
value :id => 1, :name => 'Name'
|
91
91
|
value :id => 2, :name => 'Name'
|
92
92
|
end
|
93
|
-
|
93
|
+
}.to raise_error(ActiveEnum::DuplicateValue)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -161,32 +161,50 @@ describe ActiveEnum::Base do
|
|
161
161
|
end
|
162
162
|
|
163
163
|
context "element reference method" do
|
164
|
-
|
165
|
-
|
166
|
-
enum = define_enum do
|
164
|
+
let(:enum) {
|
165
|
+
define_enum do
|
167
166
|
value :id => 1, :name => 'Name 1'
|
168
167
|
value :id => 2, :name => 'Name 2'
|
169
168
|
end
|
169
|
+
}
|
170
|
+
|
171
|
+
it 'should return name when given an id' do
|
170
172
|
enum[1].should == 'Name 1'
|
171
173
|
end
|
172
174
|
|
173
175
|
it 'should return id when given a name' do
|
174
|
-
enum = define_enum do
|
175
|
-
value :id => 1, :name => 'Name 1'
|
176
|
-
value :id => 2, :name => 'Name 2'
|
177
|
-
end
|
178
176
|
enum['Name 1'].should == 1
|
179
177
|
end
|
180
178
|
|
181
179
|
it 'should return id when given a symbol of the name' do
|
182
|
-
enum
|
180
|
+
enum[:Name_1].should == 1
|
181
|
+
enum[:name_1].should == 1
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe ".include?" do
|
186
|
+
let(:enum) {
|
187
|
+
define_enum do
|
183
188
|
value :id => 1, :name => 'Name 1'
|
184
189
|
value :id => 2, :name => 'Name 2'
|
185
190
|
end
|
186
|
-
|
187
|
-
|
191
|
+
}
|
192
|
+
|
193
|
+
it "should return true if value is a fixnum and matches an id" do
|
194
|
+
enum.include?(1).should be_true
|
188
195
|
end
|
189
196
|
|
197
|
+
it "should return false if value is a fixnum and does not match an id" do
|
198
|
+
enum.include?(3).should be_false
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should return true if value is a string and matches a name" do
|
202
|
+
enum.include?('Name 1').should be_true
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should return false if value is a string and does not match a name" do
|
206
|
+
enum.include?('No match').should be_false
|
207
|
+
end
|
190
208
|
end
|
191
209
|
|
192
210
|
describe ".to_select" do
|
@@ -199,7 +217,7 @@ describe ActiveEnum::Base do
|
|
199
217
|
end
|
200
218
|
|
201
219
|
it 'should return array sorted using order setting' do
|
202
|
-
enum = define_enum
|
220
|
+
enum = define_enum do
|
203
221
|
order :desc
|
204
222
|
value :id => 1, :name => 'Name 1'
|
205
223
|
value :id => 2, :name => 'Name 2'
|
@@ -0,0 +1,184 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActiveEnum::Storage::I18nStore do
|
4
|
+
class TestI18nStoreEnum < ActiveEnum::Base; end
|
5
|
+
|
6
|
+
let(:enum_class) { TestI18nStoreEnum }
|
7
|
+
let(:enum_key) { enum_class.name.underscore }
|
8
|
+
let(:store) { ActiveEnum::Storage::I18nStore.new(enum_class, :asc) }
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
@_default_store = ActiveEnum.storage
|
12
|
+
ActiveEnum.storage = :i18n
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) do
|
16
|
+
ActiveEnum.storage = @_default_store
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
@default_locale = I18n.locale
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
I18n.locale = @default_locale
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#set' do
|
28
|
+
it 'should store value of id and name' do
|
29
|
+
store.set 1, 'test name'
|
30
|
+
store.send(:_values).should == [[1, 'test name']]
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should store value of id, name and meta hash' do
|
34
|
+
store.set 1, 'test name', :description => 'meta'
|
35
|
+
store.send(:_values).should == [[1, 'test name', {:description => 'meta'}]]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should raise error if duplicate id' do
|
39
|
+
expect {
|
40
|
+
store.set 1, 'Name 1'
|
41
|
+
store.set 1, 'Other Name'
|
42
|
+
}.should raise_error(ActiveEnum::DuplicateValue)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should raise error if duplicate name' do
|
46
|
+
expect {
|
47
|
+
store.set 1, 'Name 1'
|
48
|
+
store.set 2, 'Name 1'
|
49
|
+
}.should raise_error(ActiveEnum::DuplicateValue)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should not raise error if duplicate name with alternate case matches' do
|
53
|
+
expect {
|
54
|
+
store.set 1, 'Name 1'
|
55
|
+
store.set 2, 'name 1'
|
56
|
+
}.should_not raise_error(ActiveEnum::DuplicateValue)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#values" do
|
61
|
+
before do
|
62
|
+
I18n.backend.store_translations :en, :active_enum => { enum_key => { :thanks => 'Thanks' } }
|
63
|
+
I18n.backend.store_translations :fr, :active_enum => { enum_key => { :thanks => 'Merce' } }
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should return array of stored values for current locale' do
|
67
|
+
store.set 1, 'thanks'
|
68
|
+
|
69
|
+
I18n.locale = :en
|
70
|
+
store.values.should == [ [1, 'Thanks'] ]
|
71
|
+
|
72
|
+
I18n.locale = :fr
|
73
|
+
store.values.should == [ [1, 'Merce'] ]
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#get_by_id" do
|
79
|
+
before do
|
80
|
+
I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return the value for a given id' do
|
84
|
+
I18n.locale = :en
|
85
|
+
|
86
|
+
store.set 1, 'test'
|
87
|
+
store.get_by_id(1).should == [1, 'Testing']
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should return nil when id not found' do
|
91
|
+
store.get_by_id(1).should be_nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#get_by_name" do
|
96
|
+
before do
|
97
|
+
I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should return the value for a given name' do
|
101
|
+
store.set 1, 'test'
|
102
|
+
store.get_by_name('test').should == [1, 'Testing']
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should return nil when name not found' do
|
106
|
+
store.get_by_name('test').should be_nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "#sort!" do
|
111
|
+
before do
|
112
|
+
I18n.backend.store_translations :en, :active_enum => { enum_key => {
|
113
|
+
'name1' => 'Name 1',
|
114
|
+
'name2' => 'Name 2',
|
115
|
+
'name3' => 'Name 3',
|
116
|
+
} }
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should sort values ascending when passed :asc' do
|
120
|
+
store = described_class.new(enum_class, :asc)
|
121
|
+
|
122
|
+
store.set 2, 'name2'
|
123
|
+
store.set 1, 'name1'
|
124
|
+
store.values.should == [[1,'Name 1'], [2, 'Name 2']]
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should sort values descending when passed :desc' do
|
128
|
+
store = described_class.new(enum_class, :desc)
|
129
|
+
|
130
|
+
store.set 1, 'name1'
|
131
|
+
store.set 2, 'name2'
|
132
|
+
store.values.should == [[2, 'Name 2'], [1,'Name 1']]
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should not sort values when passed :natural' do
|
136
|
+
store = described_class.new(enum_class, :natural)
|
137
|
+
|
138
|
+
store.set 1, 'name1'
|
139
|
+
store.set 3, 'name3'
|
140
|
+
store.set 2, 'name2'
|
141
|
+
store.values.should == [[1,'Name 1'], [3,'Name 3'], [2, 'Name 2']]
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "loaded from yaml locale" do
|
146
|
+
before do
|
147
|
+
I18n.load_path << File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'support', 'i18n.yml'))
|
148
|
+
I18n.reload!
|
149
|
+
I18n.locale = :en
|
150
|
+
end
|
151
|
+
|
152
|
+
context "for top level enum" do
|
153
|
+
class TopLevelEnum < ActiveEnum::Base; end
|
154
|
+
let(:enum_class) { TopLevelEnum }
|
155
|
+
|
156
|
+
it 'should return array values from yaml' do
|
157
|
+
store.set 1, 'things'
|
158
|
+
store.get_by_name('things').should eq [1, 'Generic things']
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should not load locale entry unless defined in enum' do
|
162
|
+
store.set 1, 'things'
|
163
|
+
store.get_by_name('not_found').should be_nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "for namespaced model enum" do
|
168
|
+
module Namespaced; class ModelEnum < ActiveEnum::Base; end; end
|
169
|
+
let(:enum_class) { Namespaced::ModelEnum }
|
170
|
+
|
171
|
+
it 'should return array values from yaml' do
|
172
|
+
store.set 1, 'things'
|
173
|
+
store.get_by_name('things').should eq [1, 'Model things']
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'should not load locale entry unless defined in enum' do
|
177
|
+
store.set 1, 'things'
|
178
|
+
store.get_by_name('not_found').should be_nil
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
class TestMemoryStoreEnum < ActiveEnum::Base; end
|
4
|
-
class TestOtherAREnum < ActiveEnum::Base; end
|
5
|
-
|
6
3
|
describe ActiveEnum::Storage::MemoryStore do
|
7
4
|
attr_accessor :store
|
8
5
|
|
6
|
+
class TestMemoryStoreEnum < ActiveEnum::Base; end
|
7
|
+
class TestOtherAREnum < ActiveEnum::Base; end
|
8
|
+
|
9
9
|
describe '#set' do
|
10
10
|
it 'should store value of id and name' do
|
11
11
|
store.set 1, 'test name'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 43
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 8
|
10
|
+
version: 0.9.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-07-02 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -59,19 +59,24 @@ files:
|
|
59
59
|
- lib/active_enum/form_helpers/simple_form.rb
|
60
60
|
- lib/active_enum/railtie.rb
|
61
61
|
- lib/active_enum/storage/abstract_store.rb
|
62
|
+
- lib/active_enum/storage/i18n_store.rb
|
62
63
|
- lib/active_enum/storage/memory_store.rb
|
63
64
|
- lib/active_enum/version.rb
|
64
65
|
- lib/generators/active_enum/install_generator.rb
|
65
|
-
- lib/generators/active_enum/
|
66
|
+
- lib/generators/active_enum/locale_generator.rb
|
67
|
+
- lib/generators/active_enum/templates/config.rb
|
68
|
+
- lib/generators/active_enum/templates/locale.yml
|
66
69
|
- spec/active_enum/acts_as_enum_spec.rb
|
67
70
|
- spec/active_enum/base_spec.rb
|
68
71
|
- spec/active_enum/extensions_spec.rb
|
69
72
|
- spec/active_enum/form_helpers/formtastic2_spec.rb
|
70
73
|
- spec/active_enum/form_helpers/formtastic_spec.rb
|
71
74
|
- spec/active_enum/form_helpers/simple_form_spec.rb
|
75
|
+
- spec/active_enum/storage/i18n_store_spec.rb
|
72
76
|
- spec/active_enum/storage/memory_store_spec.rb
|
73
77
|
- spec/active_enum_spec.rb
|
74
78
|
- spec/spec_helper.rb
|
79
|
+
- spec/support/i18n.yml
|
75
80
|
- spec/support/schema.rb
|
76
81
|
has_rdoc: true
|
77
82
|
homepage: http://github.com/adzap/active_enum
|
@@ -114,7 +119,9 @@ test_files:
|
|
114
119
|
- spec/active_enum/form_helpers/formtastic2_spec.rb
|
115
120
|
- spec/active_enum/form_helpers/formtastic_spec.rb
|
116
121
|
- spec/active_enum/form_helpers/simple_form_spec.rb
|
122
|
+
- spec/active_enum/storage/i18n_store_spec.rb
|
117
123
|
- spec/active_enum/storage/memory_store_spec.rb
|
118
124
|
- spec/active_enum_spec.rb
|
119
125
|
- spec/spec_helper.rb
|
126
|
+
- spec/support/i18n.yml
|
120
127
|
- spec/support/schema.rb
|