active_enum 1.0.0.rc3 → 1.0.0.rc8
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/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 +7 -3
- data/lib/active_enum/base.rb +7 -5
- data/lib/active_enum/extensions.rb +2 -2
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/acts_as_enum_spec.rb +6 -0
- data/spec/active_enum/base_spec.rb +23 -11
- data/spec/active_enum/extensions_spec.rb +5 -0
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 906926ead31439e2c3457cf32e453bf6c6a342245a6e8f0ce1d1debd6fd7b539
|
4
|
+
data.tar.gz: 13dd1539bded32f57ba862d4e0d068d6d3d0e7aae22bb8fe1078d6e9c823d261
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46d4c12f04dd2affd956488081f0735a9b19767ae4b370a912cf0ef31779a01d3c398da082bd1bb03e22156ccd647ace3eb54ed95a9628e81847299fdf8e0fc0
|
7
|
+
data.tar.gz: 3b362e15afb2b1cb5f2215577605027eacc3b3d15252bdca4e664baf8259c2caa72b50bac9314662323eb60869731b2a7385c84290dd239e838f5f1f5ac70182
|
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
@@ -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
|
@@ -59,7 +63,7 @@ module ActiveEnum
|
|
59
63
|
if index.is_a?(Integer)
|
60
64
|
enum_values.where(id: index)
|
61
65
|
else
|
62
|
-
enum_values.where("#{active_enum_options[:name_column]}
|
66
|
+
enum_values.where("lower(#{active_enum_options[:name_column]}) = lower(?)", Arel.sql(index.to_s))
|
63
67
|
end
|
64
68
|
end
|
65
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)
|
@@ -59,7 +61,7 @@ module ActiveEnum
|
|
59
61
|
|
60
62
|
# Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
|
61
63
|
def to_grouped_select(group_by)
|
62
|
-
store.values.group_by { |(_id, _name, meta)| meta
|
64
|
+
store.values.group_by { |(_id, _name, meta)| (meta || {})[group_by] }.map { |group, collection|
|
63
65
|
[ group, collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
|
64
66
|
}
|
65
67
|
end
|
@@ -74,7 +76,7 @@ module ActiveEnum
|
|
74
76
|
alias_method :[], :get
|
75
77
|
|
76
78
|
def include?(value)
|
77
|
-
!
|
79
|
+
!get_value(value, false).nil?
|
78
80
|
end
|
79
81
|
|
80
82
|
# Access any meta data defined for a given id or name. Returns a hash.
|
@@ -86,12 +88,12 @@ module ActiveEnum
|
|
86
88
|
private
|
87
89
|
|
88
90
|
# Access value row array for a given id or name value.
|
89
|
-
def get_value(index)
|
91
|
+
def get_value(index, raise_on_not_found = ActiveEnum.raise_on_not_found)
|
90
92
|
if index.is_a?(Integer)
|
91
93
|
store.get_by_id(index)
|
92
94
|
else
|
93
95
|
store.get_by_name(index)
|
94
|
-
end || (
|
96
|
+
end || (raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
95
97
|
end
|
96
98
|
|
97
99
|
def id_and_name_and_meta(hash)
|
@@ -110,7 +110,7 @@ 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
115
|
super
|
116
116
|
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}(: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
|
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]])
|
@@ -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
|
|
@@ -260,6 +260,18 @@ describe ActiveEnum::Base do
|
|
260
260
|
[ 'Bar', [ ['Name 2',2] ] ]
|
261
261
|
])
|
262
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
|
263
275
|
end
|
264
276
|
|
265
277
|
def define_enum(&block)
|
@@ -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
|
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.rc8
|
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-10-08 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,8 +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
|
-
rubygems_version: 3.0.
|
87
|
-
signing_key:
|
90
|
+
rubygems_version: 3.0.3
|
91
|
+
signing_key:
|
88
92
|
specification_version: 4
|
89
93
|
summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|
90
94
|
test_files:
|