active_enum 1.0.0.rc1 → 1.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/active_enum/acts_as_enum.rb +28 -16
- data/lib/active_enum/base.rb +13 -13
- data/lib/active_enum/extensions.rb +1 -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 +23 -13
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e0463e9528b9a2ec019caf593cfc2becaa1767a116d4657d93de5462c8fb5475
|
4
|
+
data.tar.gz: 2cd9be66544fa2bd00cf2ace406c4c5010411f02d53f1c926d0f197609e89993
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efd7a02386a9e3a6a45812b35e8c509ac60d47249238bee485b00fc8d0a1598ed8a0ae5319e02d76f59ca06ccce5e22dfd25ad0b217ee29f7471510a8deeb15c
|
7
|
+
data.tar.gz: d69b2b354382981ba79af48b3d0306839a1f218d7e7bf01136a2021fdc7026a4b0eedfc40e00e5d63c24deaf2eccf00b5727083a8a2023947a29a00ed579a5ec
|
@@ -17,37 +17,49 @@ module ActiveEnum
|
|
17
17
|
module ClassMethods
|
18
18
|
|
19
19
|
def ids
|
20
|
-
enum_values.map {|v| v.id }
|
20
|
+
enum_values.map { |v| v.id }
|
21
21
|
end
|
22
22
|
|
23
23
|
def names
|
24
|
-
enum_values.map {|v| v.send(active_enum_options[:name_column]) }
|
24
|
+
enum_values.map { |v| v.send(active_enum_options[:name_column]) }
|
25
25
|
end
|
26
26
|
|
27
27
|
def to_select
|
28
|
-
enum_values.map {|v| [v.send(active_enum_options[:name_column]), v.id] }
|
28
|
+
enum_values.map { |v| [v.send(active_enum_options[:name_column]), v.id] }
|
29
29
|
end
|
30
30
|
|
31
31
|
def [](index)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
else
|
36
|
-
v = lookup_by_name(index)
|
37
|
-
v.id unless v.blank?
|
38
|
-
end
|
32
|
+
row = get_value(index)
|
33
|
+
return if row.nil?
|
34
|
+
index.is_a?(Integer) ? row.send(active_enum_options[:name_column]) : row.id
|
39
35
|
end
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
# Access any meta data defined for a given id or name. Returns a hash.
|
38
|
+
def meta(index)
|
39
|
+
row = lookup_relation(index).unscope(:select).first
|
40
|
+
row&.attributes.except(primary_key.to_s, active_enum_options[:name_column].to_s)
|
45
41
|
end
|
46
42
|
|
47
|
-
|
48
|
-
|
43
|
+
# Enables use as a delimiter in inclusion validation
|
44
|
+
def include?(value)
|
45
|
+
!self[value].nil?
|
49
46
|
end
|
50
47
|
|
48
|
+
private
|
49
|
+
|
50
|
+
def get_value(index)
|
51
|
+
lookup_relation(index).first || begin
|
52
|
+
raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if ActiveEnum.raise_on_not_found
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def lookup_relation(index)
|
57
|
+
if index.is_a?(Integer)
|
58
|
+
enum_values.where(id: index)
|
59
|
+
else
|
60
|
+
enum_values.where("#{active_enum_options[:name_column]} like lower(?)", index.to_s)
|
61
|
+
end
|
62
|
+
end
|
51
63
|
end
|
52
64
|
|
53
65
|
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.
|
@@ -44,17 +44,17 @@ module ActiveEnum
|
|
44
44
|
|
45
45
|
# Array of all enum id values
|
46
46
|
def ids
|
47
|
-
store.values.map {|v| v[0] }
|
47
|
+
store.values.map { |v| v[0] }
|
48
48
|
end
|
49
49
|
|
50
50
|
# Array of all enum name values
|
51
51
|
def names
|
52
|
-
store.values.map {|v| v[1] }
|
52
|
+
store.values.map { |v| v[1] }
|
53
53
|
end
|
54
54
|
|
55
55
|
# Return enum values in an array suitable to pass to a Rails form select helper.
|
56
56
|
def to_select
|
57
|
-
store.values.map {|v| [v[1], v[0]] }
|
57
|
+
store.values.map { |v| [v[1], v[0]] }
|
58
58
|
end
|
59
59
|
|
60
60
|
# Access id or name value. Pass an id number to retrieve the name or
|
@@ -66,15 +66,6 @@ module ActiveEnum
|
|
66
66
|
end
|
67
67
|
alias_method :[], :get
|
68
68
|
|
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
69
|
def include?(value)
|
79
70
|
!get(value).nil?
|
80
71
|
end
|
@@ -87,6 +78,15 @@ module ActiveEnum
|
|
87
78
|
|
88
79
|
private
|
89
80
|
|
81
|
+
# Access value row array for a given id or name value.
|
82
|
+
def get_value(index)
|
83
|
+
if index.is_a?(Integer)
|
84
|
+
store.get_by_id(index)
|
85
|
+
else
|
86
|
+
store.get_by_name(index)
|
87
|
+
end || (ActiveEnum.raise_on_not_found ? raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") : nil)
|
88
|
+
end
|
89
|
+
|
90
90
|
def id_and_name_and_meta(hash)
|
91
91
|
if hash.has_key?(:name)
|
92
92
|
id = hash.delete(:id) || next_id
|
data/lib/active_enum/version.rb
CHANGED
@@ -30,24 +30,34 @@ describe ActiveEnum::ActsAsEnum do
|
|
30
30
|
expect(TestPerson.extended_modules).to include(ActiveEnum::ActsAsEnum::ClassMethods)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
it "should return id column value when passing symbol name to [] method" do
|
44
|
+
expect(Person[:dave]).to eq(1)
|
45
|
+
end
|
44
46
|
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
+
context '#to_select' do
|
49
|
+
it "should return array for select helpers" do
|
50
|
+
expect(Person.to_select).to eq([['Dave', 1], ['John', 2]])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return sorted array from order value for select helpers when an order is specified" do
|
54
|
+
expect(SortedPerson.to_select).to eq([['John', 2], ['Dave', 1]])
|
55
|
+
end
|
48
56
|
end
|
49
57
|
|
50
|
-
|
51
|
-
|
58
|
+
context '#meta' do
|
59
|
+
it "should return record attributes hash without id and name columns" do
|
60
|
+
expect(Person.meta(1)).to eq(Person.find(1).attributes.except('id', 'first_name'))
|
61
|
+
end
|
52
62
|
end
|
53
63
|
end
|
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.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
version: 1.3.1
|
85
85
|
requirements: []
|
86
86
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
87
|
+
rubygems_version: 2.7.6.2
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|