active_enum 0.9.12 → 1.0.0.pre
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 +7 -0
- data/.travis.yml +8 -0
- data/README.rdoc +36 -16
- data/Rakefile +1 -1
- data/active_enum.gemspec +1 -1
- data/lib/active_enum.rb +8 -1
- data/lib/active_enum/acts_as_enum.rb +4 -4
- data/lib/active_enum/base.rb +17 -11
- data/lib/active_enum/extensions.rb +5 -5
- data/lib/active_enum/form_helpers/simple_form.rb +9 -28
- data/lib/active_enum/storage/abstract_store.rb +8 -0
- data/lib/active_enum/storage/i18n_store.rb +0 -6
- data/lib/active_enum/storage/memory_store.rb +0 -6
- data/lib/active_enum/version.rb +1 -1
- data/lib/generators/active_enum/templates/config.rb +4 -2
- data/spec/active_enum/acts_as_enum_spec.rb +15 -10
- data/spec/active_enum/base_spec.rb +49 -27
- data/spec/active_enum/extensions_spec.rb +47 -35
- data/spec/active_enum/form_helpers/simple_form_spec.rb +12 -12
- data/spec/active_enum/storage/i18n_store_spec.rb +21 -21
- data/spec/active_enum/storage/memory_store_spec.rb +15 -15
- data/spec/active_enum_spec.rb +7 -7
- data/spec/spec_helper.rb +25 -4
- data/spec/support/config_helper.rb +26 -0
- data/spec/support/schema.rb +8 -0
- metadata +37 -62
- data/lib/active_enum/form_helpers/formtastic.rb +0 -21
- data/lib/active_enum/form_helpers/formtastic2.rb +0 -29
- data/spec/active_enum/form_helpers/formtastic2_spec.rb +0 -80
- data/spec/active_enum/form_helpers/formtastic_spec.rb +0 -83
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4aae7beff8dddf1e7a626ce7c6de2b35fa820f38
|
4
|
+
data.tar.gz: 3995d9471445a83cf36a771c5ef85c73ca63e54a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1047ed7a0228ddb9b1969a5222e6b080cf3f7e8453cc4d635d5ee90cda8206446e4c5b7a178d5f1496ca00c0ec0dd236ae495b3269e60add8878c84bc8147343
|
7
|
+
data.tar.gz: 008696a99059da7b5a343502ec0a4639d6ffc009acbc2beb5ed80c8903c98afb249ad9ce9c9f0165734fc14870da2c8e808bbc21e6917755c8138d2ac00d7de6
|
data/.travis.yml
ADDED
data/README.rdoc
CHANGED
@@ -7,13 +7,15 @@ Enum values are stored in memory at the moment but I plan to add database and po
|
|
7
7
|
|
8
8
|
== Install
|
9
9
|
|
10
|
-
|
10
|
+
Supprots Ruby versions 2.0+.
|
11
|
+
|
12
|
+
From gem version 1.0 onwards this plugin will only have Rails 4+ support. If you need Rails 3 compatibility, {check the 0.9 branch}[https://github.com/adzap/active_enum/tree/0.9].
|
11
13
|
|
12
14
|
gem install active_enum
|
13
15
|
|
14
16
|
Put this in your Gemfile
|
15
17
|
|
16
|
-
gem 'active_enum'
|
18
|
+
gem 'active_enum', '~> 1.0.0.pre'
|
17
19
|
|
18
20
|
Then generate the config initializer file
|
19
21
|
|
@@ -135,11 +137,17 @@ Access the enum values and the enum class using the attribute method with a symb
|
|
135
137
|
user.sex(:enum) # => Sex
|
136
138
|
user.sex(:symbol) # => ♂ ( Can use any meta data key )
|
137
139
|
|
138
|
-
You can set the default to return the enum name value for enumerated
|
140
|
+
You can set the default to return the enum name value for enumerated attributes
|
141
|
+
|
142
|
+
ActiveEnum.setup do |config|
|
143
|
+
config.use_name_as_value = true
|
144
|
+
end
|
145
|
+
|
146
|
+
And now the name is returned for the regular attribute read method.
|
139
147
|
|
140
|
-
ActiveEnum.use_name_as_value = true
|
141
148
|
user.sex # => 'Male'
|
142
149
|
|
150
|
+
|
143
151
|
=== Boolean check
|
144
152
|
|
145
153
|
You can check if the attribute value matches a particular enum value by passing the enum value as an argument to the question method
|
@@ -155,6 +163,25 @@ A convenience method on the class is available to the enum class of any enumerat
|
|
155
163
|
|
156
164
|
User.active_enum_for(:sex) # => Sex
|
157
165
|
|
166
|
+
== Raise exception when value not found
|
167
|
+
|
168
|
+
When a value if not found for a given id or name the default behaviour is to return nil. If you would like to raise an error use
|
169
|
+
|
170
|
+
ActiveEnum.setup do |config|
|
171
|
+
config.raise_on_not_found = true
|
172
|
+
end
|
173
|
+
|
174
|
+
Then for the following examples
|
175
|
+
|
176
|
+
Sex['Other']
|
177
|
+
|
178
|
+
# Or
|
179
|
+
|
180
|
+
Sex[3]
|
181
|
+
|
182
|
+
will raise an ActiveEnum::NotFound exception.
|
183
|
+
|
184
|
+
|
158
185
|
=== Bulk definition
|
159
186
|
|
160
187
|
Define enum classes in bulk without class files, in an initializer file for example.
|
@@ -194,19 +221,10 @@ Giving you the familiar enum methods
|
|
194
221
|
|
195
222
|
=== Form Helpers
|
196
223
|
|
197
|
-
There is support for
|
198
|
-
to make it easier to use the enum values as select options.
|
199
|
-
You just need to require the extension for the respective plugin in an initialiser:
|
224
|
+
There is support for SimpleForm[http://github.com/plataformatec/simple_form] version 3+.
|
225
|
+
to make it easier to use the enum values as select options.
|
200
226
|
|
201
|
-
|
202
|
-
|
203
|
-
require 'active_enum/form_helpers/formtastic2'
|
204
|
-
|
205
|
-
Formtastic <2:
|
206
|
-
|
207
|
-
require 'active_enum/form_helpers/formtastic'
|
208
|
-
|
209
|
-
Or, SimpleForm:
|
227
|
+
In the initializer:
|
210
228
|
|
211
229
|
require 'active_enum/form_helpers/simple_form'
|
212
230
|
|
@@ -266,4 +284,6 @@ nest the translations under the underscored model name
|
|
266
284
|
female: Female
|
267
285
|
|
268
286
|
|
287
|
+
== License
|
288
|
+
|
269
289
|
Copyright (c) 2009 Adam Meehan, released under the MIT license
|
data/Rakefile
CHANGED
data/active_enum.gemspec
CHANGED
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
17
|
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG", "MIT-LICENSE"]
|
18
18
|
|
19
|
-
s.add_runtime_dependency(%q<activesupport>, ["
|
19
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
|
20
20
|
end
|
data/lib/active_enum.rb
CHANGED
@@ -11,6 +11,9 @@ module ActiveEnum
|
|
11
11
|
mattr_accessor :use_name_as_value
|
12
12
|
@@use_name_as_value = false
|
13
13
|
|
14
|
+
mattr_accessor :raise_on_not_found
|
15
|
+
@@raise_on_not_found = false
|
16
|
+
|
14
17
|
mattr_accessor :storage
|
15
18
|
@@storage = :memory
|
16
19
|
|
@@ -27,10 +30,14 @@ module ActiveEnum
|
|
27
30
|
|
28
31
|
# Setup method for plugin configuration
|
29
32
|
def self.setup
|
30
|
-
yield
|
33
|
+
yield config
|
31
34
|
extend_classes!
|
32
35
|
end
|
33
36
|
|
37
|
+
def self.config
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
34
41
|
class EnumDefinitions
|
35
42
|
def enum(name, &block)
|
36
43
|
class_name = name.to_s.camelize
|
@@ -7,9 +7,9 @@ module ActiveEnum
|
|
7
7
|
extend ClassMethods
|
8
8
|
class_attribute :active_enum_options
|
9
9
|
self.active_enum_options = options.reverse_merge(:name_column => 'name')
|
10
|
-
scope :enum_values, select("#{primary_key}, #{active_enum_options[:name_column]}").
|
11
|
-
|
12
|
-
|
10
|
+
scope :enum_values, proc { select("#{primary_key}, #{active_enum_options[:name_column]}").
|
11
|
+
where(active_enum_options[:conditions]).
|
12
|
+
order("#{primary_key} #{active_enum_options[:order]}") }
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
@@ -45,7 +45,7 @@ module ActiveEnum
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def lookup_by_name(index)
|
48
|
-
enum_values.where("#{active_enum_options[:name_column]} like ?", index.to_s).first
|
48
|
+
enum_values.where("#{active_enum_options[:name_column]} like lower(?)", index.to_s).first
|
49
49
|
end
|
50
50
|
|
51
51
|
end
|
data/lib/active_enum/base.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module ActiveEnum
|
2
2
|
class DuplicateValue < StandardError; end
|
3
3
|
class InvalidValue < StandardError; end
|
4
|
+
class NotFound < StandardError; end
|
4
5
|
|
5
6
|
class Base
|
6
7
|
|
@@ -37,6 +38,10 @@ module ActiveEnum
|
|
37
38
|
store.values
|
38
39
|
end
|
39
40
|
|
41
|
+
def each(&block)
|
42
|
+
all.each(&block)
|
43
|
+
end
|
44
|
+
|
40
45
|
# Array of all enum id values
|
41
46
|
def ids
|
42
47
|
store.values.map {|v| v[0] }
|
@@ -55,15 +60,20 @@ module ActiveEnum
|
|
55
60
|
# Access id or name value. Pass an id number to retrieve the name or
|
56
61
|
# a symbol or string to retrieve the matching id.
|
57
62
|
def get(index)
|
63
|
+
row = get_value(index)
|
64
|
+
return if row.nil?
|
65
|
+
index.is_a?(Fixnum) ? row[1] : row[0]
|
66
|
+
end
|
67
|
+
alias_method :[], :get
|
68
|
+
|
69
|
+
# Access value row array for a given id or name value.
|
70
|
+
def get_value(index)
|
58
71
|
if index.is_a?(Fixnum)
|
59
|
-
|
60
|
-
row[1] if row
|
72
|
+
store.get_by_id(index)
|
61
73
|
else
|
62
|
-
|
63
|
-
|
64
|
-
end
|
74
|
+
store.get_by_name(index)
|
75
|
+
end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
65
76
|
end
|
66
|
-
alias_method :[], :get
|
67
77
|
|
68
78
|
def include?(value)
|
69
79
|
!get(value).nil?
|
@@ -71,11 +81,7 @@ module ActiveEnum
|
|
71
81
|
|
72
82
|
# Access any meta data defined for a given id or name. Returns a hash.
|
73
83
|
def meta(index)
|
74
|
-
row =
|
75
|
-
store.get_by_id(index)
|
76
|
-
else
|
77
|
-
store.get_by_name(index)
|
78
|
-
end
|
84
|
+
row = get_value(index)
|
79
85
|
row[2] || {} if row
|
80
86
|
end
|
81
87
|
|
@@ -80,10 +80,12 @@ module ActiveEnum
|
|
80
80
|
def define_active_enum_read_method(attribute)
|
81
81
|
class_eval <<-DEF
|
82
82
|
def #{attribute}(arg=nil)
|
83
|
+
enum = self.class.active_enum_for(:#{attribute})
|
84
|
+
return enum if arg == :enum
|
85
|
+
|
83
86
|
value = super()
|
84
|
-
return if value.nil?
|
87
|
+
return if value.nil?
|
85
88
|
|
86
|
-
enum = self.class.active_enum_for(:#{attribute})
|
87
89
|
case arg
|
88
90
|
when nil
|
89
91
|
#{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
|
@@ -91,8 +93,6 @@ module ActiveEnum
|
|
91
93
|
value if enum[value]
|
92
94
|
when :name
|
93
95
|
enum[value]
|
94
|
-
when :enum
|
95
|
-
enum
|
96
96
|
when Symbol
|
97
97
|
(enum.meta(value) || {})[arg]
|
98
98
|
end
|
@@ -127,7 +127,7 @@ module ActiveEnum
|
|
127
127
|
class_eval <<-DEF
|
128
128
|
def #{attribute}?(arg=nil)
|
129
129
|
if arg
|
130
|
-
self.#{attribute} == self.class.active_enum_for(:#{attribute})[arg]
|
130
|
+
self.#{attribute}(:id) == self.class.active_enum_for(:#{attribute})[arg]
|
131
131
|
else
|
132
132
|
super()
|
133
133
|
end
|
@@ -5,46 +5,27 @@ module ActiveEnum
|
|
5
5
|
module SimpleForm
|
6
6
|
module BuilderExtension
|
7
7
|
|
8
|
-
def
|
9
|
-
return :enum if
|
10
|
-
object.class.respond_to?(:active_enum_for) &&
|
11
|
-
object.class.active_enum_for(args.first || @attribute_name)
|
12
|
-
default_input_type_without_active_enum(*args, &block)
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
module InputExtension
|
18
|
-
|
19
|
-
def initialize(*args)
|
8
|
+
def find_custom_type(attribute_name)
|
9
|
+
return :enum if object.class.active_enum_for(attribute_name) if object.class.respond_to?(:active_enum_for)
|
20
10
|
super
|
21
|
-
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
|
22
|
-
if respond_to?(:input_options)
|
23
|
-
input_options[:collection] = enum.to_select
|
24
|
-
else
|
25
|
-
@builder.options[:collection] = enum.to_select
|
26
|
-
end
|
27
11
|
end
|
28
|
-
|
12
|
+
|
29
13
|
end
|
30
14
|
end
|
31
15
|
end
|
32
16
|
end
|
33
17
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
|
40
|
-
include ActiveEnum::FormHelpers::SimpleForm::InputExtension
|
18
|
+
class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
|
19
|
+
def initialize(*args)
|
20
|
+
super
|
21
|
+
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
|
22
|
+
input_options[:collection] = enum.to_select
|
41
23
|
end
|
42
24
|
end
|
43
25
|
|
44
26
|
SimpleForm::FormBuilder.class_eval do
|
45
|
-
|
27
|
+
prepend ActiveEnum::FormHelpers::SimpleForm::BuilderExtension
|
46
28
|
|
47
29
|
map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm::EnumInput
|
48
30
|
alias_method :collection_enum, :collection_select
|
49
|
-
alias_method_chain :default_input_type, :active_enum
|
50
31
|
end
|
@@ -22,6 +22,14 @@ module ActiveEnum
|
|
22
22
|
raise NotImplemented
|
23
23
|
end
|
24
24
|
|
25
|
+
def check_duplicate(id, name)
|
26
|
+
if get_by_id(id)
|
27
|
+
raise ActiveEnum::DuplicateValue, "#{@enum}: Duplicate id #{id}"
|
28
|
+
elsif get_by_name(name)
|
29
|
+
raise ActiveEnum::DuplicateValue, "#{@enum}: Duplicate name '#{name}'"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
25
33
|
def values
|
26
34
|
_values
|
27
35
|
end
|
@@ -17,12 +17,6 @@ module ActiveEnum
|
|
17
17
|
_values.map { |(id, name)| get_by_id(id) }
|
18
18
|
end
|
19
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
20
|
def i18n_scope
|
27
21
|
@i18n_scope ||= [ :active_enum ] + @enum.name.split("::").map { |nesting| nesting.underscore.to_sym }
|
28
22
|
end
|
@@ -16,12 +16,6 @@ module ActiveEnum
|
|
16
16
|
_values.rassoc(name.to_s) || _values.rassoc(name.to_s.titleize)
|
17
17
|
end
|
18
18
|
|
19
|
-
def check_duplicate(id, name)
|
20
|
-
if get_by_id(id) || get_by_name(name)
|
21
|
-
raise ActiveEnum::DuplicateValue
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
19
|
def sort!
|
26
20
|
case @order
|
27
21
|
when :asc
|
data/lib/active_enum/version.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# Form helper integration
|
2
|
-
# require 'active_enum/form_helpers/
|
3
|
-
# require 'active_enum/form_helpers/formtastic2' # for Formtastic 2.x
|
2
|
+
# require 'active_enum/form_helpers/simple_form' # for SimpleForm
|
4
3
|
|
5
4
|
ActiveEnum.setup do |config|
|
6
5
|
|
@@ -10,6 +9,9 @@ ActiveEnum.setup do |config|
|
|
10
9
|
# Return name string as value for attribute method
|
11
10
|
# config.use_name_as_value = false
|
12
11
|
|
12
|
+
# Raise exception ActiveEnum::NotFound if enum value for a given id or name is not found
|
13
|
+
# config.raise_on_not_found = false
|
14
|
+
|
13
15
|
# Storage of values (:memory, :i18n)
|
14
16
|
# config.storage = :memory
|
15
17
|
|
@@ -13,36 +13,41 @@ describe ActiveEnum::ActsAsEnum do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
class SortedPerson < ActiveRecord::Base
|
17
|
+
acts_as_enum :name_column => 'first_name', :order => :desc
|
18
|
+
end
|
19
|
+
|
16
20
|
before(:all) do
|
17
21
|
Person.create!(:first_name => 'Dave', :last_name => 'Smith')
|
18
22
|
Person.create!(:first_name => 'John', :last_name => 'Doe')
|
23
|
+
SortedPerson.create!(:first_name => 'Dave', :last_name => 'Smith')
|
24
|
+
SortedPerson.create!(:first_name => 'John', :last_name => 'Doe')
|
19
25
|
end
|
20
26
|
|
21
27
|
it "should mixin enum class methods only when act_as_enum defined" do
|
22
|
-
TestPerson.extended_modules.
|
28
|
+
expect(TestPerson.extended_modules).not_to include(ActiveEnum::ActsAsEnum::ClassMethods)
|
23
29
|
TestPerson.acts_as_enum
|
24
|
-
TestPerson.extended_modules.
|
30
|
+
expect(TestPerson.extended_modules).to include(ActiveEnum::ActsAsEnum::ClassMethods)
|
25
31
|
end
|
26
32
|
|
27
33
|
it "should return name column value when passing id to [] method" do
|
28
|
-
Person[1].
|
34
|
+
expect(Person[1]).to eq('Dave')
|
29
35
|
end
|
30
36
|
|
31
37
|
it "should return id column value when passing string name to [] method" do
|
32
|
-
Person['Dave'].
|
33
|
-
Person['dave'].
|
38
|
+
expect(Person['Dave']).to eq(1)
|
39
|
+
expect(Person['dave']).to eq(1)
|
34
40
|
end
|
35
41
|
|
36
42
|
it "should return id column value when passing symbol name to [] method" do
|
37
|
-
Person[:dave].
|
43
|
+
expect(Person[:dave]).to eq(1)
|
38
44
|
end
|
39
45
|
|
40
46
|
it "should return array for select helpers from to_select" do
|
41
|
-
Person.to_select.
|
47
|
+
expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
|
42
48
|
end
|
43
49
|
|
44
|
-
it "should return sorted array from order value for select helpers from to_select" do
|
45
|
-
|
46
|
-
Person.to_select.should == [['John', 2], ['Dave', 1]]
|
50
|
+
it "should return sorted array from order value for select helpers from to_select when an order is specified" do
|
51
|
+
expect(SortedPerson.to_select).to eq([['John', 2], ['Dave', 1]])
|
47
52
|
end
|
48
53
|
end
|