active_enum 1.0.0.rc1 → 1.0.0.rc6
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 +5 -5
- data/.travis.yml +7 -7
- data/README.rdoc +1 -1
- data/Rakefile +0 -5
- data/gemfiles/rails_4_2.gemfile +16 -0
- data/gemfiles/rails_5_0.gemfile +16 -0
- data/gemfiles/rails_5_1.gemfile +16 -0
- data/gemfiles/rails_5_2.gemfile +16 -0
- data/lib/active_enum/acts_as_enum.rb +36 -18
- data/lib/active_enum/base.rb +23 -14
- data/lib/active_enum/extensions.rb +2 -2
- data/lib/active_enum/form_helpers/simple_form.rb +13 -1
- data/lib/active_enum/storage/i18n_store.rb +1 -1
- data/lib/active_enum/storage/memory_store.rb +1 -1
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/acts_as_enum_spec.rb +45 -11
- data/spec/active_enum/base_spec.rb +37 -11
- data/spec/active_enum/form_helpers/simple_form_spec.rb +19 -0
- data/spec/support/schema.rb +1 -0
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6fb4a88a4e4c0b8894a94b35dee75c23f9ddbdba0db2c5078dd51049cbeb82a
|
4
|
+
data.tar.gz: 53e5d91603101b6c0e6590ceb58f6c214cbd221be7da6e4780825b51e71b640f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b208b4ad29373d4f604673eef17315d5ede6265384d0cb3cadda81b674953fa35d09ac8149a2831a257d2f4e05e9931b02c01767aece9b2a7e45e4152f09e462
|
7
|
+
data.tar.gz: 8ef7bde84f962ac14307dccfee60849d022699c2841fabd7fe33cb5b4c284cafcff938e91497a6f44f02df27fc0cd0d7bbf707162b16663df30ad61d63688f08
|
data/.travis.yml
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
2
|
-
-
|
3
|
-
-
|
4
|
-
-
|
5
|
-
-
|
1
|
+
gemfile:
|
2
|
+
- gemfiles/rails_4_2.gemfile
|
3
|
+
- gemfiles/rails_5_0.gemfile
|
4
|
+
- gemfiles/rails_5_1.gemfile
|
5
|
+
- gemfiles/rails_5_2.gemfile
|
6
6
|
rvm:
|
7
|
-
-
|
8
|
-
- 2.
|
7
|
+
- 2.5.8
|
8
|
+
- 2.6.6
|
data/README.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 4.2.11"
|
6
|
+
|
7
|
+
gem 'sqlite3', '~> 1.3.0'
|
8
|
+
|
9
|
+
gem 'rake'
|
10
|
+
gem 'simple_form', '~> 3.0'
|
11
|
+
gem 'byebug'
|
12
|
+
gem 'rspec', '~> 3.4'
|
13
|
+
gem 'rspec-rails', '~> 3.4'
|
14
|
+
gem 'capybara'
|
15
|
+
|
16
|
+
gemspec path: "../"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 5.1.0"
|
6
|
+
|
7
|
+
gem 'sqlite3', '~> 1.3.0'
|
8
|
+
|
9
|
+
gem 'rake'
|
10
|
+
gem 'simple_form', '~> 5.0'
|
11
|
+
gem 'byebug'
|
12
|
+
gem 'rspec', '~> 3.4'
|
13
|
+
gem 'rspec-rails', '~> 3.4'
|
14
|
+
gem 'capybara'
|
15
|
+
|
16
|
+
gemspec path: "../"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 5.1.0"
|
6
|
+
|
7
|
+
gem 'sqlite3', '~> 1.3.0'
|
8
|
+
|
9
|
+
gem 'rake'
|
10
|
+
gem 'simple_form', '~> 5.0'
|
11
|
+
gem 'byebug'
|
12
|
+
gem 'rspec', '~> 3.4'
|
13
|
+
gem 'rspec-rails', '~> 3.4'
|
14
|
+
gem 'capybara'
|
15
|
+
|
16
|
+
gemspec path: "../"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 5.2.0"
|
6
|
+
|
7
|
+
gem 'sqlite3', '~> 1.3.0'
|
8
|
+
|
9
|
+
gem 'rake'
|
10
|
+
gem 'simple_form', '~> 5.0'
|
11
|
+
gem 'byebug'
|
12
|
+
gem 'rspec', '~> 3.4'
|
13
|
+
gem 'rspec-rails', '~> 3.4'
|
14
|
+
gem 'capybara'
|
15
|
+
|
16
|
+
gemspec path: "../"
|
@@ -7,47 +7,65 @@ 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, proc { select("#{primary_key}, #{active_enum_options[:name_column]}").
|
10
|
+
scope :enum_values, proc { select(Arel.sql("#{primary_key}, #{active_enum_options[:name_column]}")).
|
11
11
|
where(active_enum_options[:conditions]).
|
12
|
-
order("#{primary_key} #{active_enum_options[:order]}") }
|
12
|
+
order(Arel.sql("#{primary_key} #{active_enum_options[:order]}")) }
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
16
16
|
|
17
17
|
module ClassMethods
|
18
18
|
|
19
|
+
def values
|
20
|
+
enum_values.map { |v| [ v.id, v.send(active_enum_options[:name_column]) ] }
|
21
|
+
end
|
22
|
+
|
19
23
|
def ids
|
20
|
-
enum_values.map {|v| v.id }
|
24
|
+
enum_values.map { |v| v.id }
|
21
25
|
end
|
22
26
|
|
23
27
|
def names
|
24
|
-
enum_values.map {|v| v.send(active_enum_options[:name_column]) }
|
28
|
+
enum_values.map { |v| v.send(active_enum_options[:name_column]) }
|
25
29
|
end
|
26
30
|
|
27
31
|
def to_select
|
28
|
-
enum_values.map {|v| [v.send(active_enum_options[:name_column]), v.id] }
|
32
|
+
enum_values.map { |v| [v.send(active_enum_options[:name_column]), v.id] }
|
29
33
|
end
|
30
34
|
|
31
35
|
def [](index)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
else
|
36
|
-
v = lookup_by_name(index)
|
37
|
-
v.id unless v.blank?
|
38
|
-
end
|
36
|
+
row = get_value(index)
|
37
|
+
return if row.nil?
|
38
|
+
index.is_a?(Integer) ? row.send(active_enum_options[:name_column]) : row.id
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
# Access any meta data defined for a given id or name. Returns a hash.
|
42
|
+
def meta(index)
|
43
|
+
row = lookup_relation(index).unscope(:select).first
|
44
|
+
row&.attributes.except(primary_key.to_s, active_enum_options[:name_column].to_s)
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
|
47
|
+
# Enables use as a delimiter in inclusion validation
|
48
|
+
def include?(value)
|
49
|
+
return super if value.is_a?(Module)
|
50
|
+
|
51
|
+
!self[value].nil?
|
49
52
|
end
|
50
53
|
|
54
|
+
private
|
55
|
+
|
56
|
+
def get_value(index)
|
57
|
+
lookup_relation(index).first || begin
|
58
|
+
raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if ActiveEnum.raise_on_not_found
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def lookup_relation(index)
|
63
|
+
if index.is_a?(Integer)
|
64
|
+
enum_values.where(id: index)
|
65
|
+
else
|
66
|
+
enum_values.where("lower(#{active_enum_options[:name_column]}) = lower(?)", Arel.sql(index.to_s))
|
67
|
+
end
|
68
|
+
end
|
51
69
|
end
|
52
70
|
|
53
71
|
end
|
data/lib/active_enum/base.rb
CHANGED
@@ -20,7 +20,7 @@ module ActiveEnum
|
|
20
20
|
# value 1 => 'Foo'
|
21
21
|
#
|
22
22
|
def value(enum_value)
|
23
|
-
store.set
|
23
|
+
store.set(*id_and_name_and_meta(enum_value))
|
24
24
|
end
|
25
25
|
|
26
26
|
# Specify order enum values are returned.
|
@@ -34,9 +34,11 @@ module ActiveEnum
|
|
34
34
|
@order = order
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
# Array of arrays of stored values defined id, name, meta values hash
|
38
|
+
def values
|
38
39
|
store.values
|
39
40
|
end
|
41
|
+
alias_method :all, :values
|
40
42
|
|
41
43
|
def each(&block)
|
42
44
|
all.each(&block)
|
@@ -44,17 +46,24 @@ module ActiveEnum
|
|
44
46
|
|
45
47
|
# Array of all enum id values
|
46
48
|
def ids
|
47
|
-
store.values.map {|v| v[0] }
|
49
|
+
store.values.map { |v| v[0] }
|
48
50
|
end
|
49
51
|
|
50
52
|
# Array of all enum name values
|
51
53
|
def names
|
52
|
-
store.values.map {|v| v[1] }
|
54
|
+
store.values.map { |v| v[1] }
|
53
55
|
end
|
54
56
|
|
55
57
|
# Return enum values in an array suitable to pass to a Rails form select helper.
|
56
58
|
def to_select
|
57
|
-
store.values.map {|v| [v[1], v[0]] }
|
59
|
+
store.values.map { |v| [v[1].html_safe, v[0]] }
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
|
63
|
+
def to_grouped_select(group_by)
|
64
|
+
store.values.group_by { |(_id, _name, meta)| (meta || {})[group_by] }.map { |group, collection|
|
65
|
+
[ group, collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
|
66
|
+
}
|
58
67
|
end
|
59
68
|
|
60
69
|
# Access id or name value. Pass an id number to retrieve the name or
|
@@ -66,15 +75,6 @@ module ActiveEnum
|
|
66
75
|
end
|
67
76
|
alias_method :[], :get
|
68
77
|
|
69
|
-
# Access value row array for a given id or name value.
|
70
|
-
def get_value(index)
|
71
|
-
if index.is_a?(Integer)
|
72
|
-
store.get_by_id(index)
|
73
|
-
else
|
74
|
-
store.get_by_name(index)
|
75
|
-
end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
76
|
-
end
|
77
|
-
|
78
78
|
def include?(value)
|
79
79
|
!get(value).nil?
|
80
80
|
end
|
@@ -87,6 +87,15 @@ module ActiveEnum
|
|
87
87
|
|
88
88
|
private
|
89
89
|
|
90
|
+
# Access value row array for a given id or name value.
|
91
|
+
def get_value(index)
|
92
|
+
if index.is_a?(Integer)
|
93
|
+
store.get_by_id(index)
|
94
|
+
else
|
95
|
+
store.get_by_name(index)
|
96
|
+
end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
97
|
+
end
|
98
|
+
|
90
99
|
def id_and_name_and_meta(hash)
|
91
100
|
if hash.has_key?(:name)
|
92
101
|
id = hash.delete(:id) || next_id
|
@@ -64,7 +64,7 @@ module ActiveEnum
|
|
64
64
|
enum_class_name = "#{name}::#{attribute.to_s.camelize}"
|
65
65
|
eval("class #{enum_class_name} < ActiveEnum::Base; end")
|
66
66
|
enum = enum_class_name.constantize
|
67
|
-
enum.class_eval
|
67
|
+
enum.class_eval(&block)
|
68
68
|
enum
|
69
69
|
end
|
70
70
|
|
@@ -112,7 +112,7 @@ module ActiveEnum
|
|
112
112
|
if arg.is_a?(Symbol)
|
113
113
|
super self.class.active_enum_for(:#{attribute})[arg]
|
114
114
|
else
|
115
|
-
super
|
115
|
+
super
|
116
116
|
end
|
117
117
|
end
|
118
118
|
DEF
|
@@ -23,9 +23,21 @@ class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::Col
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
class ActiveEnum::FormHelpers::SimpleForm::GroupedEnumInput < ::SimpleForm::Inputs::GroupedCollectionSelectInput
|
27
|
+
def initialize(*args)
|
28
|
+
super
|
29
|
+
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
|
30
|
+
|
31
|
+
input_options[:collection] = enum.to_grouped_select(input_options[:group_by])
|
32
|
+
input_options[:group_method] = :last
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
26
36
|
SimpleForm::FormBuilder.class_eval do
|
27
37
|
prepend ActiveEnum::FormHelpers::SimpleForm::BuilderExtension
|
28
38
|
|
29
|
-
map_type :enum, :
|
39
|
+
map_type :enum, to: ActiveEnum::FormHelpers::SimpleForm::EnumInput
|
40
|
+
map_type :grouped_enum, to: ActiveEnum::FormHelpers::SimpleForm::GroupedEnumInput
|
41
|
+
|
30
42
|
alias_method :collection_enum, :collection_select
|
31
43
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -30,24 +30,58 @@ describe ActiveEnum::ActsAsEnum do
|
|
30
30
|
expect(TestPerson.extended_modules).to include(ActiveEnum::ActsAsEnum::ClassMethods)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
|
33
|
+
context "#[]" do
|
34
|
+
it "should return name column value when passing id to [] method" do
|
35
|
+
expect(Person[1]).to eq('Dave')
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return id column value when passing string name to [] method" do
|
39
|
+
expect(Person['Dave']).to eq(1)
|
40
|
+
expect(Person['dave']).to eq(1)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return id column value when passing symbol name to [] method" do
|
44
|
+
expect(Person[:dave]).to eq(1)
|
45
|
+
end
|
35
46
|
end
|
36
47
|
|
37
|
-
|
38
|
-
|
39
|
-
|
48
|
+
context '#values' do
|
49
|
+
it "should return array of arrays containing id and name column values" do
|
50
|
+
expect(Person.values).to eq([[1, 'Dave'], [2, 'John']])
|
51
|
+
end
|
40
52
|
end
|
41
53
|
|
42
|
-
|
43
|
-
|
54
|
+
context '#to_select' do
|
55
|
+
it "should return array for select helpers" do
|
56
|
+
expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should return sorted array from order value for select helpers when an order is specified" do
|
60
|
+
expect(SortedPerson.to_select).to eq([['John', 2], ['Dave', 1]])
|
61
|
+
end
|
44
62
|
end
|
45
63
|
|
46
|
-
|
47
|
-
|
64
|
+
context '#meta' do
|
65
|
+
it "should return record attributes hash without id and name columns" do
|
66
|
+
expect(Person.meta(1)).to eq(Person.find(1).attributes.except('id', 'first_name'))
|
67
|
+
end
|
48
68
|
end
|
49
69
|
|
50
|
-
|
51
|
-
|
70
|
+
context '#include?' do
|
71
|
+
it "should return true if value is integer and model has id" do
|
72
|
+
expect(Person.exists?(id: 1)).to eq(true)
|
73
|
+
expect(Person.include?(1)).to eq(true)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return false if value is integer and model does not have id" do
|
77
|
+
expect(Person.exists?(id: 100)).to eq(false)
|
78
|
+
expect(Person.include?(100)).to eq(false)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should return super if value is a module" do
|
82
|
+
expect(Person.include?(ActiveRecord::Attributes)).to eq(true)
|
83
|
+
|
84
|
+
expect(Person.include?(Module.new)).to eq(false)
|
85
|
+
end
|
52
86
|
end
|
53
87
|
end
|
@@ -24,9 +24,9 @@ describe ActiveEnum::Base do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
describe ".
|
27
|
+
describe ".values" do
|
28
28
|
it 'should return an empty array when no values defined' do
|
29
|
-
expect(define_enum.
|
29
|
+
expect(define_enum.values).to eq([])
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should return an array of arrays with all values defined as [id, name]' do
|
@@ -34,7 +34,7 @@ describe ActiveEnum::Base do
|
|
34
34
|
value :name => 'Name 1'
|
35
35
|
value :name => 'Name 2'
|
36
36
|
end
|
37
|
-
expect(enum.
|
37
|
+
expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -43,28 +43,28 @@ describe ActiveEnum::Base do
|
|
43
43
|
enum = define_enum do
|
44
44
|
value :id => 1, :name => 'Name'
|
45
45
|
end
|
46
|
-
expect(enum.
|
46
|
+
expect(enum.values).to eq([[1,'Name']])
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'should allow me to define a value with a name only' do
|
50
50
|
enum = define_enum do
|
51
51
|
value :name => 'Name'
|
52
52
|
end
|
53
|
-
expect(enum.
|
53
|
+
expect(enum.values).to eq([[1,'Name']])
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'should allow me to define a value as hash with id as key and name as value' do
|
57
57
|
enum = define_enum do
|
58
58
|
value 1 => 'Name'
|
59
59
|
end
|
60
|
-
expect(enum.
|
60
|
+
expect(enum.values).to eq([[1,'Name']])
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'should allow to define meta data value with extra key value pairs' do
|
64
64
|
enum = define_enum do
|
65
65
|
value :id => 1, :name => 'Name', :description => 'extra'
|
66
66
|
end
|
67
|
-
expect(enum.
|
67
|
+
expect(enum.values).to eq([[1,'Name',{:description => 'extra'}]])
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'should increment value ids when defined without ids' do
|
@@ -72,7 +72,7 @@ describe ActiveEnum::Base do
|
|
72
72
|
value :name => 'Name 1'
|
73
73
|
value :name => 'Name 2'
|
74
74
|
end
|
75
|
-
expect(enum.
|
75
|
+
expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'should raise error if the id is a duplicate' do
|
@@ -117,7 +117,7 @@ describe ActiveEnum::Base do
|
|
117
117
|
value :id => 2, :name => 'Name 2'
|
118
118
|
value :id => 1, :name => 'Name 1'
|
119
119
|
end
|
120
|
-
expect(enum.
|
120
|
+
expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
|
121
121
|
end
|
122
122
|
|
123
123
|
it 'should return sorted values by id using order setting' do
|
@@ -126,7 +126,7 @@ describe ActiveEnum::Base do
|
|
126
126
|
value :id => 1, :name => 'Name 1'
|
127
127
|
value :id => 2, :name => 'Name 2'
|
128
128
|
end
|
129
|
-
expect(enum.
|
129
|
+
expect(enum.values).to eq([[2, 'Name 2'], [1,'Name 1']])
|
130
130
|
end
|
131
131
|
|
132
132
|
it 'should return sorted values by id using order setting' do
|
@@ -136,7 +136,7 @@ describe ActiveEnum::Base do
|
|
136
136
|
value :id => 1, :name => 'Name 1'
|
137
137
|
value :id => 2, :name => 'Name 2'
|
138
138
|
end
|
139
|
-
expect(enum.
|
139
|
+
expect(enum.values).to eq([[3,'Name 3'], [1,'Name 1'], [2, 'Name 2']])
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
@@ -248,6 +248,32 @@ describe ActiveEnum::Base do
|
|
248
248
|
end
|
249
249
|
end
|
250
250
|
|
251
|
+
describe ".to_grouped_select" do
|
252
|
+
it 'should return array for grouped select helpers grouped by meta key value' do
|
253
|
+
enum = define_enum do
|
254
|
+
value :id => 1, :name => 'Name 1', :category => 'Foo'
|
255
|
+
value :id => 2, :name => 'Name 2', :category => 'Bar'
|
256
|
+
end
|
257
|
+
|
258
|
+
expect(enum.to_grouped_select(:category)).to eq([
|
259
|
+
[ 'Foo', [ ['Name 1',1] ] ],
|
260
|
+
[ 'Bar', [ ['Name 2',2] ] ]
|
261
|
+
])
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'should group any value missing the group_by key by nil' do
|
265
|
+
enum = define_enum do
|
266
|
+
value :id => 1, :name => 'Name 1', :category => 'Foo'
|
267
|
+
value :id => 2, :name => 'Name 2'
|
268
|
+
end
|
269
|
+
|
270
|
+
expect(enum.to_grouped_select(:category)).to eq([
|
271
|
+
[ 'Foo', [ ['Name 1',1] ] ],
|
272
|
+
[ nil, [ ['Name 2',2] ] ]
|
273
|
+
])
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
251
277
|
def define_enum(&block)
|
252
278
|
Class.new(ActiveEnum::Base, &block)
|
253
279
|
end
|
@@ -13,6 +13,16 @@ describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
|
|
13
13
|
value :id => 1, :name => 'Male'
|
14
14
|
value :id => 2, :name => 'Female'
|
15
15
|
end
|
16
|
+
|
17
|
+
enumerate :employment_status do
|
18
|
+
value :id => 1, :name => 'Full-time', group: 'Waged'
|
19
|
+
value :id => 3, :name => 'Part-time', group: 'Waged'
|
20
|
+
value :id => 4, :name => 'Casual', group: 'Waged'
|
21
|
+
value :id => 5, :name => 'Student', group: 'Un-waged'
|
22
|
+
value :id => 6, :name => 'Retired', group: 'Un-waged'
|
23
|
+
value :id => 7, :name => 'Unemployed', group: 'Un-waged'
|
24
|
+
value :id => 8, :name => 'Carer', group: 'Un-waged'
|
25
|
+
end
|
16
26
|
end
|
17
27
|
end
|
18
28
|
|
@@ -34,6 +44,15 @@ describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
|
|
34
44
|
expect(output).to have_xpath('//option[@value=2]', :text => 'Female')
|
35
45
|
end
|
36
46
|
|
47
|
+
it "should use explicit :grouped_enum input type" do
|
48
|
+
output = simple_form_for(Person.new, :url => people_path) do |f|
|
49
|
+
concat f.input(:employment_status, :as => :grouped_enum, :group_by => :group)
|
50
|
+
end
|
51
|
+
expect(output).to have_selector('select#person_employment_status')
|
52
|
+
expect(output).to have_xpath('//optgroup[@label="Waged"]/option[@value=1]', :text => 'Full-time')
|
53
|
+
expect(output).to have_xpath('//optgroup[@label="Un-waged"]/option[@value=8]', :text => 'Carer')
|
54
|
+
end
|
55
|
+
|
37
56
|
it "should not use enum input type if :as option indicates other type" do
|
38
57
|
output = simple_form_for(Person.new, :url => people_path) do |f|
|
39
58
|
concat f.input(:sex, :as => :string)
|
data/spec/support/schema.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -39,6 +39,10 @@ files:
|
|
39
39
|
- README.rdoc
|
40
40
|
- Rakefile
|
41
41
|
- active_enum.gemspec
|
42
|
+
- gemfiles/rails_4_2.gemfile
|
43
|
+
- gemfiles/rails_5_0.gemfile
|
44
|
+
- gemfiles/rails_5_1.gemfile
|
45
|
+
- gemfiles/rails_5_2.gemfile
|
42
46
|
- init.rb
|
43
47
|
- lib/active_enum.rb
|
44
48
|
- lib/active_enum/acts_as_enum.rb
|
@@ -68,7 +72,7 @@ files:
|
|
68
72
|
homepage: http://github.com/adzap/active_enum
|
69
73
|
licenses: []
|
70
74
|
metadata: {}
|
71
|
-
post_install_message:
|
75
|
+
post_install_message:
|
72
76
|
rdoc_options: []
|
73
77
|
require_paths:
|
74
78
|
- lib
|
@@ -83,9 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
87
|
- !ruby/object:Gem::Version
|
84
88
|
version: 1.3.1
|
85
89
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
90
|
+
rubygems_version: 3.0.3
|
91
|
+
signing_key:
|
89
92
|
specification_version: 4
|
90
93
|
summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|
91
94
|
test_files:
|