active_enum 1.0.0.rc2 → 1.0.0.rc3
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/Rakefile +0 -5
- data/lib/active_enum/acts_as_enum.rb +2 -0
- data/lib/active_enum/base.rb +8 -1
- data/lib/active_enum/extensions.rb +1 -1
- 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 +18 -0
- data/spec/active_enum/base_spec.rb +14 -0
- data/spec/active_enum/form_helpers/simple_form_spec.rb +19 -0
- data/spec/support/schema.rb +1 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d2ff570eda0a94b326f65a6a97b5b6bb68cd3f953fba95cdc14e34e184f8afc
|
4
|
+
data.tar.gz: fb5e45a1e55ee514408b1e1a71c0559b52bacb74f2d632dea63680a495bcf523
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dae56bad88d50dbef7355c37243b5c76dea0cd4a741ee6389f94824453f3e5d05bae64966102a304ce643fc675f3a37584637ac4e6bf9d0219d96911e0effbd2
|
7
|
+
data.tar.gz: 6aec92d08a5afad8eb137cd319346d41784ec19648a8c261408dc2896fcb853a4e8ba345bd50d98b67ddf5e49d82747c3fdd32035a9011184a1c7783a1f69b63
|
data/Rakefile
CHANGED
data/lib/active_enum/base.rb
CHANGED
@@ -54,7 +54,14 @@ module ActiveEnum
|
|
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].html_safe, v[0]] }
|
58
|
+
end
|
59
|
+
|
60
|
+
# Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
|
61
|
+
def to_grouped_select(group_by)
|
62
|
+
store.values.group_by { |(_id, _name, meta)| meta.fetch(group_by) }.map { |group, collection|
|
63
|
+
[ group, collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
|
64
|
+
}
|
58
65
|
end
|
59
66
|
|
60
67
|
# Access id or name value. Pass an id number to retrieve the name or
|
@@ -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
@@ -60,4 +60,22 @@ describe ActiveEnum::ActsAsEnum do
|
|
60
60
|
expect(Person.meta(1)).to eq(Person.find(1).attributes.except('id', 'first_name'))
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
context '#include?' do
|
65
|
+
it "should return true if value is integer and model has id" do
|
66
|
+
expect(Person.exists?(id: 1)).to eq(true)
|
67
|
+
expect(Person.include?(1)).to eq(true)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should return false if value is integer and model does not have id" do
|
71
|
+
expect(Person.exists?(id: 100)).to eq(false)
|
72
|
+
expect(Person.include?(100)).to eq(false)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return super if value is a module" do
|
76
|
+
expect(Person.include?(ActiveRecord::Attributes)).to eq(true)
|
77
|
+
|
78
|
+
expect(Person.include?(Module.new)).to eq(false)
|
79
|
+
end
|
80
|
+
end
|
63
81
|
end
|
@@ -248,6 +248,20 @@ 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
|
+
end
|
264
|
+
|
251
265
|
def define_enum(&block)
|
252
266
|
Class.new(ActiveEnum::Base, &block)
|
253
267
|
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.rc3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -83,8 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: 1.3.1
|
85
85
|
requirements: []
|
86
|
-
|
87
|
-
rubygems_version: 2.7.6.2
|
86
|
+
rubygems_version: 3.0.6
|
88
87
|
signing_key:
|
89
88
|
specification_version: 4
|
90
89
|
summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
|