enum_fields 0.0.1
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 +7 -0
- data/CHANGELOG.md +21 -0
- data/LICENSE +21 -0
- data/README.md +276 -0
- data/lib/enum_fields/definition.rb +64 -0
- data/lib/enum_fields/enum_field.rb +142 -0
- data/lib/enum_fields/errors.rb +6 -0
- data/lib/enum_fields/version.rb +5 -0
- data/lib/enum_fields.rb +41 -0
- metadata +98 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0ae632efc222874e0efa5a5859e9e8b69b3a86e18ff162ea0d6b1ba2a65771da
|
|
4
|
+
data.tar.gz: 2dbbaffc02090af1a2a6185b966ec45e010defd99469d952ccecc3f9929cdc60
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7bd2c106cabd6e3709e9c69fc83535596818c5805359b63d34edd48515c8f31aaf33ed7033910470850966f66627ca2d1ee6e81859fc0efd9b7c0df190aa0d7f
|
|
7
|
+
data.tar.gz: 02ec904e369f37cdcdb6249a8a58370d02420188f8505f8abce43f967b23ab6dafc70870575eaf5603f85e04bb6b7bef19fe9df18700eda20fa2ef1ce4f94b52
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.1] - 2025-11-17
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Initial release of EnumFields gem
|
|
8
|
+
- Support for enum-like fields with metadata properties
|
|
9
|
+
- Array and Hash definition formats
|
|
10
|
+
- Automatic generation of:
|
|
11
|
+
- Class methods (`<accessor>s`, `<accessor>s_count`, `<accessor>_values`, `<accessor>_options`)
|
|
12
|
+
- Instance getter/setter methods (when accessor differs from column)
|
|
13
|
+
- Metadata accessor methods (`<accessor>_metadata`)
|
|
14
|
+
- Property accessor methods for all defined properties (`<accessor>_<property>`)
|
|
15
|
+
- Inquiry methods (`<key>_<accessor>?`)
|
|
16
|
+
- Scopes (`<key>_<accessor>`)
|
|
17
|
+
- Validations (inclusion with `allow_nil: true`)
|
|
18
|
+
- Custom column mapping support via `column:` option
|
|
19
|
+
- Dynamic property method generation for custom properties
|
|
20
|
+
|
|
21
|
+
[0.0.1]: https://github.com/kinnell/enum_fields/releases/tag/v0.0.1
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kinnell Shah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# EnumFields
|
|
2
|
+
|
|
3
|
+
Enhanced enum-like fields for ActiveRecord models with metadata support
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Ruby >= 2.7.6
|
|
8
|
+
- Rails >= 6.0 (ActiveRecord and ActiveSupport)
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Add this line to your application's Gemfile:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
gem 'enum_fields'
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
And then execute:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle install
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Basic Setup
|
|
27
|
+
|
|
28
|
+
Include the `EnumFields` module in your `ApplicationRecord`:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
32
|
+
include EnumFields
|
|
33
|
+
|
|
34
|
+
self.abstract_class = true
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Now all models inheriting from `ApplicationRecord` can use `enum_field`.
|
|
39
|
+
|
|
40
|
+
### Defining Enum Fields
|
|
41
|
+
|
|
42
|
+
#### Hash Definition (Recommended)
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
class Campaign < ApplicationRecord
|
|
46
|
+
enum_field :stage, {
|
|
47
|
+
pending: {
|
|
48
|
+
value: 'pending',
|
|
49
|
+
label: 'Pending',
|
|
50
|
+
icon: 'clock',
|
|
51
|
+
color: 'yellow',
|
|
52
|
+
tooltip: 'Campaign is awaiting processing',
|
|
53
|
+
},
|
|
54
|
+
processing: {
|
|
55
|
+
value: 'processing',
|
|
56
|
+
label: 'Processing',
|
|
57
|
+
icon: 'cog',
|
|
58
|
+
color: 'blue',
|
|
59
|
+
tooltip: 'Campaign is being processed',
|
|
60
|
+
},
|
|
61
|
+
shipped: {
|
|
62
|
+
value: 'shipped',
|
|
63
|
+
label: 'Shipped',
|
|
64
|
+
icon: 'truck',
|
|
65
|
+
color: 'green',
|
|
66
|
+
tooltip: 'Campaign has been shipped',
|
|
67
|
+
},
|
|
68
|
+
delivered: {
|
|
69
|
+
value: 'delivered',
|
|
70
|
+
label: 'Delivered',
|
|
71
|
+
icon: 'check',
|
|
72
|
+
color: 'green',
|
|
73
|
+
tooltip: 'Campaign has been delivered',
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### Array Definition (Simple)
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
class Task < ApplicationRecord
|
|
83
|
+
enum_field :priority, ['low', 'medium', 'high']
|
|
84
|
+
end
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
This automatically generates:
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
{
|
|
91
|
+
low: {
|
|
92
|
+
value: 'low',
|
|
93
|
+
label: 'low',
|
|
94
|
+
},
|
|
95
|
+
medium: {
|
|
96
|
+
value: 'medium',
|
|
97
|
+
label: 'medium',
|
|
98
|
+
},
|
|
99
|
+
high: {
|
|
100
|
+
value: 'high',
|
|
101
|
+
label: 'high',
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Custom Column Mapping
|
|
107
|
+
|
|
108
|
+
If your accessor name differs from your column name:
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
class User < ApplicationRecord
|
|
112
|
+
enum_field :role, definitions, column: :user_role
|
|
113
|
+
end
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Generated Methods
|
|
117
|
+
|
|
118
|
+
For an enum field defined as:
|
|
119
|
+
|
|
120
|
+
```ruby
|
|
121
|
+
class Campaign < ApplicationRecord
|
|
122
|
+
enum_field :stage, {
|
|
123
|
+
draft: {
|
|
124
|
+
value: 'draft',
|
|
125
|
+
label: 'Draft',
|
|
126
|
+
icon: 'file',
|
|
127
|
+
color: 'gray',
|
|
128
|
+
},
|
|
129
|
+
scheduled: {
|
|
130
|
+
value: 'scheduled',
|
|
131
|
+
label: 'Scheduled',
|
|
132
|
+
icon: 'calendar',
|
|
133
|
+
color: 'blue',
|
|
134
|
+
},
|
|
135
|
+
completed: {
|
|
136
|
+
value: 'completed',
|
|
137
|
+
label: 'Completed',
|
|
138
|
+
icon: 'check',
|
|
139
|
+
color: 'green',
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
end
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Class Methods
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
# Returns the definitions as an HashWithIndifferentAccess
|
|
149
|
+
Campaign.stages
|
|
150
|
+
|
|
151
|
+
# Returns the count of definitions
|
|
152
|
+
Campaign.stages_count # 3
|
|
153
|
+
|
|
154
|
+
# Returns the values of the definitions
|
|
155
|
+
Campaign.stages_values # ['draft', 'scheduled', 'completed']
|
|
156
|
+
|
|
157
|
+
# Returns the options for form helpers
|
|
158
|
+
Campaign.stages_options # [['Draft', 'draft'], ['Scheduled', 'scheduled'], ['Completed', 'completed']]
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### Instance Getter/Setter
|
|
162
|
+
|
|
163
|
+
If the accessor name differs from the column name, getter and setter methods are defined for the accessor.
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
166
|
+
campaign.stage # 'draft'
|
|
167
|
+
campaign.stage = 'scheduled'
|
|
168
|
+
campaign.stage # 'scheduled'
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
- `campaign.stage` - Get the current stage value
|
|
172
|
+
- `campaign.stage = 'scheduled'` - Set the stage value
|
|
173
|
+
|
|
174
|
+
#### Metadata Methods
|
|
175
|
+
|
|
176
|
+
The gem automatically creates accessor methods for all properties defined in your enum definitions.
|
|
177
|
+
|
|
178
|
+
- `value` (required) - The actual value stored in the database
|
|
179
|
+
- `label` (auto-generated if not provided) - A human-readable label
|
|
180
|
+
|
|
181
|
+
Any additional properties you define (like `icon`, `color`, `tooltip`, etc.) will also get dedicated accessor methods automatically.
|
|
182
|
+
|
|
183
|
+
```ruby
|
|
184
|
+
# Returns the full metadata hash for current value
|
|
185
|
+
campaign.stage_metadata
|
|
186
|
+
# => { value: 'draft', label: 'Draft', icon: 'file', color: 'gray' }
|
|
187
|
+
|
|
188
|
+
# Access individual properties
|
|
189
|
+
campaign.stage_value # 'draft'
|
|
190
|
+
campaign.stage_label # 'Draft'
|
|
191
|
+
campaign.stage_icon # 'file'
|
|
192
|
+
campaign.stage_color # 'gray'
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Inquiry Methods
|
|
196
|
+
|
|
197
|
+
```ruby
|
|
198
|
+
# Returns true if the current value is 'draft'
|
|
199
|
+
campaign.draft_stage?
|
|
200
|
+
|
|
201
|
+
# Returns true if the current value is 'scheduled'
|
|
202
|
+
campaign.scheduled_stage?
|
|
203
|
+
|
|
204
|
+
# Returns true if the current value is 'completed'
|
|
205
|
+
campaign.completed_stage?
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### Scopes
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
# Returns all campaigns with draft stage
|
|
212
|
+
Campaign.draft_stage
|
|
213
|
+
|
|
214
|
+
# Returns all campaigns with scheduled stage
|
|
215
|
+
Campaign.scheduled_stage
|
|
216
|
+
|
|
217
|
+
# Returns all campaigns with completed stage
|
|
218
|
+
Campaign.completed_stage
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### Validation
|
|
222
|
+
|
|
223
|
+
Automatically validates that the column value is included in the defined values (with `allow_nil: true`).
|
|
224
|
+
|
|
225
|
+
### Custom Properties
|
|
226
|
+
|
|
227
|
+
You can add any custom properties to your definitions, and the gem will automatically create accessor methods for them:
|
|
228
|
+
|
|
229
|
+
```ruby
|
|
230
|
+
class Ticket < ApplicationRecord
|
|
231
|
+
enum_field :priority, {
|
|
232
|
+
low: {
|
|
233
|
+
value: 'low',
|
|
234
|
+
label: 'Low Priority',
|
|
235
|
+
sla_hours: 72,
|
|
236
|
+
notify_manager: false,
|
|
237
|
+
},
|
|
238
|
+
high: {
|
|
239
|
+
value: 'high',
|
|
240
|
+
label: 'High Priority',
|
|
241
|
+
sla_hours: 4,
|
|
242
|
+
notify_manager: true,
|
|
243
|
+
},
|
|
244
|
+
}
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Access custom properties directly via generated methods
|
|
248
|
+
ticket.priority_sla_hours # 72
|
|
249
|
+
ticket.priority_notify_manager # false
|
|
250
|
+
|
|
251
|
+
# Or access via metadata hash
|
|
252
|
+
ticket.priority_metadata[:sla_hours] # 72
|
|
253
|
+
ticket.priority_metadata[:notify_manager] # false
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Development
|
|
257
|
+
|
|
258
|
+
After checking out the repo, run:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
bundle install
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Run the test suite:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
bundle exec rspec
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Contributing
|
|
271
|
+
|
|
272
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kinnell/enum_fields.
|
|
273
|
+
|
|
274
|
+
## License
|
|
275
|
+
|
|
276
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module EnumFields
|
|
4
|
+
class Definition < SimpleDelegator
|
|
5
|
+
STANDARD_PROPERTIES = %i[
|
|
6
|
+
value
|
|
7
|
+
label
|
|
8
|
+
].freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :properties
|
|
11
|
+
|
|
12
|
+
def initialize(data)
|
|
13
|
+
@data = build(data)
|
|
14
|
+
raise InvalidDefinitionsError, 'Definitions must be a Hash or Array' unless valid_hash?(@data)
|
|
15
|
+
|
|
16
|
+
@properties = Set.new(STANDARD_PROPERTIES)
|
|
17
|
+
@properties.merge(@data.flat_map { |_, metadata| metadata.keys })
|
|
18
|
+
|
|
19
|
+
super(@data)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def data
|
|
23
|
+
__getobj__
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def valid?
|
|
27
|
+
valid_hash?(__getobj__)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def build(input)
|
|
33
|
+
output = begin
|
|
34
|
+
case input
|
|
35
|
+
when HashWithIndifferentAccess
|
|
36
|
+
input
|
|
37
|
+
when Hash
|
|
38
|
+
input.with_indifferent_access
|
|
39
|
+
when Array
|
|
40
|
+
input.to_h do |value|
|
|
41
|
+
[value, { value: value }]
|
|
42
|
+
end.with_indifferent_access
|
|
43
|
+
else
|
|
44
|
+
raise InvalidDefinitionsError, "Invalid definitions format: #{input.class}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
output.transform_values do |metadata|
|
|
49
|
+
if metadata.key?(:label)
|
|
50
|
+
metadata
|
|
51
|
+
else
|
|
52
|
+
metadata.merge(label: metadata[:value])
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def valid_hash?(data)
|
|
58
|
+
return false unless data.is_a?(HashWithIndifferentAccess)
|
|
59
|
+
return false unless data.values.all? { |metadata| metadata.key?(:value) }
|
|
60
|
+
|
|
61
|
+
true
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module EnumFields
|
|
4
|
+
class EnumField
|
|
5
|
+
def self.define(**args)
|
|
6
|
+
new(**args).define!
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def initialize(model_class:, accessor:, definition:, options: {})
|
|
10
|
+
@model_class = model_class
|
|
11
|
+
@accessor = accessor.to_sym
|
|
12
|
+
@column_name = options.fetch(:column, @accessor).to_sym
|
|
13
|
+
@definition = Definition.new(definition)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def define!
|
|
17
|
+
store_definition!
|
|
18
|
+
define_class_methods!
|
|
19
|
+
define_instance_getter!
|
|
20
|
+
define_instance_setter!
|
|
21
|
+
define_metadata_method!
|
|
22
|
+
define_property_methods!
|
|
23
|
+
define_inquiry_methods!
|
|
24
|
+
define_scopes!
|
|
25
|
+
define_validation!
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def store_definition!
|
|
31
|
+
@model_class.enum_fields[@accessor] = @definition.data
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def define_class_methods!
|
|
35
|
+
collection_name = @accessor.to_s.pluralize
|
|
36
|
+
definition_data = @definition.data
|
|
37
|
+
|
|
38
|
+
@model_class.define_singleton_method(collection_name) do
|
|
39
|
+
definition_data
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@model_class.define_singleton_method("#{collection_name}_count") do
|
|
43
|
+
definition_data.size
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
@model_class.define_singleton_method("#{@accessor}_values") do
|
|
47
|
+
definition_data.values.pluck(:value)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
@model_class.define_singleton_method("#{@accessor}_options") do
|
|
51
|
+
definition_data.map do |key, metadata|
|
|
52
|
+
[metadata[:label], key.to_s]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def define_instance_getter!
|
|
58
|
+
return if @accessor == @column_name
|
|
59
|
+
|
|
60
|
+
accessor = @accessor
|
|
61
|
+
column_name = @column_name
|
|
62
|
+
|
|
63
|
+
@model_class.define_method(accessor) do
|
|
64
|
+
attributes[column_name.to_s]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def define_instance_setter!
|
|
69
|
+
return if @accessor == @column_name
|
|
70
|
+
|
|
71
|
+
accessor = @accessor
|
|
72
|
+
column_name = @column_name
|
|
73
|
+
|
|
74
|
+
@model_class.define_method("#{accessor}=") do |value|
|
|
75
|
+
public_send("#{column_name}=", value)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def define_metadata_method!
|
|
80
|
+
accessor = @accessor
|
|
81
|
+
column_name = @column_name
|
|
82
|
+
|
|
83
|
+
@model_class.define_method("#{accessor}_metadata") do
|
|
84
|
+
column_value = attributes[column_name.to_s]
|
|
85
|
+
return nil if column_value.nil?
|
|
86
|
+
|
|
87
|
+
definitions = self.class.enum_field_for(accessor)
|
|
88
|
+
return nil if definitions.blank?
|
|
89
|
+
|
|
90
|
+
definitions[column_value]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def define_property_methods!
|
|
95
|
+
accessor = @accessor
|
|
96
|
+
column_name = @column_name
|
|
97
|
+
|
|
98
|
+
@definition.properties.each do |property|
|
|
99
|
+
@model_class.define_method("#{accessor}_#{property}") do
|
|
100
|
+
column_value = attributes[column_name.to_s]
|
|
101
|
+
return nil if column_value.nil?
|
|
102
|
+
|
|
103
|
+
definitions = self.class.enum_field_for(accessor)
|
|
104
|
+
return nil if definitions.blank?
|
|
105
|
+
|
|
106
|
+
definitions.dig(column_value, property)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def define_inquiry_methods!
|
|
112
|
+
accessor = @accessor
|
|
113
|
+
column_name = @column_name
|
|
114
|
+
|
|
115
|
+
@definition.each do |key, metadata|
|
|
116
|
+
definition_value = metadata[:value]
|
|
117
|
+
@model_class.define_method("#{key}_#{accessor}?") do
|
|
118
|
+
public_send(column_name) == definition_value
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def define_scopes!
|
|
124
|
+
column_name = @column_name
|
|
125
|
+
|
|
126
|
+
@definition.each do |key, metadata|
|
|
127
|
+
definition_value = metadata[:value]
|
|
128
|
+
@model_class.scope("#{key}_#{@accessor}", -> { where(column_name => definition_value) })
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def define_validation!
|
|
133
|
+
return if @definition.blank?
|
|
134
|
+
|
|
135
|
+
valid_values = @definition.values.pluck(:value)
|
|
136
|
+
@model_class.validates(@column_name, inclusion: {
|
|
137
|
+
in: valid_values,
|
|
138
|
+
allow_nil: true,
|
|
139
|
+
})
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
data/lib/enum_fields.rb
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'delegate'
|
|
4
|
+
require 'active_support/concern'
|
|
5
|
+
require 'active_support/dependencies/autoload'
|
|
6
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
|
7
|
+
require 'active_support/core_ext/array/extract_options'
|
|
8
|
+
require 'active_support/inflector'
|
|
9
|
+
require 'active_record'
|
|
10
|
+
|
|
11
|
+
require_relative 'enum_fields/errors'
|
|
12
|
+
require_relative 'enum_fields/version'
|
|
13
|
+
|
|
14
|
+
module EnumFields
|
|
15
|
+
extend ActiveSupport::Concern
|
|
16
|
+
extend ActiveSupport::Autoload
|
|
17
|
+
|
|
18
|
+
autoload :Definition
|
|
19
|
+
autoload :EnumField
|
|
20
|
+
|
|
21
|
+
class_methods do
|
|
22
|
+
def enum_field(accessor, definition, options = {})
|
|
23
|
+
raise MissingDefinitionsError unless definition.present?
|
|
24
|
+
|
|
25
|
+
EnumField.define(
|
|
26
|
+
model_class: self,
|
|
27
|
+
accessor: accessor,
|
|
28
|
+
definition: definition,
|
|
29
|
+
options: options
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def enum_field_for(accessor)
|
|
34
|
+
enum_fields[accessor]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def enum_fields
|
|
38
|
+
@enum_fields ||= {}.with_indifferent_access
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: enum_fields
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kinnell Shah
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activerecord
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '9.0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '6.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '9.0'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: activesupport
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '6.0'
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '9.0'
|
|
42
|
+
type: :runtime
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '6.0'
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '9.0'
|
|
52
|
+
description: 'A Rails gem that provides enum-like functionality with support for metadata
|
|
53
|
+
properties (label, icon, color, tooltip) and automatic scopes, validations, and
|
|
54
|
+
inquiry methods.
|
|
55
|
+
|
|
56
|
+
'
|
|
57
|
+
email:
|
|
58
|
+
- kinnell@gmail.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- CHANGELOG.md
|
|
64
|
+
- LICENSE
|
|
65
|
+
- README.md
|
|
66
|
+
- lib/enum_fields.rb
|
|
67
|
+
- lib/enum_fields/definition.rb
|
|
68
|
+
- lib/enum_fields/enum_field.rb
|
|
69
|
+
- lib/enum_fields/errors.rb
|
|
70
|
+
- lib/enum_fields/version.rb
|
|
71
|
+
homepage: https://github.com/kinnell/enum_fields
|
|
72
|
+
licenses:
|
|
73
|
+
- MIT
|
|
74
|
+
metadata:
|
|
75
|
+
homepage_uri: https://github.com/kinnell/enum_fields
|
|
76
|
+
github_repo: ssh://github.com/kinnell/enum_fields
|
|
77
|
+
source_code_uri: https://github.com/kinnell/enum_fields
|
|
78
|
+
changelog_uri: https://github.com/kinnell/enum_fields/blob/main/CHANGELOG.md
|
|
79
|
+
bug_tracker_uri: https://github.com/kinnell/enum_fields/issues
|
|
80
|
+
rubygems_mfa_required: 'true'
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: 2.7.6
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '2.0'
|
|
94
|
+
requirements: []
|
|
95
|
+
rubygems_version: 3.7.2
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: Enhanced enum-like fields for ActiveRecord models with metadata support
|
|
98
|
+
test_files: []
|