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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0463e9528b9a2ec019caf593cfc2becaa1767a116d4657d93de5462c8fb5475
4
- data.tar.gz: 2cd9be66544fa2bd00cf2ace406c4c5010411f02d53f1c926d0f197609e89993
3
+ metadata.gz: 8d2ff570eda0a94b326f65a6a97b5b6bb68cd3f953fba95cdc14e34e184f8afc
4
+ data.tar.gz: fb5e45a1e55ee514408b1e1a71c0559b52bacb74f2d632dea63680a495bcf523
5
5
  SHA512:
6
- metadata.gz: efd7a02386a9e3a6a45812b35e8c509ac60d47249238bee485b00fc8d0a1598ed8a0ae5319e02d76f59ca06ccce5e22dfd25ad0b217ee29f7471510a8deeb15c
7
- data.tar.gz: d69b2b354382981ba79af48b3d0306839a1f218d7e7bf01136a2021fdc7026a4b0eedfc40e00e5d63c24deaf2eccf00b5727083a8a2023947a29a00ed579a5ec
6
+ metadata.gz: dae56bad88d50dbef7355c37243b5c76dea0cd4a741ee6389f94824453f3e5d05bae64966102a304ce643fc675f3a37584637ac4e6bf9d0219d96911e0effbd2
7
+ data.tar.gz: 6aec92d08a5afad8eb137cd319346d41784ec19648a8c261408dc2896fcb853a4e8ba345bd50d98b67ddf5e49d82747c3fdd32035a9011184a1c7783a1f69b63
data/Rakefile CHANGED
@@ -24,8 +24,3 @@ end
24
24
 
25
25
  desc 'Default: run specs.'
26
26
  task :default => :spec
27
-
28
- task :all do
29
- sh "export FORMTASTIC=2.0 && bundle install && bundle exec rake"
30
- sh "export FORMTASTIC=1.2 && bundle install && bundle exec rake"
31
- end
@@ -42,6 +42,8 @@ module ActiveEnum
42
42
 
43
43
  # Enables use as a delimiter in inclusion validation
44
44
  def include?(value)
45
+ return super if value.is_a?(Module)
46
+
45
47
  !self[value].nil?
46
48
  end
47
49
 
@@ -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
@@ -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 arg
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, :to => ActiveEnum::FormHelpers::SimpleForm::EnumInput
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
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.0.0.rc2'
2
+ VERSION = '1.0.0.rc3'
3
3
  end
@@ -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)
@@ -5,6 +5,7 @@ ActiveRecord::Schema.define(:version => 1) do
5
5
  t.integer :sex
6
6
  t.integer :attending
7
7
  t.integer :staying
8
+ t.integer :employment_status
8
9
  end
9
10
 
10
11
  create_table :sorted_people, :force => true do |t|
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.rc2
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-07 00:00:00.000000000 Z
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
- rubyforge_project:
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