active_enum 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README.rdoc +1 -1
- data/Rakefile +0 -2
- data/active_enum.gemspec +3 -4
- data/lib/active_enum.rb +4 -3
- data/lib/active_enum/acts_as_enum.rb +1 -6
- data/lib/active_enum/base.rb +14 -4
- data/lib/active_enum/extensions.rb +6 -1
- data/lib/active_enum/form_helpers/formtastic.rb +4 -5
- data/lib/active_enum/form_helpers/simple_form.rb +13 -10
- data/lib/active_enum/version.rb +1 -1
- data/lib/generators/active_enum/templates/active_enum.rb +2 -2
- data/spec/active_enum/acts_as_enum_spec.rb +10 -12
- data/spec/active_enum/base_spec.rb +16 -8
- data/spec/active_enum/extensions_spec.rb +66 -89
- data/spec/active_enum/form_helpers/formtastic_spec.rb +22 -5
- data/spec/active_enum/form_helpers/simple_form_spec.rb +23 -6
- data/spec/active_enum/storage/memory_store_spec.rb +8 -8
- data/spec/active_enum_spec.rb +4 -5
- data/spec/schema.rb +0 -13
- data/spec/spec_helper.rb +12 -9
- metadata +5 -7
- data/lib/active_enum/storage/active_record_store.rb +0 -64
- data/spec/active_enum/storage/active_record_store_spec.rb +0 -133
data/CHANGELOG
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[0.9.1] : 2011-01-12
|
2
|
+
- Fix SimpleForm helper for v1.3.0 with backwards compat.
|
3
|
+
|
1
4
|
[0.9.0] : 2010-08-11
|
2
5
|
- Full ActiveModel support for enumerate extension. Use ActiveEnum.extend_classes array to indicate classes to extend.
|
3
6
|
- Added generator for initializer config file with all settings.
|
data/README.rdoc
CHANGED
@@ -7,7 +7,7 @@ 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
|
-
From version 0.7 onwards this plugin will only have Rails 3 support. If you need Rails 2 compatibility, check the 0.6 branch.
|
10
|
+
From version 0.7 onwards this plugin will only have Rails 3 support. If you need Rails 2 compatibility, {check the 0.6 branch}[https://github.com/adzap/active_enum/tree/0.6].
|
11
11
|
|
12
12
|
gem install active_enum
|
13
13
|
|
data/Rakefile
CHANGED
@@ -20,9 +20,7 @@ spec = Gem::Specification.new do |s|
|
|
20
20
|
s.author = "Adam Meehan"
|
21
21
|
s.email = "adam.meehan@gmail.com"
|
22
22
|
s.homepage = "http://github.com/adzap/active_enum"
|
23
|
-
|
24
23
|
s.require_path = 'lib'
|
25
|
-
s.autorequire = GEM_NAME
|
26
24
|
s.files = %w(active_enum.gemspec MIT-LICENSE CHANGELOG README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
|
27
25
|
end
|
28
26
|
|
data/active_enum.gemspec
CHANGED
@@ -2,16 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{active_enum}
|
5
|
-
s.version = "0.9.
|
5
|
+
s.version = "0.9.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Adam Meehan"]
|
9
|
-
s.
|
10
|
-
s.date = %q{2010-08-11}
|
9
|
+
s.date = %q{2011-01-12}
|
11
10
|
s.description = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
|
12
11
|
s.email = %q{adam.meehan@gmail.com}
|
13
12
|
s.extra_rdoc_files = ["README.rdoc"]
|
14
|
-
s.files = ["active_enum.gemspec", "MIT-LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/active_enum", "lib/active_enum/acts_as_enum.rb", "lib/active_enum/base.rb", "lib/active_enum/extensions.rb", "lib/active_enum/form_helpers", "lib/active_enum/form_helpers/formtastic.rb", "lib/active_enum/form_helpers/simple_form.rb", "lib/active_enum/storage", "lib/active_enum/storage/abstract_store.rb", "lib/active_enum/storage/
|
13
|
+
s.files = ["active_enum.gemspec", "MIT-LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/active_enum", "lib/active_enum/acts_as_enum.rb", "lib/active_enum/base.rb", "lib/active_enum/extensions.rb", "lib/active_enum/form_helpers", "lib/active_enum/form_helpers/formtastic.rb", "lib/active_enum/form_helpers/simple_form.rb", "lib/active_enum/storage", "lib/active_enum/storage/abstract_store.rb", "lib/active_enum/storage/memory_store.rb", "lib/active_enum/version.rb", "lib/active_enum.rb", "lib/generators", "lib/generators/active_enum", "lib/generators/active_enum/install_generator.rb", "lib/generators/active_enum/templates", "lib/generators/active_enum/templates/active_enum.rb", "spec/active_enum", "spec/active_enum/acts_as_enum_spec.rb", "spec/active_enum/base_spec.rb", "spec/active_enum/extensions_spec.rb", "spec/active_enum/form_helpers", "spec/active_enum/form_helpers/formtastic_spec.rb", "spec/active_enum/form_helpers/simple_form_spec.rb", "spec/active_enum/storage", "spec/active_enum/storage/memory_store_spec.rb", "spec/active_enum_spec.rb", "spec/schema.rb", "spec/spec_helper.rb"]
|
15
14
|
s.homepage = %q{http://github.com/adzap/active_enum}
|
16
15
|
s.require_paths = ["lib"]
|
17
16
|
s.rubyforge_project = %q{active_enum}
|
data/lib/active_enum.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'active_enum/base'
|
2
2
|
require 'active_enum/extensions'
|
3
|
-
require 'active_enum/acts_as_enum'
|
3
|
+
require 'active_enum/acts_as_enum' if defined?(ActiveRecord)
|
4
4
|
require 'active_enum/storage/abstract_store'
|
5
|
-
require 'active_enum/version'
|
6
5
|
|
7
6
|
module ActiveEnum
|
7
|
+
autoload :VERSION, 'active_enum/version'
|
8
|
+
|
8
9
|
mattr_accessor :enum_classes
|
9
10
|
@@enum_classes = []
|
10
11
|
|
@@ -15,7 +16,7 @@ module ActiveEnum
|
|
15
16
|
@@storage = :memory
|
16
17
|
|
17
18
|
mattr_accessor :extend_classes
|
18
|
-
@@extend_classes = []
|
19
|
+
@@extend_classes = [ defined?(ActiveRecord) && ActiveRecord::Base ].compact
|
19
20
|
|
20
21
|
def self.extend_classes=(klasses)
|
21
22
|
@@extend_classes = klasses
|
@@ -1,11 +1,6 @@
|
|
1
1
|
module ActiveEnum
|
2
|
-
|
3
2
|
module ActsAsEnum
|
4
3
|
|
5
|
-
def self.included(base)
|
6
|
-
base.extend MacroMethods
|
7
|
-
end
|
8
|
-
|
9
4
|
module MacroMethods
|
10
5
|
|
11
6
|
def acts_as_enum(options={})
|
@@ -58,4 +53,4 @@ module ActiveEnum
|
|
58
53
|
end
|
59
54
|
end
|
60
55
|
|
61
|
-
ActiveRecord::Base.
|
56
|
+
ActiveRecord::Base.extend ActiveEnum::ActsAsEnum::MacroMethods
|
data/lib/active_enum/base.rb
CHANGED
@@ -11,15 +11,19 @@ module ActiveEnum
|
|
11
11
|
ActiveEnum.enum_classes << subclass
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
14
|
+
# Define enum values.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
# value :id => 1, :name => 'Foo'
|
18
|
+
# value :name => 'Foo' # implicit id, incrementing from 1.
|
19
|
+
# value 1 => 'Foo'
|
17
20
|
#
|
18
21
|
def value(enum_value)
|
19
22
|
store.set *id_and_name_and_meta(enum_value)
|
20
23
|
end
|
21
24
|
|
22
|
-
#
|
25
|
+
# Specify order enum values are returned.
|
26
|
+
# Allowed values are :asc, :desc or :as_defined
|
23
27
|
#
|
24
28
|
def order(order)
|
25
29
|
@order = order
|
@@ -29,18 +33,23 @@ module ActiveEnum
|
|
29
33
|
store.values
|
30
34
|
end
|
31
35
|
|
36
|
+
# Array of all enum id values
|
32
37
|
def ids
|
33
38
|
store.values.map {|v| v[0] }
|
34
39
|
end
|
35
40
|
|
41
|
+
# Array of all enum name values
|
36
42
|
def names
|
37
43
|
store.values.map {|v| v[1] }
|
38
44
|
end
|
39
45
|
|
46
|
+
# Return enum values in an array suitable to pass to a Rails form select helper.
|
40
47
|
def to_select
|
41
48
|
store.values.map {|v| [v[1], v[0]] }
|
42
49
|
end
|
43
50
|
|
51
|
+
# Access id or name value. Pass an id number to retrieve the name or
|
52
|
+
# a symbol or string to retrieve the matching id.
|
44
53
|
def [](index)
|
45
54
|
if index.is_a?(Fixnum)
|
46
55
|
row = store.get_by_id(index)
|
@@ -51,6 +60,7 @@ module ActiveEnum
|
|
51
60
|
end
|
52
61
|
end
|
53
62
|
|
63
|
+
# Access any meta data defined for a given id or name. Returns a hash.
|
54
64
|
def meta(index)
|
55
65
|
row = if index.is_a?(Fixnum)
|
56
66
|
store.get_by_id(index)
|
@@ -13,6 +13,7 @@ module ActiveEnum
|
|
13
13
|
|
14
14
|
# Declare an attribute to be enumerated by an enum class
|
15
15
|
#
|
16
|
+
# Example:
|
16
17
|
# class Person < ActiveRecord::Base
|
17
18
|
# enumerate :sex, :with => Sex
|
18
19
|
# enumerate :sex # implies a Sex enum class exists
|
@@ -26,6 +27,7 @@ module ActiveEnum
|
|
26
27
|
# enumerate :to, :from, :with => Sex
|
27
28
|
#
|
28
29
|
def enumerate(*attributes, &block)
|
30
|
+
self.enumerated_attributes ||= {}
|
29
31
|
options = attributes.extract_options!
|
30
32
|
attributes.each do |attribute|
|
31
33
|
begin
|
@@ -51,11 +53,12 @@ module ActiveEnum
|
|
51
53
|
end
|
52
54
|
|
53
55
|
def active_enum_for(attribute)
|
54
|
-
self.enumerated_attributes[attribute.to_sym]
|
56
|
+
self.enumerated_attributes && self.enumerated_attributes[attribute.to_sym]
|
55
57
|
end
|
56
58
|
|
57
59
|
# Define read method to allow an argument for the enum component
|
58
60
|
#
|
61
|
+
# Examples:
|
59
62
|
# user.sex
|
60
63
|
# user.sex(:id)
|
61
64
|
# user.sex(:name)
|
@@ -87,6 +90,7 @@ module ActiveEnum
|
|
87
90
|
|
88
91
|
# Define write method to also handle enum value
|
89
92
|
#
|
93
|
+
# Examples:
|
90
94
|
# user.sex = 1
|
91
95
|
# user.sex = :male
|
92
96
|
#
|
@@ -105,6 +109,7 @@ module ActiveEnum
|
|
105
109
|
|
106
110
|
# Define question method to check enum value against attribute value
|
107
111
|
#
|
112
|
+
# Example:
|
108
113
|
# user.sex?(:male)
|
109
114
|
#
|
110
115
|
def define_active_enum_question_method(attribute)
|
@@ -1,10 +1,6 @@
|
|
1
1
|
module ActiveEnum
|
2
2
|
module FormHelpers
|
3
3
|
module Formtastic
|
4
|
-
def self.included(base)
|
5
|
-
base.alias_method_chain :default_input_type, :active_enum
|
6
|
-
end
|
7
|
-
|
8
4
|
def default_input_type_with_active_enum(method, options)
|
9
5
|
return :enum if @object.class.respond_to?(:active_enum_for) && @object.class.active_enum_for(method)
|
10
6
|
default_input_type_without_active_enum(method, options)
|
@@ -19,4 +15,7 @@ module ActiveEnum
|
|
19
15
|
end
|
20
16
|
end
|
21
17
|
|
22
|
-
Formtastic::SemanticFormBuilder.
|
18
|
+
Formtastic::SemanticFormBuilder.class_eval do
|
19
|
+
include ActiveEnum::FormHelpers::Formtastic
|
20
|
+
alias_method_chain :default_input_type, :active_enum
|
21
|
+
end
|
@@ -2,24 +2,26 @@ module ActiveEnum
|
|
2
2
|
module FormHelpers
|
3
3
|
module SimpleForm
|
4
4
|
module BuilderExtension
|
5
|
-
def self.included(base)
|
6
|
-
base.alias_method_chain :default_input_type, :active_enum
|
7
|
-
end
|
8
5
|
|
9
|
-
def default_input_type_with_active_enum
|
10
|
-
return :enum if @options[:as].nil? &&
|
11
|
-
|
12
|
-
|
13
|
-
default_input_type_without_active_enum
|
6
|
+
def default_input_type_with_active_enum(*args, &block)
|
7
|
+
return :enum if (args.last.is_a?(Hash) ? args.last[:as] : @options[:as]).nil? &&
|
8
|
+
object.class.respond_to?(:active_enum_for) &&
|
9
|
+
object.class.active_enum_for(args.first || @attribute_name)
|
10
|
+
default_input_type_without_active_enum(*args, &block)
|
14
11
|
end
|
12
|
+
|
15
13
|
end
|
16
14
|
|
17
15
|
class EnumInput < ::SimpleForm::Inputs::CollectionInput
|
18
16
|
|
19
|
-
def initialize(
|
17
|
+
def initialize(*args)
|
20
18
|
super
|
21
19
|
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
|
22
|
-
|
20
|
+
if respond_to?(:input_options)
|
21
|
+
input_options[:collection] = enum.to_select
|
22
|
+
else
|
23
|
+
@builder.options[:collection] = enum.to_select
|
24
|
+
end
|
23
25
|
end
|
24
26
|
|
25
27
|
end
|
@@ -32,4 +34,5 @@ SimpleForm::FormBuilder.class_eval do
|
|
32
34
|
|
33
35
|
map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm::EnumInput
|
34
36
|
alias_method :collection_enum, :collection_select
|
37
|
+
alias_method_chain :default_input_type, :active_enum
|
35
38
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -4,8 +4,8 @@
|
|
4
4
|
|
5
5
|
ActiveEnum.setup do |config|
|
6
6
|
|
7
|
-
# Extend classes to add enumerate method
|
8
|
-
config.extend_classes = [ ActiveRecord::Base ]
|
7
|
+
# Extend classes to add enumerate method
|
8
|
+
# config.extend_classes = [ ActiveRecord::Base ]
|
9
9
|
|
10
10
|
# Return name string as value for attribute method
|
11
11
|
# config.use_name_as_value = false
|
@@ -1,18 +1,18 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
describe ActiveEnum::ActsAsEnum do
|
4
|
+
class Person < ActiveRecord::Base
|
5
|
+
acts_as_enum :name_column => 'first_name'
|
6
|
+
end
|
6
7
|
|
7
|
-
class TestPerson < ActiveRecord::Base
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
class TestPerson < ActiveRecord::Base
|
9
|
+
def self.extended_modules
|
10
|
+
class << self
|
11
|
+
self.included_modules
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
13
|
-
end
|
14
15
|
|
15
|
-
describe ActiveEnum::ActsAsEnum do
|
16
16
|
before(:all) do
|
17
17
|
Person.create!(:first_name => 'Dave', :last_name => 'Smith')
|
18
18
|
Person.create!(:first_name => 'John', :last_name => 'Doe')
|
@@ -42,9 +42,7 @@ describe ActiveEnum::ActsAsEnum do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should return sorted array from order value for select helpers from to_select" do
|
45
|
-
Person.
|
46
|
-
acts_as_enum :name_column => 'first_name', :order => :desc
|
47
|
-
end
|
45
|
+
Person.acts_as_enum :name_column => 'first_name', :order => :desc
|
48
46
|
Person.to_select.should == [['John', 2], ['Dave', 1]]
|
49
47
|
end
|
50
48
|
end
|
@@ -2,13 +2,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
2
2
|
|
3
3
|
describe ActiveEnum::Base do
|
4
4
|
|
5
|
-
|
5
|
+
describe ".store" do
|
6
6
|
it 'should load the storage class instance using the storage setting' do
|
7
7
|
ActiveEnum::Base.send(:store).should be_instance_of(ActiveEnum::Storage::MemoryStore)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
describe ".enum_classes" do
|
12
12
|
it 'should return all enum classes defined use class definition' do
|
13
13
|
ActiveEnum.enum_classes = []
|
14
14
|
class NewEnum < ActiveEnum::Base; end
|
@@ -24,13 +24,21 @@ describe ActiveEnum::Base do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
describe ".all" do
|
28
28
|
it 'should return an empty array when no values defined' do
|
29
29
|
define_enum.all.should == []
|
30
30
|
end
|
31
|
+
|
32
|
+
it 'should return an array of arrays with all values defined as [id, name]' do
|
33
|
+
enum = define_enum do
|
34
|
+
value :name => 'Name 1'
|
35
|
+
value :name => 'Name 2'
|
36
|
+
end
|
37
|
+
enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
|
38
|
+
end
|
31
39
|
end
|
32
40
|
|
33
|
-
|
41
|
+
describe ".value" do
|
34
42
|
it 'should allow me to define a value with an id and name' do
|
35
43
|
enum = define_enum do
|
36
44
|
value :id => 1, :name => 'Name'
|
@@ -86,7 +94,7 @@ describe ActiveEnum::Base do
|
|
86
94
|
end
|
87
95
|
end
|
88
96
|
|
89
|
-
|
97
|
+
describe ".meta" do
|
90
98
|
it 'should return meta values hash for a given index value' do
|
91
99
|
enum = define_enum do
|
92
100
|
value :id => 1, :name => 'Name', :description => 'extra'
|
@@ -132,7 +140,7 @@ describe ActiveEnum::Base do
|
|
132
140
|
end
|
133
141
|
end
|
134
142
|
|
135
|
-
|
143
|
+
describe ".ids" do
|
136
144
|
it 'should return array of ids' do
|
137
145
|
enum = define_enum do
|
138
146
|
value :id => 1, :name => 'Name 1'
|
@@ -142,7 +150,7 @@ describe ActiveEnum::Base do
|
|
142
150
|
end
|
143
151
|
end
|
144
152
|
|
145
|
-
|
153
|
+
describe ".names" do
|
146
154
|
it 'should return array of names' do
|
147
155
|
enum = define_enum do
|
148
156
|
value :id => 1, :name => 'Name 1'
|
@@ -181,7 +189,7 @@ describe ActiveEnum::Base do
|
|
181
189
|
|
182
190
|
end
|
183
191
|
|
184
|
-
|
192
|
+
describe ".to_select" do
|
185
193
|
it 'should return array for select helpers' do
|
186
194
|
enum = define_enum do
|
187
195
|
value :id => 1, :name => 'Name 1'
|
@@ -1,17 +1,16 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
class Sex < ActiveEnum::Base
|
4
|
-
value :id => 1, :name => 'Male'
|
5
|
-
value :id => 2, :name => 'Female'
|
6
|
-
end
|
7
|
-
|
8
|
-
class Accepted < ActiveEnum::Base
|
9
|
-
value :id => 0, :name => 'No'
|
10
|
-
value :id => 1, :name => 'Definitely'
|
11
|
-
value :id => 2, :name => 'Maybe'
|
12
|
-
end
|
13
|
-
|
14
3
|
describe ActiveEnum::Extensions do
|
4
|
+
class Sex < ActiveEnum::Base
|
5
|
+
value :id => 1, :name => 'Male'
|
6
|
+
value :id => 2, :name => 'Female'
|
7
|
+
end
|
8
|
+
|
9
|
+
class Accepted < ActiveEnum::Base
|
10
|
+
value :id => 0, :name => 'No'
|
11
|
+
value :id => 1, :name => 'Definitely'
|
12
|
+
value :id => 2, :name => 'Maybe'
|
13
|
+
end
|
15
14
|
|
16
15
|
it 'should add class :enumerate method to ActiveRecord' do
|
17
16
|
ActiveRecord::Base.should respond_to(:enumerate)
|
@@ -22,109 +21,99 @@ describe ActiveEnum::Extensions do
|
|
22
21
|
end
|
23
22
|
|
24
23
|
it 'should allow multiple attributes to be enumerated with same enum' do
|
25
|
-
Person.
|
26
|
-
enumerate :attending, :staying, :with => Accepted
|
27
|
-
end
|
24
|
+
Person.enumerate :attending, :staying, :with => Accepted
|
28
25
|
Person.active_enum_for(:attending).should == Accepted
|
29
26
|
Person.active_enum_for(:staying).should == Accepted
|
30
27
|
end
|
31
28
|
|
32
29
|
it 'should allow implicit enumeration class from attribute name' do
|
33
|
-
Person.
|
34
|
-
enumerate :sex
|
35
|
-
end
|
30
|
+
Person.enumerate :sex
|
36
31
|
Person.active_enum_for(:sex).should == Sex
|
37
32
|
end
|
38
33
|
|
39
34
|
it 'should create enum namespaced enum class from block' do
|
40
|
-
Person.
|
41
|
-
|
42
|
-
value :id => 1, :name => 'Male'
|
43
|
-
end
|
35
|
+
Person.enumerate :sex do
|
36
|
+
value :id => 1, :name => 'Male'
|
44
37
|
end
|
45
38
|
Person.active_enum_for(:sex).should == ::Person::Sex
|
46
39
|
end
|
47
40
|
|
48
41
|
it 'should raise error if implicit enumeration class cannot be found' do
|
49
|
-
|
50
|
-
Person.
|
51
|
-
|
42
|
+
expect {
|
43
|
+
Person.enumerate :first_name
|
44
|
+
}.should raise_error(ActiveEnum::EnumNotFound)
|
52
45
|
end
|
53
46
|
|
54
|
-
|
47
|
+
context "attribute" do
|
48
|
+
let(:person) { Person.new(:sex => 1) }
|
49
|
+
|
55
50
|
before(:all) do
|
56
51
|
reset_class Person do
|
57
52
|
enumerate :sex, :with => Sex
|
58
53
|
end
|
59
54
|
end
|
60
55
|
|
61
|
-
|
62
|
-
@person = Person.new(:sex =>1)
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "with value" do
|
56
|
+
context "with value" do
|
66
57
|
it 'should return value with no arg' do
|
67
|
-
|
58
|
+
person.sex.should == 1
|
68
59
|
end
|
69
60
|
|
70
61
|
it 'should return enum id for value' do
|
71
|
-
|
62
|
+
person.sex(:id).should == 1
|
72
63
|
end
|
73
64
|
|
74
65
|
it 'should return enum name for value' do
|
75
|
-
|
66
|
+
person.sex(:name).should == 'Male'
|
76
67
|
end
|
77
68
|
|
78
69
|
it 'should return enum class for attribute' do
|
79
|
-
|
70
|
+
person.sex(:enum).should == Sex
|
80
71
|
end
|
81
72
|
end
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
@person.sex = nil
|
86
|
-
end
|
74
|
+
context "with nil value" do
|
75
|
+
let(:person) { Person.new(:sex => nil) }
|
87
76
|
|
88
77
|
it 'should return nil with no arg' do
|
89
|
-
|
78
|
+
person.sex.should be_nil
|
90
79
|
end
|
91
80
|
|
92
81
|
it 'should return nil enum id' do
|
93
|
-
|
82
|
+
person.sex(:id).should be_nil
|
94
83
|
end
|
95
84
|
|
96
85
|
it 'should return nil enum name' do
|
97
|
-
|
86
|
+
person.sex(:name).should be_nil
|
98
87
|
end
|
99
88
|
|
100
89
|
it 'should return enum class for attribute' do
|
101
|
-
|
90
|
+
person.sex(:enum).should == Sex
|
102
91
|
end
|
103
92
|
end
|
104
93
|
|
105
|
-
|
106
|
-
|
107
|
-
@person.sex = -1
|
108
|
-
end
|
94
|
+
context "with undefined value" do
|
95
|
+
let(:person) { Person.new(:sex => -1) }
|
109
96
|
|
110
97
|
it 'should return value with no arg' do
|
111
|
-
|
98
|
+
person.sex.should == -1
|
112
99
|
end
|
113
100
|
|
114
101
|
it 'should return nil enum id' do
|
115
|
-
|
102
|
+
person.sex(:id).should be_nil
|
116
103
|
end
|
117
104
|
|
118
105
|
it 'should return nil enum name' do
|
119
|
-
|
106
|
+
person.sex(:name).should be_nil
|
120
107
|
end
|
121
108
|
|
122
109
|
it 'should return enum class for attribute' do
|
123
|
-
|
110
|
+
person.sex(:enum).should == Sex
|
124
111
|
end
|
125
112
|
end
|
126
113
|
|
127
|
-
|
114
|
+
context "with meta data" do
|
115
|
+
let(:person) { Person.new(:sex =>1) }
|
116
|
+
|
128
117
|
before(:all) do
|
129
118
|
reset_class Person do
|
130
119
|
enumerate :sex do
|
@@ -134,84 +123,72 @@ describe ActiveEnum::Extensions do
|
|
134
123
|
end
|
135
124
|
end
|
136
125
|
|
137
|
-
before do
|
138
|
-
@person = Person.new(:sex =>1)
|
139
|
-
end
|
140
|
-
|
141
126
|
it 'should return meta value for existing key' do
|
142
|
-
|
127
|
+
person.sex(:description).should == 'Man'
|
143
128
|
end
|
144
129
|
|
145
130
|
it 'should return nil for missing meta value' do
|
146
|
-
|
131
|
+
person.sex(:nonexistent).should be_nil
|
147
132
|
end
|
148
133
|
|
149
134
|
it 'should return nil for missing index' do
|
150
|
-
|
151
|
-
|
135
|
+
person.sex = nil
|
136
|
+
person.sex(:description).should be_nil
|
152
137
|
end
|
153
138
|
end
|
154
139
|
|
155
|
-
|
156
|
-
before do
|
157
|
-
@person.sex = 1
|
158
|
-
end
|
159
|
-
|
140
|
+
context "question method" do
|
160
141
|
it 'should return normal value without arg' do
|
161
|
-
|
162
|
-
|
163
|
-
|
142
|
+
person.sex?.should be_true
|
143
|
+
person.sex = nil
|
144
|
+
person.sex?.should be_false
|
164
145
|
end
|
165
146
|
|
166
147
|
it 'should return true if string name matches for id value' do
|
167
|
-
|
148
|
+
person.sex?("Male").should be_true
|
168
149
|
end
|
169
150
|
|
170
151
|
it 'should return true if symbol name matches for id value' do
|
171
|
-
|
172
|
-
|
152
|
+
person.sex?(:male).should be_true
|
153
|
+
person.sex?(:Male).should be_true
|
173
154
|
end
|
174
155
|
|
175
156
|
it 'should return false if name does not match for id value' do
|
176
|
-
|
177
|
-
|
178
|
-
|
157
|
+
person.sex?("Female").should be_false
|
158
|
+
person.sex?(:female).should be_false
|
159
|
+
person.sex?(:Female).should be_false
|
179
160
|
end
|
180
161
|
end
|
181
162
|
|
182
|
-
|
163
|
+
context "with value as enum name symbol" do
|
183
164
|
|
184
165
|
it 'should store id value when valid enum name' do
|
185
|
-
|
186
|
-
|
166
|
+
person.sex = :female
|
167
|
+
person.sex.should == 2
|
187
168
|
end
|
188
169
|
|
189
170
|
it 'should store nil value when invalid enum name' do
|
190
|
-
|
191
|
-
|
171
|
+
person.sex = :invalid
|
172
|
+
person.sex.should == nil
|
192
173
|
end
|
193
174
|
|
194
175
|
end
|
195
176
|
|
196
|
-
|
197
|
-
before(:all)
|
198
|
-
|
199
|
-
end
|
177
|
+
context "with value as enum name" do
|
178
|
+
before(:all) { ActiveEnum.use_name_as_value = true }
|
179
|
+
let(:person) { Person.new(:sex =>1) }
|
200
180
|
|
201
|
-
before do
|
181
|
+
before do
|
202
182
|
reset_class Person do
|
203
183
|
enumerate :sex, :with => Sex
|
204
184
|
end
|
205
|
-
|
206
|
-
end
|
185
|
+
end
|
207
186
|
|
208
187
|
it 'should return text name value for attribute' do
|
209
|
-
|
188
|
+
person.sex.should == 'Male'
|
210
189
|
end
|
211
190
|
|
212
|
-
after(:all)
|
213
|
-
ActiveEnum.use_name_as_value = false
|
214
|
-
end
|
191
|
+
after(:all) { ActiveEnum.use_name_as_value = false }
|
215
192
|
end
|
216
193
|
|
217
194
|
end
|
@@ -3,8 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|
3
3
|
require 'formtastic'
|
4
4
|
require 'active_enum/form_helpers/formtastic'
|
5
5
|
|
6
|
-
describe
|
7
|
-
include RSpec::Rails::HelperExampleGroup
|
6
|
+
describe ActiveEnum::FormHelpers::Formtastic, :type => :helper do
|
8
7
|
include Formtastic::SemanticFormHelper
|
9
8
|
|
10
9
|
before do
|
@@ -25,6 +24,15 @@ describe 'ActiveEnum::FormHelpers::Formtastic' do
|
|
25
24
|
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
26
25
|
end
|
27
26
|
|
27
|
+
it "should use explicit :enum input type" do
|
28
|
+
output = semantic_form_for(Person.new, :url => people_path) do |f|
|
29
|
+
concat f.input(:sex, :as => :enum)
|
30
|
+
end
|
31
|
+
output.should have_selector('select#person_sex')
|
32
|
+
output.should have_xpath('//option[@value=1]', :content => 'Male')
|
33
|
+
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
34
|
+
end
|
35
|
+
|
28
36
|
it "should not use enum input type if :as option indicates other type" do
|
29
37
|
output = semantic_form_for(Person.new, :url => people_path) do |f|
|
30
38
|
concat f.input(:sex, :as => :string)
|
@@ -33,18 +41,27 @@ describe 'ActiveEnum::FormHelpers::Formtastic' do
|
|
33
41
|
end
|
34
42
|
|
35
43
|
it "should raise error if attribute for enum input is not enumerated" do
|
36
|
-
|
37
|
-
semantic_form_for(Person.new, :url => people_path)
|
44
|
+
expect {
|
45
|
+
semantic_form_for(Person.new, :url => people_path) do |f|
|
46
|
+
f.input(:attending, :as => :enum)
|
47
|
+
end
|
38
48
|
}.should raise_error "Attribute 'attending' has no enum class"
|
39
49
|
end
|
40
50
|
|
41
51
|
it "should not use enum input type if class does not support ActiveEnum" do
|
42
|
-
output = semantic_form_for(
|
52
|
+
output = semantic_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
|
43
53
|
concat f.input(:name)
|
44
54
|
end
|
45
55
|
output.should have_selector('input#not_active_record_name')
|
46
56
|
end
|
47
57
|
|
58
|
+
it "should allow non-enum fields to use default input determination" do
|
59
|
+
output = semantic_form_for(Person.new, :url => people_path) do |f|
|
60
|
+
concat f.input(:first_name)
|
61
|
+
end
|
62
|
+
output.should have_selector('input#person_first_name')
|
63
|
+
end
|
64
|
+
|
48
65
|
def people_path
|
49
66
|
'/people'
|
50
67
|
end
|
@@ -3,8 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|
3
3
|
require 'simple_form'
|
4
4
|
require 'active_enum/form_helpers/simple_form'
|
5
5
|
|
6
|
-
describe
|
7
|
-
include RSpec::Rails::HelperExampleGroup
|
6
|
+
describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
|
8
7
|
include SimpleForm::ActionViewExtensions::FormHelper
|
9
8
|
|
10
9
|
before do
|
@@ -26,6 +25,15 @@ describe 'ActiveEnum::FormHelpers::Simple' do
|
|
26
25
|
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
27
26
|
end
|
28
27
|
|
28
|
+
it "should use explicit :enum input type" do
|
29
|
+
output = simple_form_for(Person.new, :url => people_path) do |f|
|
30
|
+
concat f.input(:sex, :as => :enum)
|
31
|
+
end
|
32
|
+
output.should have_selector('select#person_sex')
|
33
|
+
output.should have_xpath('//option[@value=1]', :content => 'Male')
|
34
|
+
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
35
|
+
end
|
36
|
+
|
29
37
|
it "should not use enum input type if :as option indicates other type" do
|
30
38
|
output = simple_form_for(Person.new, :url => people_path) do |f|
|
31
39
|
concat f.input(:sex, :as => :string)
|
@@ -34,18 +42,27 @@ describe 'ActiveEnum::FormHelpers::Simple' do
|
|
34
42
|
end
|
35
43
|
|
36
44
|
it "should raise error if attribute for enum input is not enumerated" do
|
37
|
-
|
38
|
-
simple_form_for(Person.new, :url => people_path)
|
39
|
-
|
45
|
+
expect {
|
46
|
+
simple_form_for(Person.new, :url => people_path) do |f|
|
47
|
+
f.input(:attending, :as => :enum)
|
48
|
+
end
|
49
|
+
}.should raise_error "Attribute 'attending' has no enum class"
|
40
50
|
end
|
41
51
|
|
42
52
|
it "should not use enum input type if class does not support ActiveEnum" do
|
43
|
-
output = simple_form_for(
|
53
|
+
output = simple_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
|
44
54
|
concat f.input(:name)
|
45
55
|
end
|
46
56
|
output.should have_selector('input#not_active_record_name')
|
47
57
|
end
|
48
58
|
|
59
|
+
it "should allow non-enum fields to use default input determination" do
|
60
|
+
output = simple_form_for(Person.new, :url => people_path) do |f|
|
61
|
+
concat f.input(:first_name)
|
62
|
+
end
|
63
|
+
output.should have_selector('input#person_first_name')
|
64
|
+
end
|
65
|
+
|
49
66
|
def people_path
|
50
67
|
'/people'
|
51
68
|
end
|
@@ -6,7 +6,7 @@ class TestOtherAREnum < ActiveEnum::Base; end
|
|
6
6
|
describe ActiveEnum::Storage::MemoryStore do
|
7
7
|
attr_accessor :store
|
8
8
|
|
9
|
-
|
9
|
+
describe '#set' do
|
10
10
|
it 'should store value of id and name' do
|
11
11
|
store.set 1, 'test name'
|
12
12
|
store.values.should == [[1, 'test name']]
|
@@ -18,28 +18,28 @@ describe ActiveEnum::Storage::MemoryStore do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'should raise error if duplicate id' do
|
21
|
-
|
21
|
+
expect {
|
22
22
|
store.set 1, 'Name 1'
|
23
23
|
store.set 1, 'Other Name'
|
24
24
|
}.should raise_error(ActiveEnum::DuplicateValue)
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'should raise error if duplicate name' do
|
28
|
-
|
28
|
+
expect {
|
29
29
|
store.set 1, 'Name 1'
|
30
30
|
store.set 2, 'Name 1'
|
31
31
|
}.should raise_error(ActiveEnum::DuplicateValue)
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should raise error if duplicate name matches title-case name' do
|
35
|
-
|
35
|
+
expect {
|
36
36
|
store.set 1, 'Name 1'
|
37
37
|
store.set 2, 'name 1'
|
38
38
|
}.should raise_error(ActiveEnum::DuplicateValue)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
|
42
|
+
describe "#values" do
|
43
43
|
it 'should return array of stored values' do
|
44
44
|
store.set 1, 'Name 1'
|
45
45
|
store.values.should == [[1, 'Name 1']]
|
@@ -52,7 +52,7 @@ describe ActiveEnum::Storage::MemoryStore do
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
|
55
|
+
describe "#get_by_id" do
|
56
56
|
it 'should return the value for a given id' do
|
57
57
|
store.set 1, 'test name'
|
58
58
|
store.get_by_id(1).should == [1, 'test name']
|
@@ -63,7 +63,7 @@ describe ActiveEnum::Storage::MemoryStore do
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
|
66
|
+
describe "#get_by_name" do
|
67
67
|
it 'should return the value for a given name' do
|
68
68
|
store.set 1, 'test name'
|
69
69
|
store.get_by_name('test name').should == [1, 'test name']
|
@@ -79,7 +79,7 @@ describe ActiveEnum::Storage::MemoryStore do
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
|
82
|
+
describe "#sort!" do
|
83
83
|
it 'should sort values ascending when passed :asc' do
|
84
84
|
@order = :asc
|
85
85
|
store.set 2, 'Name 2'
|
data/spec/active_enum_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe ActiveEnum do
|
4
4
|
|
5
|
-
|
5
|
+
describe ".define" do
|
6
6
|
it 'should define enum constants using block' do
|
7
7
|
ActiveEnum.define do
|
8
8
|
enum(:foo) do
|
@@ -18,7 +18,7 @@ describe "Bulk enum definitions" do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
describe ".setup" do
|
22
22
|
before :all do
|
23
23
|
@original = ActiveEnum.use_name_as_value
|
24
24
|
end
|
@@ -34,9 +34,8 @@ describe "Bulk enum definitions" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
describe ".extend_classes" do
|
38
38
|
it 'should add enumerate extensions to given classes' do
|
39
|
-
ActiveEnum.extend_classes = [ActiveRecord::Base, NotActiveRecord]
|
40
39
|
ActiveRecord::Base.should respond_to(:enumerate)
|
41
40
|
NotActiveRecord.should respond_to(:enumerate)
|
42
41
|
end
|
data/spec/schema.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
ActiveRecord::Schema.define(:version => 1) do
|
2
|
-
|
3
2
|
create_table :people, :force => true do |t|
|
4
3
|
t.string :first_name
|
5
4
|
t.string :last_name
|
@@ -11,16 +10,4 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
11
10
|
create_table :sexes, :force => true do |t|
|
12
11
|
t.string :name
|
13
12
|
end
|
14
|
-
|
15
|
-
create_table :enums, :force => true do |t|
|
16
|
-
t.integer :enum_id
|
17
|
-
t.string :name
|
18
|
-
t.string :enum_type
|
19
|
-
t.string :meta
|
20
|
-
t.datetime :modified_at
|
21
|
-
end
|
22
|
-
add_index :enums, [:enum_id, :enum_type], :unique => true
|
23
|
-
add_index :enums, [:name, :enum_type]
|
24
|
-
add_index :enums, [:modified_at, :enum_type]
|
25
|
-
|
26
13
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
|
2
|
-
$:.unshift(File.join(File.dirname(__FILE__), '..', 'spec'))
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'rspec/autorun'
|
1
|
+
require 'rspec'
|
6
2
|
|
3
|
+
require 'rails'
|
7
4
|
require 'active_record'
|
8
5
|
require 'action_controller'
|
9
6
|
require 'action_view'
|
@@ -11,15 +8,19 @@ require 'action_mailer'
|
|
11
8
|
|
12
9
|
require 'active_enum'
|
13
10
|
|
14
|
-
|
11
|
+
module Config
|
12
|
+
class Application < Rails::Application
|
13
|
+
config.generators do |g|
|
14
|
+
g.orm :active_record
|
15
|
+
g.test_framework :rspec, :fixture => false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
15
19
|
|
16
20
|
require 'rspec/rails'
|
17
21
|
|
18
22
|
ActiveRecord::Migration.verbose = false
|
19
23
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
20
|
-
ActiveRecord::Base.logger = Logger.new('/dev/null')
|
21
|
-
|
22
|
-
ActiveEnum.extend_classes = [ActiveRecord::Base]
|
23
24
|
|
24
25
|
require 'schema'
|
25
26
|
|
@@ -30,6 +31,8 @@ class NotActiveRecord
|
|
30
31
|
attr_accessor :name
|
31
32
|
end
|
32
33
|
|
34
|
+
ActiveEnum.extend_classes = [ActiveRecord::Base, NotActiveRecord]
|
35
|
+
|
33
36
|
module SpecHelper
|
34
37
|
def reset_class(klass, &block)
|
35
38
|
name = klass.name.to_sym
|
metadata
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 57
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 1
|
10
|
+
version: 0.9.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
14
|
-
autorequire:
|
14
|
+
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-12 00:00:00 +11:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -39,7 +39,6 @@ files:
|
|
39
39
|
- lib/active_enum/form_helpers/formtastic.rb
|
40
40
|
- lib/active_enum/form_helpers/simple_form.rb
|
41
41
|
- lib/active_enum/storage/abstract_store.rb
|
42
|
-
- lib/active_enum/storage/active_record_store.rb
|
43
42
|
- lib/active_enum/storage/memory_store.rb
|
44
43
|
- lib/active_enum/version.rb
|
45
44
|
- lib/active_enum.rb
|
@@ -50,7 +49,6 @@ files:
|
|
50
49
|
- spec/active_enum/extensions_spec.rb
|
51
50
|
- spec/active_enum/form_helpers/formtastic_spec.rb
|
52
51
|
- spec/active_enum/form_helpers/simple_form_spec.rb
|
53
|
-
- spec/active_enum/storage/active_record_store_spec.rb
|
54
52
|
- spec/active_enum/storage/memory_store_spec.rb
|
55
53
|
- spec/active_enum_spec.rb
|
56
54
|
- spec/schema.rb
|
@@ -1,64 +0,0 @@
|
|
1
|
-
module ActiveEnum
|
2
|
-
class Model < ActiveRecord::Base
|
3
|
-
set_table_name 'enums'
|
4
|
-
serialize :meta, Hash
|
5
|
-
end
|
6
|
-
|
7
|
-
module Storage
|
8
|
-
class ActiveRecordStore < AbstractStore
|
9
|
-
def initialize(enum_class, order)
|
10
|
-
super
|
11
|
-
@values = values_from_db
|
12
|
-
end
|
13
|
-
|
14
|
-
def set(id, name)
|
15
|
-
check_duplicate id, name
|
16
|
-
values << [id, name]
|
17
|
-
sort!
|
18
|
-
end
|
19
|
-
|
20
|
-
def get_by_id(id)
|
21
|
-
values.assoc(id)
|
22
|
-
end
|
23
|
-
|
24
|
-
def get_by_name(name)
|
25
|
-
values.rassoc(name.to_s) || values.rassoc(name.to_s.titleize)
|
26
|
-
end
|
27
|
-
|
28
|
-
def values
|
29
|
-
@values
|
30
|
-
end
|
31
|
-
|
32
|
-
def check_duplicate(id, name)
|
33
|
-
if get_by_id(id) || get_by_name(name)
|
34
|
-
raise ActiveEnum::DuplicateValue
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def sort!
|
39
|
-
return if @order == :as_defined
|
40
|
-
case @order
|
41
|
-
when :asc
|
42
|
-
values.sort! {|a,b| a[0] <=> b[0] }
|
43
|
-
when :desc
|
44
|
-
values.sort! {|a,b| b[0] <=> a[0] }
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def order_clause
|
49
|
-
@order_clause ||= case @order
|
50
|
-
when :asc
|
51
|
-
'enum_id ASC'
|
52
|
-
when :desc
|
53
|
-
'enum_id DESC'
|
54
|
-
else
|
55
|
-
'modified_at ASC'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def values_from_db
|
60
|
-
ActiveEnum::Model.all(:conditions => {:enum_type => @enum.to_s}, :order => order_clause).map {|r| [r.enum_id, r.name] }
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,133 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
require 'active_enum/storage/active_record_store'
|
3
|
-
|
4
|
-
class TestARStoreEnum < ActiveEnum::Base; end
|
5
|
-
class TestOtherAREnum < ActiveEnum::Base; end
|
6
|
-
|
7
|
-
describe ActiveEnum::Storage::ActiveRecordStore do
|
8
|
-
attr_accessor :store
|
9
|
-
|
10
|
-
context '#set' do
|
11
|
-
it 'should store value of id and name' do
|
12
|
-
store.set 1, 'Name 1'
|
13
|
-
store.values.should == [[1, 'Name 1']]
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should raise error if duplicate id' do
|
17
|
-
lambda {
|
18
|
-
store.set 1, 'Name 1'
|
19
|
-
store.set 1, 'Other Name'
|
20
|
-
}.should raise_error(ActiveEnum::DuplicateValue)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should raise error if duplicate name' do
|
24
|
-
lambda {
|
25
|
-
store.set 1, 'Name 1'
|
26
|
-
store.set 2, 'Name 1'
|
27
|
-
}.should raise_error(ActiveEnum::DuplicateValue)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'should raise error if duplicate name matches title-case name' do
|
31
|
-
lambda {
|
32
|
-
store.set 1, 'Name 1'
|
33
|
-
store.set 2, 'name 1'
|
34
|
-
}.should raise_error(ActiveEnum::DuplicateValue)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should create a record for values' do
|
38
|
-
store.set 1, 'Name 1'
|
39
|
-
ActiveEnum::Model.count.should == 1
|
40
|
-
r = ActiveEnum::Model.first
|
41
|
-
r.enum_id.should == 1
|
42
|
-
r.name.should == 'Name 1'
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "#values" do
|
47
|
-
it 'should return array of stored values' do
|
48
|
-
store.set 1, 'Name 1'
|
49
|
-
store.values.should == [[1, 'Name 1']]
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'should return values for set enum only' do
|
53
|
-
alt_store.set 1, 'Other Name 1'
|
54
|
-
store.set 1, 'Name 1'
|
55
|
-
store.values.should == [[1, 'Name 1']]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context "#get_by_id" do
|
60
|
-
it 'should return the value for a given id' do
|
61
|
-
store.set 1, 'Name 1'
|
62
|
-
store.get_by_id(1).should == [1, 'Name 1']
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should return nil when id not found' do
|
66
|
-
store.get_by_id(1).should be_nil
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should return value for correct enum' do
|
70
|
-
alt_store.set 1, 'Other Name 1'
|
71
|
-
store.set 1, 'Name 1'
|
72
|
-
store.get_by_id(1).should == [1, 'Name 1']
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context "#get_by_name" do
|
77
|
-
it 'should return the value for a given name' do
|
78
|
-
store.set 1, 'Name 1'
|
79
|
-
store.get_by_name('Name 1').should == [1, 'Name 1']
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'should return the value with title-cased name for a given lowercase name' do
|
83
|
-
store.set 1, 'Name 1'
|
84
|
-
store.get_by_name('name 1').should == [1, 'Name 1']
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'should return nil when name not found' do
|
88
|
-
store.get_by_name('test name').should be_nil
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'should return value for correct enum' do
|
92
|
-
alt_store.set 1, 'Other Name 1'
|
93
|
-
store.set 1, 'Name 1'
|
94
|
-
store.get_by_name('Name 1').should == [1, 'Name 1']
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context "sort!" do
|
99
|
-
it 'should sort values ascending when passed :asc' do
|
100
|
-
@order = :asc
|
101
|
-
store.set 2, 'Name 2'
|
102
|
-
store.set 1, 'Name 1'
|
103
|
-
store.values.should == [[1,'Name 1'], [2, 'Name 2']]
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should sort values descending when passed :desc' do
|
107
|
-
@order = :desc
|
108
|
-
store.set 1, 'Name 1'
|
109
|
-
store.set 2, 'Name 2'
|
110
|
-
store.values.should == [[2, 'Name 2'], [1,'Name 1']]
|
111
|
-
end
|
112
|
-
|
113
|
-
it 'should not sort values when passed :as_defined' do
|
114
|
-
@order = :as_defined
|
115
|
-
store.set 1, 'Name 1'
|
116
|
-
store.set 3, 'Name 3'
|
117
|
-
store.set 2, 'Name 2'
|
118
|
-
store.values.should == [[1,'Name 1'], [3,'Name 3'], [2, 'Name 2']]
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
after do
|
123
|
-
ActiveEnum::Model.delete_all
|
124
|
-
end
|
125
|
-
|
126
|
-
def store
|
127
|
-
@store ||= ActiveEnum::Storage::ActiveRecordStore.new(TestARStoreEnum, @order)
|
128
|
-
end
|
129
|
-
|
130
|
-
def alt_store
|
131
|
-
@alt_store ||= ActiveEnum::Storage::ActiveRecordStore.new(TestOtherAREnum, :asc)
|
132
|
-
end
|
133
|
-
end
|