active_enum 1.0.0.rc2 → 1.0.0.rc7
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 +4 -4
- 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 +9 -3
- data/lib/active_enum/base.rb +11 -2
- data/lib/active_enum/extensions.rb +3 -3
- data/lib/active_enum/form_helpers/simple_form.rb +13 -1
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/acts_as_enum_spec.rb +24 -0
- data/spec/active_enum/base_spec.rb +37 -11
- data/spec/active_enum/extensions_spec.rb +5 -0
- 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
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c79754f5d68c12d7955e1a77e870612ffb1fdb62cc65681e50efc137da125ba
|
4
|
+
data.tar.gz: e0604ad18f10935d8111491b18481e3b273b7d6d064c0284c41c02836cadff3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '07383e22c02937744c155c72e0c35333c03169f7f3807dfd54765f490a225035ecd24017664fe7b1274c134422e164b12d4fc1a18f57b227f649b93bb166c3b2'
|
7
|
+
data.tar.gz: 8bd02a86d9a478ec1bf1cbdf76f28fa6d673b00c61f2edddf6a9c3d90782a33986087ff8a3cf789ab439ab58b60227f3f7c36c4ab5ab097ee6b6430bff119119
|
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,15 +7,19 @@ 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
24
|
enum_values.map { |v| v.id }
|
21
25
|
end
|
@@ -42,6 +46,8 @@ module ActiveEnum
|
|
42
46
|
|
43
47
|
# Enables use as a delimiter in inclusion validation
|
44
48
|
def include?(value)
|
49
|
+
return super if value.is_a?(Module)
|
50
|
+
|
45
51
|
!self[value].nil?
|
46
52
|
end
|
47
53
|
|
@@ -57,7 +63,7 @@ module ActiveEnum
|
|
57
63
|
if index.is_a?(Integer)
|
58
64
|
enum_values.where(id: index)
|
59
65
|
else
|
60
|
-
enum_values.where("#{active_enum_options[:name_column]}
|
66
|
+
enum_values.where("lower(#{active_enum_options[:name_column]}) = lower(?)", Arel.sql(index.to_s))
|
61
67
|
end
|
62
68
|
end
|
63
69
|
end
|
data/lib/active_enum/base.rb
CHANGED
@@ -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)
|
@@ -54,7 +56,14 @@ module ActiveEnum
|
|
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
|
@@ -110,9 +110,9 @@ module ActiveEnum
|
|
110
110
|
class_eval <<-DEF
|
111
111
|
def #{attribute}=(arg)
|
112
112
|
if arg.is_a?(Symbol)
|
113
|
-
super
|
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
|
@@ -127,7 +127,7 @@ module ActiveEnum
|
|
127
127
|
class_eval <<-DEF
|
128
128
|
def #{attribute}?(arg=nil)
|
129
129
|
if arg
|
130
|
-
self.#{attribute}(:id) == self.class.active_enum_for(:#{attribute})[arg]
|
130
|
+
self.#{attribute}(:id).present? && self.#{attribute}(:id) == self.class.active_enum_for(:#{attribute})[arg]
|
131
131
|
else
|
132
132
|
super()
|
133
133
|
end
|
@@ -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
@@ -45,6 +45,12 @@ describe ActiveEnum::ActsAsEnum do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
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
|
52
|
+
end
|
53
|
+
|
48
54
|
context '#to_select' do
|
49
55
|
it "should return array for select helpers" do
|
50
56
|
expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
|
@@ -60,4 +66,22 @@ describe ActiveEnum::ActsAsEnum do
|
|
60
66
|
expect(Person.meta(1)).to eq(Person.find(1).attributes.except('id', 'first_name'))
|
61
67
|
end
|
62
68
|
end
|
69
|
+
|
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
|
86
|
+
end
|
63
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
|
@@ -176,6 +176,11 @@ describe ActiveEnum::Extensions do
|
|
176
176
|
expect(person.sex?(:female)).to be_falsey
|
177
177
|
expect(person.sex?(:Female)).to be_falsey
|
178
178
|
end
|
179
|
+
|
180
|
+
it 'should return false if attribute is nil regardless of enum value' do
|
181
|
+
person.sex = nil
|
182
|
+
expect(person.sex?(:nonexistent)).to be_falsey
|
183
|
+
end
|
179
184
|
end
|
180
185
|
|
181
186
|
context "with value as enum name symbol" do
|
@@ -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.rc7
|
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-13 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:
|